From 6523dc38b6b251cd6973e0fffba52e16ebeb396b Mon Sep 17 00:00:00 2001 From: taynpg Date: Thu, 9 Jan 2025 12:58:30 +0800 Subject: [PATCH] =?UTF-8?q?debug=EF=BC=9A=E5=88=9D=E6=AD=A5=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E8=A1=A5=E5=85=A8=E6=8F=90=E7=A4=BA=E5=BD=93=E5=89=8D?= =?UTF-8?q?=E5=B7=A5=E4=BD=9C=E7=9B=AE=E5=BD=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 16 ++++- filecomplete.cpp | 162 +++++++++++++++++++++++++++++++++++------- filecomplete.h | 14 ++++ main.cpp | 1 + 4 files changed, 165 insertions(+), 28 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 87a0026..fc82f5a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -71,6 +71,20 @@ "xstddef": "cpp", "xstring": "cpp", "xtr1common": "cpp", - "xutility": "cpp" + "xutility": "cpp", + "filesystem": "cpp", + "algorithm": "cpp", + "cctype": "cpp", + "chrono": "cpp", + "ctime": "cpp", + "iomanip": "cpp", + "list": "cpp", + "locale": "cpp", + "ratio": "cpp", + "string_view": "cpp", + "xlocbuf": "cpp", + "xlocmes": "cpp", + "xlocmon": "cpp", + "xloctime": "cpp" } } \ No newline at end of file diff --git a/filecomplete.cpp b/filecomplete.cpp index 8c5705f..a4fcc00 100644 --- a/filecomplete.cpp +++ b/filecomplete.cpp @@ -2,6 +2,14 @@ #include #include +#ifdef USE_BOOST_FILESYSTEM +#include +namespace fs = boost::filesystem; +#else +#include +namespace fs = std::filesystem; +#endif + #ifdef _MSC_VER #ifndef _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS @@ -81,7 +89,59 @@ void clear_line(); void set_cursor_x(short x); short get_cursor_y(); char* fc_readline(); -void color_print(char* text, COLOR_TYPE color); +void color_print(const char* text, COLOR_TYPE color); + +static std::vector cur_work_content; +static std::vector dynamic_content; +static std::vector deadline_vch; +static std::string str_predict; + +void flush_content(const std::string& search_dir, std::vector& out, + bool reletive) +{ + out.clear(); + fs::path work_path(search_dir); + if (!fs::exists(work_path)) { + return; + } + for (const auto& entry : fs::directory_iterator(work_path)) { + if (reletive) { + out.push_back(entry.path().filename().string()); + } else { + out.push_back(entry.path().string()); + } + } +} + +std::string file_predict(const char* data) +{ + std::string result; + std::string cur(data); + + std::string not_key(" "); + std::string search_key; + for (const auto& item : deadline_vch) { + not_key.push_back(item); + } + auto fk = cur.find_last_of(not_key); + if (fk != std::string::npos) { + search_key = cur.substr(fk + 1, cur.size() - fk); + } else { + search_key = cur; + } + if (cur.find("/") == std::string::npos && + cur.find("\\") == std::string::npos) { + for (const auto& item : cur_work_content) { + if (item != search_key && item.find(search_key) == 0) { + return item.substr(search_key.size(), + item.size() - search_key.size()); + } + } + } + for (const auto& item : dynamic_content) { + } + return result; +} std::pair get_wh() { @@ -249,6 +309,11 @@ short get_cursor_y() #endif } +void fc_append(char deadline_ch) +{ + deadline_vch.push_back(deadline_ch); +} + char* fc_readline() { char* buffer = (char*)calloc((size_t)buf_size, sizeof(char)); @@ -257,6 +322,26 @@ char* fc_readline() exit(1); } + flush_content(fs::current_path().string(), cur_work_content, true); + + auto add_newer = [&](const std::vector& wch) { + 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] = wch; + } else { + buf.push_back(wch); + } + ++wo; + ++len; + }; + // Current hint number int hint_num = 0; while (1) { @@ -264,8 +349,10 @@ char* fc_readline() word.clear(); clear_line(); // Print current buffer - color_print(buffer, DEFAULT_MAIN_COLOR); - + color_print(nullptr, DEFAULT_MAIN_COLOR); + if (!str_predict.empty()) { + color_print(str_predict.data(), DEFAULT_PREDICT_COLOR); + } // Move cursor int cur_pos{}; for (int i = 0; i < buf.size() && i < wo; ++i) { @@ -295,6 +382,29 @@ char* fc_readline() break; } case TAB: { + // 在这里补全 + if (!str_predict.empty()) { + std::vector> temp; + for (int i = 0; i < str_predict.size(); ++i) { + std::vector blk; + char curch = str_predict[i]; + blk.push_back(curch); + if (curch >= 0 && curch < 128) { + temp.push_back(blk); + } else { + if ((i + 1) < str_predict.size()) { + blk.push_back(str_predict[i + 1]); + temp.push_back(blk); + ++i; + } + } + } + str_predict.clear(); + for (const auto& item : temp) { + add_newer(item); + } + continue; + } break; } #if defined(OS_WINDOWS) @@ -335,24 +445,19 @@ char* fc_readline() } default: { 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; + add_newer(word); break; } } + // 预测 + std::string tbuf; + 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'); + str_predict = file_predict(tbuf.data()); } return buffer; } @@ -362,7 +467,7 @@ void fc_free(char* str) free(str); } -void color_print(char* text, COLOR_TYPE color) +void color_print(const char* text, COLOR_TYPE color) { #if defined(OS_WINDOWS) HANDLE h_console = GetStdHandle(STD_OUTPUT_HANDLE); @@ -390,16 +495,19 @@ void color_print(char* text, COLOR_TYPE color) exit(1); } - std::string tbuf; - - for (size_t i = 0; i < buf.size() && i < len; ++i) { - for (const auto& c : buf[i]) { - tbuf.push_back(c); + if (!text) { + std::string tbuf; + 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()); - tbuf.push_back('\0'); - printf("%s", tbuf.c_str()); + } else { + printf("%s", text); + } HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cursorInfo; diff --git a/filecomplete.h b/filecomplete.h index 160b7a1..45ffe91 100644 --- a/filecomplete.h +++ b/filecomplete.h @@ -1,4 +1,18 @@ #pragma once +/* **************************************************************************************************** + 添加截止符号,提示路径时,以此截断,比如当前输入为: + GetFile someparm|tesa_fil_ + 如果设置了截止符号|, 提示时将认为 tesa_fil 是某个文件的名称开头,否则认为 someparm|tesa_fil 是文件名称的开头 +*****************************************************************************************************/ +void fc_append(char deadline_ch); + +/***************************************************************************************************** + 读取用户输入,替代 std::getline +*****************************************************************************************************/ char* fc_readline(); + +/***************************************************************************************************** + 用于释放 fc_readline() 返回的 buffer 空间。 +*****************************************************************************************************/ void fc_free(char* str); \ No newline at end of file diff --git a/main.cpp b/main.cpp index bd82562..17a951b 100644 --- a/main.cpp +++ b/main.cpp @@ -6,6 +6,7 @@ int main(int argc, char** argv) { std::cout << "PreContent1 " << std::endl; std::cout << "PreContent2 " << std::endl; + fc_append('|'); while (1) { char* content = fc_readline(); if (content) {