add:添加上下功能键翻阅历史输入。
This commit is contained in:
parent
569ba7232d
commit
2501ef76bc
21
.vscode/settings.json
vendored
21
.vscode/settings.json
vendored
@ -89,6 +89,25 @@
|
|||||||
"xloctime": "cpp",
|
"xloctime": "cpp",
|
||||||
"xtree": "cpp",
|
"xtree": "cpp",
|
||||||
"deque": "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"
|
||||||
}
|
}
|
||||||
}
|
}
|
115
filecomplete.cpp
115
filecomplete.cpp
@ -1,9 +1,9 @@
|
|||||||
#include "filecomplete.h"
|
#include "filecomplete.h"
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
#ifdef USE_BOOST_FILESYSTEM
|
#ifdef USE_BOOST_FILESYSTEM
|
||||||
#include <boost/filesystem.hpp>
|
#include <boost/filesystem.hpp>
|
||||||
@ -101,6 +101,43 @@ static std::vector<std::string> cur_work_content;
|
|||||||
static std::vector<char> deadline_vch;
|
static std::vector<char> deadline_vch;
|
||||||
static std::string str_predict;
|
static std::string str_predict;
|
||||||
static std::map<std::string, std::vector<std::string>> path_cache;
|
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)
|
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;
|
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)
|
void supply(std::vector<char>& wch, char ch)
|
||||||
{
|
{
|
||||||
#if defined(BINARY_GBK)
|
#if defined(BINARY_GBK)
|
||||||
@ -462,6 +527,7 @@ char* fc_readline()
|
|||||||
|
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case ENTER:
|
case ENTER:
|
||||||
|
append_his(main_buf);
|
||||||
return main_buf;
|
return main_buf;
|
||||||
#if defined(OS_WINDOWS)
|
#if defined(OS_WINDOWS)
|
||||||
case CTRL_C: {
|
case CTRL_C: {
|
||||||
@ -482,29 +548,7 @@ char* fc_readline()
|
|||||||
case TAB: {
|
case TAB: {
|
||||||
// 在这里补全
|
// 在这里补全
|
||||||
if (!str_predict.empty()) {
|
if (!str_predict.empty()) {
|
||||||
std::vector<std::vector<char>> temp;
|
auto temp = str_to_vec(str_predict);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
str_predict.clear();
|
str_predict.clear();
|
||||||
for (const auto& item : temp) {
|
for (const auto& item : temp) {
|
||||||
add_newer(item);
|
add_newer(item);
|
||||||
@ -536,10 +580,29 @@ char* fc_readline()
|
|||||||
++wo;
|
++wo;
|
||||||
}
|
}
|
||||||
break;
|
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;
|
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;
|
break;
|
||||||
|
}
|
||||||
case DEL:
|
case DEL:
|
||||||
// Edit buffer like DELETE key
|
// Edit buffer like DELETE key
|
||||||
#if defined(OS_UNIX)
|
#if defined(OS_UNIX)
|
||||||
|
2
main.cpp
2
main.cpp
@ -10,8 +10,8 @@ int main(int argc, char** argv)
|
|||||||
while (1) {
|
while (1) {
|
||||||
char* content = fc_readline();
|
char* content = fc_readline();
|
||||||
if (content) {
|
if (content) {
|
||||||
std::cout << std::string(content) << std::endl;
|
|
||||||
fc_free(content);
|
fc_free(content);
|
||||||
|
std::cout << "" << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user