From f608e091eeda112ae672d10ccf69b279447fdb1a Mon Sep 17 00:00:00 2001 From: taynpg Date: Thu, 9 Jan 2025 09:07:16 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A1.=E5=A4=84=E7=90=86=E5=88=B7?= =?UTF-8?q?=E6=96=B0=E6=97=B6=E5=85=89=E6=A0=87=E9=97=AA=E7=83=81=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E3=80=822.=E5=8D=95=E8=A1=8C=E8=8C=83=E5=9B=B4?= =?UTF-8?q?=E5=86=85=E5=9F=BA=E6=9C=AC=E7=9A=84=E7=A7=BB=E5=8A=A8=E3=80=81?= =?UTF-8?q?=E6=8F=92=E5=85=A5=E3=80=81=E5=88=A0=E9=99=A4=E5=B7=B2OK?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- filecomplete.cpp | 83 ++++++++++++++++++++++++++++++++---------------- main.cpp | 1 + 2 files changed, 57 insertions(+), 27 deletions(-) diff --git a/filecomplete.cpp b/filecomplete.cpp index 0d59fa3..2edcaef 100644 --- a/filecomplete.cpp +++ b/filecomplete.cpp @@ -69,10 +69,10 @@ #define SPACE 32 #define TAB 9 -static std::vector> g_buff{}; -static std::vector g_wch{}; -static size_t g_woffset{}; -static size_t g_len{}; +static std::vector> buf{}; +static std::vector 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(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"); diff --git a/main.cpp b/main.cpp index 12a9471..d1ab142 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ #include "filecomplete.h" #include +#include int main(int argc, char** argv) {