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