fix:1.处理刷新时光标闪烁问题。2.单行范围内基本的移动、插入、删除已OK。

This commit is contained in:
taynpg 2025-01-09 09:07:16 +08:00
parent 306889f6b9
commit f608e091ee
2 changed files with 57 additions and 27 deletions

View File

@ -69,10 +69,10 @@
#define SPACE 32 #define SPACE 32
#define TAB 9 #define TAB 9
static std::vector<std::vector<char>> g_buff{}; static std::vector<std::vector<char>> buf{};
static std::vector<char> g_wch{}; static std::vector<char> word{};
static size_t g_woffset{}; static size_t wo{};
static size_t g_len{}; static size_t len{};
short terminal_width(); short terminal_width();
void clear_line(); void clear_line();
@ -138,6 +138,13 @@ void clear_line()
memset(empty, ' ', width); memset(empty, ' ', width);
empty[width - 1] = '\0'; empty[width - 1] = '\0';
} }
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(hConsole, &cursorInfo);
cursorInfo.bVisible = FALSE;
SetConsoleCursorInfo(hConsole, &cursorInfo);
// Clear line // Clear line
printf("\r%s\r", empty); printf("\r%s\r", empty);
@ -250,28 +257,29 @@ short get_cursor_y()
char* readline() char* readline()
{ {
short buff_cap = terminal_width(); short buff_cap = terminal_width();
char* buff = (char*)calloc((size_t)buff_cap, sizeof(char)); char* buffer = (char*)calloc((size_t)buff_cap, sizeof(char));
if (buff == NULL) { if (buffer == NULL) {
fprintf(stderr, "[ERROR] Couldn't allocate memory for buffer\n"); fprintf(stderr, "[ERROR] Couldn't allocate memory for buffer\n");
exit(1); exit(1);
} }
short buff_len = 0; short buff_len = 0;
// Cursor offset in buffer for moving
int offset = 0;
// Current hint number // Current hint number
int hint_num = 0; int hint_num = 0;
while (1) { while (1) {
g_wch.clear(); word.clear();
clear_line(); clear_line();
// Print current buffer // Print current buffer
color_print(buff, DEFAULT_MAIN_COLOR); color_print(buffer, DEFAULT_MAIN_COLOR);
// Move cursor to buffer end // Move cursor to buffer end
// short x = (short)(buff_len + 1 - offset); int cur_pos{};
// set_cursor_x(x); for (int i = 0; i < buf.size() && i < wo; ++i) {
cur_pos += static_cast<int>(buf[i].size());
}
set_cursor_x(cur_pos + 1);
// Read character from console // Read character from console
int ch = _getch(); int ch = _getch();
@ -285,12 +293,12 @@ char* readline()
} }
#endif #endif
case BACKSPACE: { case BACKSPACE: {
if (g_woffset > 0) { if (wo > 0) {
for (size_t i = g_woffset - 1; i < g_buff.size() - 1; ++i) { for (size_t i = wo - 1; i < buf.size() - 1; ++i) {
g_buff[i] = g_buff[i + 1]; buf[i] = buf[i + 1];
} }
--g_woffset; --wo;
--g_len; --len;
} }
break; break;
} }
@ -311,10 +319,10 @@ char* readline()
#endif #endif
switch (_getch()) { switch (_getch()) {
case LEFT: case LEFT:
offset = offset < 1 ? 0 : (--offset); wo = wo < 1 ? 0 : (--wo);
break; break;
case RIGHT: case RIGHT:
offset = offset > g_buff.size() ? g_buff.size() : (++offset); wo = wo >= len ? len : (++wo);
break; break;
case UP: case UP:
break; break;
@ -334,15 +342,28 @@ char* readline()
break; break;
} }
default: { default: {
supply(g_wch, ch); supply(word, ch);
g_buff.push_back(g_wch); if (wo < buf.size()) {
++g_woffset; if (len >= buf.size()) {
++g_len; buf.resize(buf.size() * 2);
}
if (len > 0) {
for (size_t i = len - 1; i >= wo; --i) {
buf[i + 1] = buf[i];
}
buf[wo] = word;
}
}
else {
buf.push_back(word);
}
++wo;
++len;
break; break;
} }
} }
} }
return buff; return buffer;
} }
void color_print(char* text, COLOR_TYPE color) void color_print(char* text, COLOR_TYPE color)
@ -362,6 +383,7 @@ void color_print(char* text, COLOR_TYPE color)
fprintf(stderr, "[ERROR] Couldn't get terminal info\n"); fprintf(stderr, "[ERROR] Couldn't get terminal info\n");
exit(1); exit(1);
} }
backup = console_info.wAttributes; backup = console_info.wAttributes;
// Print colored text // Print colored text
@ -372,14 +394,21 @@ void color_print(char* text, COLOR_TYPE color)
std::string tbuf; std::string tbuf;
for (size_t i = 0; i < g_buff.size() && i < g_len; ++i) { for (size_t i = 0; i < buf.size() && i < len; ++i) {
for (const auto& c : g_buff[i]) { for (const auto& c : buf[i]) {
tbuf.push_back(c); tbuf.push_back(c);
} }
} }
tbuf.push_back('\0'); tbuf.push_back('\0');
printf("%s", tbuf.c_str()); printf("%s", tbuf.c_str());
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(hConsole, &cursorInfo);
cursorInfo.bVisible = TRUE;
SetConsoleCursorInfo(hConsole, &cursorInfo);
// Restore original color // Restore original color
if (SetConsoleTextAttribute(h_console, backup) == 0) { if (SetConsoleTextAttribute(h_console, backup) == 0) {
fprintf(stderr, "[ERROR] Couldn't reset terminal color\n"); fprintf(stderr, "[ERROR] Couldn't reset terminal color\n");

View File

@ -1,5 +1,6 @@
#include "filecomplete.h" #include "filecomplete.h"
#include <iostream> #include <iostream>
#include <string>
int main(int argc, char** argv) int main(int argc, char** argv)
{ {