debug:初步可以补全提示当前工作目录。
This commit is contained in:
parent
16b040363f
commit
6523dc38b6
16
.vscode/settings.json
vendored
16
.vscode/settings.json
vendored
@ -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"
|
||||
}
|
||||
}
|
162
filecomplete.cpp
162
filecomplete.cpp
@ -2,6 +2,14 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifdef USE_BOOST_FILESYSTEM
|
||||
#include <boost/filesystem.hpp>
|
||||
namespace fs = boost::filesystem;
|
||||
#else
|
||||
#include <filesystem>
|
||||
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<std::string> cur_work_content;
|
||||
static std::vector<std::string> dynamic_content;
|
||||
static std::vector<char> deadline_vch;
|
||||
static std::string str_predict;
|
||||
|
||||
void flush_content(const std::string& search_dir, std::vector<std::string>& 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<short, short> 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<char>& 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<std::vector<char>> temp;
|
||||
for (int i = 0; i < str_predict.size(); ++i) {
|
||||
std::vector<char> 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;
|
||||
|
@ -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);
|
Loading…
x
Reference in New Issue
Block a user