From 2501ef76bce6fcd4a9fc01572329ee081b236f49 Mon Sep 17 00:00:00 2001 From: taynpg Date: Tue, 21 Jan 2025 12:43:53 +0800 Subject: [PATCH] =?UTF-8?q?add=EF=BC=9A=E6=B7=BB=E5=8A=A0=E4=B8=8A?= =?UTF-8?q?=E4=B8=8B=E5=8A=9F=E8=83=BD=E9=94=AE=E7=BF=BB=E9=98=85=E5=8E=86?= =?UTF-8?q?=E5=8F=B2=E8=BE=93=E5=85=A5=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 21 +++++++- filecomplete.cpp | 115 ++++++++++++++++++++++++++++++++---------- main.cpp | 2 +- 3 files changed, 110 insertions(+), 28 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 4ddb7cd..8239b74 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -89,6 +89,25 @@ "xloctime": "cpp", "xtree": "cpp", "deque": "cpp", - "array": "cpp" + "array": "cpp", + "functional": "cpp", + "map": "cpp", + "queue": "cpp", + "unordered_map": "cpp", + "xhash": "cpp", + "atomic": "cpp", + "bit": "cpp", + "charconv": "cpp", + "clocale": "cpp", + "compare": "cpp", + "concepts": "cpp", + "format": "cpp", + "forward_list": "cpp", + "iterator": "cpp", + "mutex": "cpp", + "optional": "cpp", + "sstream": "cpp", + "stop_token": "cpp", + "thread": "cpp" } } \ No newline at end of file diff --git a/filecomplete.cpp b/filecomplete.cpp index c44b3d5..e656e3b 100644 --- a/filecomplete.cpp +++ b/filecomplete.cpp @@ -1,9 +1,9 @@ #include "filecomplete.h" #include #include +#include #include #include -#include #ifdef USE_BOOST_FILESYSTEM #include @@ -101,6 +101,43 @@ static std::vector cur_work_content; static std::vector deadline_vch; static std::string str_predict; static std::map> path_cache; +static std::vector history; +static size_t his_pos{}; + +void append_his(const std::string& his) +{ + history.push_back(his); + his_pos = history.size(); +} + +std::string up_his() +{ + if (history.size() < 1) { + return ""; + } + + if (his_pos >= 1) { + --his_pos; + return history[his_pos]; + } else { + return history[0]; + } +} + +std::string next_his() +{ + if (history.size() < 1) { + return ""; + } + + if (his_pos < (history.size() - 1)) { + ++his_pos; + return history[his_pos]; + } else { + his_pos = history.size(); + return ""; + } +} void flush_content(const std::string& search_dir, std::vector& out) { @@ -234,6 +271,34 @@ size_t get_u8_len(unsigned char ch) return 0; } +std::vector> str_to_vec(const std::string& source) +{ + std::vector> result; + for (size_t i = 0; i < source.size();) { + std::vector b; + char curch = source[i]; + b.push_back(curch); + if (curch >= 0) { + result.push_back(b); + ++i; + } else { +#if defined(BINARY_GBK) + size_t length = 2; +#else + size_t length = get_u8_len(curch); +#endif + for (size_t z = 1; z < length; ++z) { + if ((i + z) < source.size()) { + b.push_back(source[i + z]); + } + } + result.push_back(b); + i += length; + } + } + return result; +} + void supply(std::vector& wch, char ch) { #if defined(BINARY_GBK) @@ -462,6 +527,7 @@ char* fc_readline() switch (ch) { case ENTER: + append_his(main_buf); return main_buf; #if defined(OS_WINDOWS) case CTRL_C: { @@ -482,29 +548,7 @@ char* fc_readline() case TAB: { // 在这里补全 if (!str_predict.empty()) { - std::vector> temp; - for (size_t i = 0; i < str_predict.size();) { - std::vector b; - char curch = str_predict[i]; - b.push_back(curch); - if (curch >= 0) { - temp.push_back(b); - ++i; - } else { -#if defined(BINARY_GBK) - size_t length = 2; -#else - size_t length = get_u8_len(curch); -#endif - for (size_t z = 1; z < length; ++z) { - if ((i + z) < str_predict.size()) { - b.push_back(str_predict[i + z]); - } - } - temp.push_back(b); - i += length; - } - } + auto temp = str_to_vec(str_predict); str_predict.clear(); for (const auto& item : temp) { add_newer(item); @@ -536,10 +580,29 @@ char* fc_readline() ++wo; } break; - case UP: + case UP: { + auto up_str = up_his(); + buf.clear(); + auto t = str_to_vec(up_str); + if (t.size() > 0) { + buf.insert(buf.end(), t.begin(), t.end()); + } + len = buf.size(); + wo = len; break; - case DOWN: + } + + case DOWN: { + auto next_str = next_his(); + buf.clear(); + auto t = str_to_vec(next_str); + if (t.size() > 0) { + buf.insert(buf.end(), t.begin(), t.end()); + } + len = buf.size(); + wo = len; break; + } case DEL: // Edit buffer like DELETE key #if defined(OS_UNIX) diff --git a/main.cpp b/main.cpp index 17a951b..fe3d491 100644 --- a/main.cpp +++ b/main.cpp @@ -10,8 +10,8 @@ int main(int argc, char** argv) while (1) { char* content = fc_readline(); if (content) { - std::cout << std::string(content) << std::endl; fc_free(content); + std::cout << "" << std::endl; } } return 0;