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 TAB 9
static std::vector<std::vector<char>> g_buff{};
static std::vector<char> g_wch{};
static size_t g_woffset{};
static size_t g_len{};
static std::vector<std::vector<char>> buf{};
static std::vector<char> word{};
static size_t wo{};
static size_t len{};
short terminal_width();
void clear_line();
@ -138,6 +138,13 @@ void clear_line()
memset(empty, ' ', width);
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
printf("\r%s\r", empty);
@ -250,29 +257,30 @@ short get_cursor_y()
char* readline()
{
short buff_cap = terminal_width();
char* buff = (char*)calloc((size_t)buff_cap, sizeof(char));
if (buff == NULL) {
char* buffer = (char*)calloc((size_t)buff_cap, sizeof(char));
if (buffer == NULL) {
fprintf(stderr, "[ERROR] Couldn't allocate memory for buffer\n");
exit(1);
}
short buff_len = 0;
// Cursor offset in buffer for moving
int offset = 0;
// Current hint number
int hint_num = 0;
while (1) {
g_wch.clear();
word.clear();
clear_line();
// Print current buffer
color_print(buff, DEFAULT_MAIN_COLOR);
color_print(buffer, DEFAULT_MAIN_COLOR);
// Move cursor to buffer end
// short x = (short)(buff_len + 1 - offset);
// set_cursor_x(x);
int cur_pos{};
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
int ch = _getch();
@ -285,12 +293,12 @@ char* readline()
}
#endif
case BACKSPACE: {
if (g_woffset > 0) {
for (size_t i = g_woffset - 1; i < g_buff.size() - 1; ++i) {
g_buff[i] = g_buff[i + 1];
if (wo > 0) {
for (size_t i = wo - 1; i < buf.size() - 1; ++i) {
buf[i] = buf[i + 1];
}
--g_woffset;
--g_len;
--wo;
--len;
}
break;
}
@ -311,10 +319,10 @@ char* readline()
#endif
switch (_getch()) {
case LEFT:
offset = offset < 1 ? 0 : (--offset);
wo = wo < 1 ? 0 : (--wo);
break;
case RIGHT:
offset = offset > g_buff.size() ? g_buff.size() : (++offset);
wo = wo >= len ? len : (++wo);
break;
case UP:
break;
@ -334,15 +342,28 @@ char* readline()
break;
}
default: {
supply(g_wch, ch);
g_buff.push_back(g_wch);
++g_woffset;
++g_len;
supply(word, ch);
if (wo < buf.size()) {
if (len >= buf.size()) {
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;
}
}
}
return buff;
return buffer;
}
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");
exit(1);
}
backup = console_info.wAttributes;
// Print colored text
@ -372,14 +394,21 @@ void color_print(char* text, COLOR_TYPE color)
std::string tbuf;
for (size_t i = 0; i < g_buff.size() && i < g_len; ++i) {
for (const auto& c : g_buff[i]) {
for (size_t i = 0; i < buf.size() && i < len; ++i) {
for (const auto& c : buf[i]) {
tbuf.push_back(c);
}
}
tbuf.push_back('\0');
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
if (SetConsoleTextAttribute(h_console, backup) == 0) {
fprintf(stderr, "[ERROR] Couldn't reset terminal color\n");

View File

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