From 16633d9d9aea6064a1f070558e680c67076e4f9c Mon Sep 17 00:00:00 2001 From: taynpg Date: Tue, 25 Feb 2025 00:54:00 -0800 Subject: [PATCH] =?UTF-8?q?add=EF=BC=9A=E6=B7=BB=E5=8A=A0~=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E4=B8=BA=E5=AE=9E=E9=99=85=E8=B7=AF=E5=BE=84=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- filecomplete.cpp | 19 +++++++++++++++++++ filecomplete.h | 10 ++++++++++ 2 files changed, 29 insertions(+) diff --git a/filecomplete.cpp b/filecomplete.cpp index 7a280b0..b04758f 100644 --- a/filecomplete.cpp +++ b/filecomplete.cpp @@ -97,6 +97,7 @@ static std::mutex mut; static std::string header{}; static size_t header_len{}; static size_t unix_signal{}; +static bool need_turn{true}; void trans2buf(char* buffer); void clear_line(); @@ -136,6 +137,16 @@ static std::string home_path{}; // #endif // } +void fc_turn_on() +{ + need_turn = true; +} + +void fc_turn_off() +{ + need_turn = false; +} + std::string get_home() { #if defined(_WIN32) @@ -648,6 +659,14 @@ char* fc_readline() str_predict.clear(); refresh_show(); append_his(main_buf); + if (need_turn) { + std::string rep(main_buf); + if (rep.find("~") == 0) { + rep = get_home() + rep.substr(1, rep.size() - 1); + std::memset(main_buf, 0x0, buf_size); + std::snprintf(main_buf, buf_size, "%s", rep.c_str()); + } + } return main_buf; case CTRL_C: { free(main_buf); diff --git a/filecomplete.h b/filecomplete.h index 43f9ba8..dfc9b6d 100644 --- a/filecomplete.h +++ b/filecomplete.h @@ -68,6 +68,16 @@ void fc_unlock_print(void); */ void fc_free(char* str); +/** + * @brief 开启特殊字符替换,如 ~ 回车后会替换为家目录路径。 + */ +void fc_turn_on(); + +/** + * @brief 关闭殊字符替换 + */ +void fc_turn_off(); + #ifdef __cplusplus } #endif