add:添加上下功能键翻阅历史输入。

This commit is contained in:
taynpg 2025-01-21 12:43:53 +08:00
parent 569ba7232d
commit 2501ef76bc
3 changed files with 110 additions and 28 deletions

21
.vscode/settings.json vendored
View File

@ -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"
}
}

View File

@ -1,9 +1,9 @@
#include "filecomplete.h"
#include <cstring>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <memory>
#ifdef USE_BOOST_FILESYSTEM
#include <boost/filesystem.hpp>
@ -101,6 +101,43 @@ static std::vector<std::string> cur_work_content;
static std::vector<char> deadline_vch;
static std::string str_predict;
static std::map<std::string, std::vector<std::string>> path_cache;
static std::vector<std::string> 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<std::string>& out)
{
@ -234,6 +271,34 @@ size_t get_u8_len(unsigned char ch)
return 0;
}
std::vector<std::vector<char>> str_to_vec(const std::string& source)
{
std::vector<std::vector<char>> result;
for (size_t i = 0; i < source.size();) {
std::vector<char> 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<char>& 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<std::vector<char>> temp;
for (size_t i = 0; i < str_predict.size();) {
std::vector<char> 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)

View File

@ -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;