func:基本能用版本(仅win下GBK环境)。
This commit is contained in:
parent
6523dc38b6
commit
36730cb4df
@ -11,7 +11,7 @@ ReflowComments: true
|
|||||||
SpacesBeforeTrailingComments: 3
|
SpacesBeforeTrailingComments: 3
|
||||||
TabWidth: 4
|
TabWidth: 4
|
||||||
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||||
ColumnLimit: 80
|
ColumnLimit: 130
|
||||||
AllowShortBlocksOnASingleLine: Never
|
AllowShortBlocksOnASingleLine: Never
|
||||||
AllowShortFunctionsOnASingleLine: None
|
AllowShortFunctionsOnASingleLine: None
|
||||||
AllowShortEnumsOnASingleLine: false
|
AllowShortEnumsOnASingleLine: false
|
||||||
|
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@ -85,6 +85,7 @@
|
|||||||
"xlocbuf": "cpp",
|
"xlocbuf": "cpp",
|
||||||
"xlocmes": "cpp",
|
"xlocmes": "cpp",
|
||||||
"xlocmon": "cpp",
|
"xlocmon": "cpp",
|
||||||
"xloctime": "cpp"
|
"xloctime": "cpp",
|
||||||
|
"xtree": "cpp"
|
||||||
}
|
}
|
||||||
}
|
}
|
114
filecomplete.cpp
114
filecomplete.cpp
@ -1,4 +1,5 @@
|
|||||||
#include "filecomplete.h"
|
#include "filecomplete.h"
|
||||||
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -30,8 +31,7 @@ namespace fs = std::filesystem;
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#endif
|
#endif
|
||||||
#elif defined(__APPLE__) || defined(__unix__) || defined(__unix) || \
|
#elif defined(__APPLE__) || defined(__unix__) || defined(__unix) || defined(unix) || defined(__linux__)
|
||||||
defined(unix) || defined(__linux__)
|
|
||||||
#ifndef OS_UNIX
|
#ifndef OS_UNIX
|
||||||
#define OS_UNIX
|
#define OS_UNIX
|
||||||
|
|
||||||
@ -84,7 +84,9 @@ static size_t wo{};
|
|||||||
static size_t len{};
|
static size_t len{};
|
||||||
static short cy{};
|
static short cy{};
|
||||||
static short cx{};
|
static short cx{};
|
||||||
|
static char* main_buf{};
|
||||||
|
|
||||||
|
void trans2buf(char* buffer);
|
||||||
void clear_line();
|
void clear_line();
|
||||||
void set_cursor_x(short x);
|
void set_cursor_x(short x);
|
||||||
short get_cursor_y();
|
short get_cursor_y();
|
||||||
@ -92,24 +94,28 @@ char* fc_readline();
|
|||||||
void color_print(const 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> cur_work_content;
|
||||||
static std::vector<std::string> dynamic_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;
|
||||||
|
|
||||||
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)
|
||||||
bool reletive)
|
|
||||||
{
|
{
|
||||||
out.clear();
|
out.clear();
|
||||||
fs::path work_path(search_dir);
|
fs::path work_path(search_dir);
|
||||||
if (!fs::exists(work_path)) {
|
try {
|
||||||
|
if (!fs::exists(work_path)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch (const std::exception& e) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (const auto& entry : fs::directory_iterator(work_path)) {
|
if (path_cache.count(search_dir)) {
|
||||||
if (reletive) {
|
out = path_cache[search_dir];
|
||||||
out.push_back(entry.path().filename().string());
|
} else {
|
||||||
} else {
|
for (const auto& entry : fs::directory_iterator(work_path)) {
|
||||||
out.push_back(entry.path().string());
|
out.push_back(entry.path().lexically_normal().string());
|
||||||
}
|
}
|
||||||
|
path_cache[search_dir] = out;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,16 +135,20 @@ std::string file_predict(const char* data)
|
|||||||
} else {
|
} else {
|
||||||
search_key = cur;
|
search_key = cur;
|
||||||
}
|
}
|
||||||
if (cur.find("/") == std::string::npos &&
|
if (cur.find("/") == std::string::npos && cur.find("\\") == std::string::npos) {
|
||||||
cur.find("\\") == std::string::npos) {
|
|
||||||
for (const auto& item : cur_work_content) {
|
for (const auto& item : cur_work_content) {
|
||||||
if (item != search_key && item.find(search_key) == 0) {
|
if (item != search_key && item.find(search_key) == 0) {
|
||||||
return item.substr(search_key.size(),
|
return item.substr(search_key.size(), item.size() - search_key.size());
|
||||||
item.size() - search_key.size());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (const auto& item : dynamic_content) {
|
std::string bk_search_path = search_key.substr(0, search_key.find_last_of("\\/") + 1);
|
||||||
|
std::vector<std::string> sr;
|
||||||
|
flush_content(bk_search_path, sr);
|
||||||
|
for (const auto& item : sr) {
|
||||||
|
if (item != search_key && item.find(search_key) == 0) {
|
||||||
|
return item.substr(search_key.size(), item.size() - search_key.size());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -224,9 +234,7 @@ void set_cursor_x(short x)
|
|||||||
// Set cursor position
|
// Set cursor position
|
||||||
if (SetConsoleCursorPosition(h_console, xy) == 0) {
|
if (SetConsoleCursorPosition(h_console, xy) == 0) {
|
||||||
auto code = GetLastError();
|
auto code = GetLastError();
|
||||||
fprintf(stderr,
|
fprintf(stderr, "[ERROR] Couldn't set terminal cursor position, err=%lu\n", code);
|
||||||
"[ERROR] Couldn't set terminal cursor position, err=%lu\n",
|
|
||||||
code);
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
#elif defined(OS_UNIX)
|
#elif defined(OS_UNIX)
|
||||||
@ -316,14 +324,21 @@ void fc_append(char deadline_ch)
|
|||||||
|
|
||||||
char* fc_readline()
|
char* fc_readline()
|
||||||
{
|
{
|
||||||
char* buffer = (char*)calloc((size_t)buf_size, sizeof(char));
|
main_buf = (char*)malloc(sizeof(char) * buf_size);
|
||||||
if (buffer == NULL) {
|
std::memset(main_buf, 0x0, buf_size);
|
||||||
|
if (main_buf == NULL) {
|
||||||
fprintf(stderr, "[ERROR] Couldn't allocate memory for buffer\n");
|
fprintf(stderr, "[ERROR] Couldn't allocate memory for buffer\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
flush_content(fs::current_path().string(), cur_work_content, true);
|
char* tmp_buf = (char*)malloc(sizeof(char) * buf_size);
|
||||||
|
std::memset(tmp_buf, 0x0, buf_size);
|
||||||
|
std::shared_ptr<int> deleter(new int(), [tmp_buf](int* p) {
|
||||||
|
delete p;
|
||||||
|
free(tmp_buf);
|
||||||
|
});
|
||||||
|
|
||||||
|
flush_content(".", cur_work_content);
|
||||||
auto add_newer = [&](const std::vector<char>& wch) {
|
auto add_newer = [&](const std::vector<char>& wch) {
|
||||||
if (wo < buf.size()) {
|
if (wo < buf.size()) {
|
||||||
if (len >= buf.size()) {
|
if (len >= buf.size()) {
|
||||||
@ -342,10 +357,7 @@ char* fc_readline()
|
|||||||
++len;
|
++len;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Current hint number
|
|
||||||
int hint_num = 0;
|
|
||||||
while (1) {
|
while (1) {
|
||||||
|
|
||||||
word.clear();
|
word.clear();
|
||||||
clear_line();
|
clear_line();
|
||||||
// Print current buffer
|
// Print current buffer
|
||||||
@ -365,7 +377,7 @@ char* fc_readline()
|
|||||||
|
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case ENTER:
|
case ENTER:
|
||||||
return nullptr;
|
return main_buf;
|
||||||
#if defined(OS_WINDWS)
|
#if defined(OS_WINDWS)
|
||||||
case CTRL_C: {
|
case CTRL_C: {
|
||||||
exit(0);
|
exit(0);
|
||||||
@ -386,15 +398,15 @@ char* fc_readline()
|
|||||||
if (!str_predict.empty()) {
|
if (!str_predict.empty()) {
|
||||||
std::vector<std::vector<char>> temp;
|
std::vector<std::vector<char>> temp;
|
||||||
for (int i = 0; i < str_predict.size(); ++i) {
|
for (int i = 0; i < str_predict.size(); ++i) {
|
||||||
std::vector<char> blk;
|
std::vector<char> b;
|
||||||
char curch = str_predict[i];
|
char curch = str_predict[i];
|
||||||
blk.push_back(curch);
|
b.push_back(curch);
|
||||||
if (curch >= 0 && curch < 128) {
|
if (curch >= 0 && curch < 128) {
|
||||||
temp.push_back(blk);
|
temp.push_back(b);
|
||||||
} else {
|
} else {
|
||||||
if ((i + 1) < str_predict.size()) {
|
if ((i + 1) < str_predict.size()) {
|
||||||
blk.push_back(str_predict[i + 1]);
|
b.push_back(str_predict[i + 1]);
|
||||||
temp.push_back(blk);
|
temp.push_back(b);
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -449,24 +461,32 @@ char* fc_readline()
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 预测
|
// 补正
|
||||||
std::string tbuf;
|
trans2buf(tmp_buf);
|
||||||
for (size_t i = 0; i < buf.size() && i < len; ++i) {
|
str_predict = file_predict(tmp_buf);
|
||||||
for (const auto& c : buf[i]) {
|
|
||||||
tbuf.push_back(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tbuf.push_back('\0');
|
|
||||||
str_predict = file_predict(tbuf.data());
|
|
||||||
}
|
}
|
||||||
return buffer;
|
return main_buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
void fc_free(char* str)
|
void fc_free(char* str)
|
||||||
{
|
{
|
||||||
|
buf.clear();
|
||||||
|
wo = 0;
|
||||||
|
len = 0;
|
||||||
free(str);
|
free(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void trans2buf(char* buffer)
|
||||||
|
{
|
||||||
|
int j = 0;
|
||||||
|
for (size_t i = 0; i < buf.size() && i < len; ++i) {
|
||||||
|
for (const auto& c : buf[i]) {
|
||||||
|
buffer[j++] = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buffer[j++] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
void color_print(const char* text, COLOR_TYPE color)
|
void color_print(const char* text, COLOR_TYPE color)
|
||||||
{
|
{
|
||||||
#if defined(OS_WINDOWS)
|
#if defined(OS_WINDOWS)
|
||||||
@ -486,7 +506,6 @@ void color_print(const char* text, COLOR_TYPE color)
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto wh = get_wh();
|
auto wh = get_wh();
|
||||||
|
|
||||||
backup = console_info.wAttributes;
|
backup = console_info.wAttributes;
|
||||||
|
|
||||||
// Print colored text
|
// Print colored text
|
||||||
@ -496,15 +515,8 @@ void color_print(const char* text, COLOR_TYPE color)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!text) {
|
if (!text) {
|
||||||
std::string tbuf;
|
trans2buf(main_buf);
|
||||||
for (size_t i = 0; i < buf.size() && i < len; ++i) {
|
printf("%s", main_buf);
|
||||||
for (const auto& c : buf[i]) {
|
|
||||||
tbuf.push_back(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tbuf.push_back('\0');
|
|
||||||
printf("%s", tbuf.c_str());
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
printf("%s", text);
|
printf("%s", text);
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,23 @@
|
|||||||
#pragma once
|
#ifndef FILE_COMPLETE_H
|
||||||
|
#define FILE_COMPLETE_H
|
||||||
|
|
||||||
/* ****************************************************************************************************
|
/* **************************************************
|
||||||
添加截止符号,提示路径时,以此截断,比如当前输入为:
|
添加截止符号,提示路径时,以此截断,比如当前输入为:
|
||||||
GetFile someparm|tesa_fil_
|
GetFile someparm|tesa_fil_
|
||||||
如果设置了截止符号|, 提示时将认为 tesa_fil 是某个文件的名称开头,否则认为 someparm|tesa_fil 是文件名称的开头
|
如果设置了截止符号|, 提示时将认为 tesa_fil 是某个
|
||||||
*****************************************************************************************************/
|
文件的名称开头,否则认为 someparm|tesa_fil 是文件
|
||||||
|
名称的开头
|
||||||
|
******************************************************/
|
||||||
void fc_append(char deadline_ch);
|
void fc_append(char deadline_ch);
|
||||||
|
|
||||||
/*****************************************************************************************************
|
/*****************************************************
|
||||||
读取用户输入,替代 std::getline
|
读取用户输入,替代 std::getline
|
||||||
*****************************************************************************************************/
|
******************************************************/
|
||||||
char* fc_readline();
|
char* fc_readline();
|
||||||
|
|
||||||
/*****************************************************************************************************
|
/*****************************************************
|
||||||
用于释放 fc_readline() 返回的 buffer 空间。
|
用于释放 fc_readline() 返回的 buffer 堆内存。
|
||||||
*****************************************************************************************************/
|
******************************************************/
|
||||||
void fc_free(char* str);
|
void fc_free(char* str);
|
||||||
|
|
||||||
|
#endif
|
Loading…
x
Reference in New Issue
Block a user