fix:修正Unix下UTF-8编码处理。

This commit is contained in:
taynpg 2025-01-09 15:49:54 +08:00
parent 36730cb4df
commit 378e489947
3 changed files with 111 additions and 15 deletions

View File

@ -86,6 +86,7 @@
"xlocmes": "cpp", "xlocmes": "cpp",
"xlocmon": "cpp", "xlocmon": "cpp",
"xloctime": "cpp", "xloctime": "cpp",
"xtree": "cpp" "xtree": "cpp",
"deque": "cpp"
} }
} }

View File

@ -1,4 +1,5 @@
#include "filecomplete.h" #include "filecomplete.h"
#include <cstring>
#include <map> #include <map>
#include <string> #include <string>
#include <vector> #include <vector>
@ -17,9 +18,9 @@ namespace fs = std::filesystem;
#endif #endif
#endif #endif
#include <stdio.h> #include <cstdio>
#include <stdlib.h> #include <cstdlib>
#include <string.h> #include <cstring>
#define MAX_OF(x, y) (((x) > (y)) ? (x) : (y)) #define MAX_OF(x, y) (((x) > (y)) ? (x) : (y))
@ -91,7 +92,7 @@ void clear_line();
void set_cursor_x(short x); void set_cursor_x(short x);
short get_cursor_y(); short get_cursor_y();
char* fc_readline(); char* fc_readline();
void color_print(const char* text, COLOR_TYPE color); void color_print(const char* text, const COLOR_TYPE color);
static std::vector<std::string> cur_work_content; static std::vector<std::string> cur_work_content;
static std::vector<char> deadline_vch; static std::vector<char> deadline_vch;
@ -153,8 +154,42 @@ std::string file_predict(const char* data)
return result; return result;
} }
#if defined(OS_UNIX)
int _getch()
{
int character;
struct termios old_attr, new_attr;
// Backup terminal attributes
if (tcgetattr(STDIN_FILENO, &old_attr) == -1) {
fprintf(stderr, "[ERROR] Couldn't get terminal attributes\n");
exit(1);
}
// Disable echo
new_attr = old_attr;
new_attr.c_lflag &= ~(ICANON | ECHO);
if (tcsetattr(STDIN_FILENO, TCSANOW, &new_attr) == -1) {
fprintf(stderr, "[ERROR] Couldn't set terminal attributes\n");
exit(1);
}
// Get input character
character = getchar();
// Restore terminal attributes
if (tcsetattr(STDIN_FILENO, TCSANOW, &old_attr) == -1) {
fprintf(stderr, "[ERROR] Couldn't reset terminal attributes\n");
exit(1);
}
return character;
}
#endif
std::pair<short, short> get_wh() std::pair<short, short> get_wh()
{ {
#if defined(OS_WINDOWS)
HANDLE h_console = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE h_console = GetStdHandle(STD_OUTPUT_HANDLE);
if (h_console == NULL) { if (h_console == NULL) {
fprintf(stderr, "[ERROR] Couldn't handle terminal\n"); fprintf(stderr, "[ERROR] Couldn't handle terminal\n");
@ -170,16 +205,51 @@ std::pair<short, short> get_wh()
auto w = static_cast<short>(cw); auto w = static_cast<short>(cw);
auto h = static_cast<short>(ch); auto h = static_cast<short>(ch);
return std::make_pair(w, h); return std::make_pair(w, h);
#elif defined(OS_UNIX)
return std::make_pair(12, 12);
#endif
}
int get_u8_len(unsigned char ch)
{
if (ch >= 0x00 && ch <= 0x7F) {
return 1;
} else if ((ch & 0xE0) == 0xC0) {
return 2;
} else if ((ch & 0xF0) == 0xE0) {
return 3;
} else if ((ch & 0xF8) == 0xF0) {
return 4;
} else if ((ch & 0xFC) == 0xF8) {
return 5;
} else if ((ch & 0xFE) == 0xFC) {
return 6;
} else {
return 0;
}
} }
void supply(std::vector<char>& wch, char ch) void supply(std::vector<char>& wch, char ch)
{ {
#if defined(STRCODE_GBK)
wch.push_back(ch); wch.push_back(ch);
if (ch >= 0 && ch < 128) { if (ch >= 0 && ch < 128) {
return; return;
} }
auto tch = _getch(); auto tch = _getch();
wch.push_back(tch); wch.push_back(tch);
#else
wch.push_back(ch);
int length = get_u8_len(static_cast<unsigned char>(ch));
if (length == 0) {
printf("Invalid Charactor!\n");
exit(1);
}
for (int i = 1; i < length; ++i) {
auto tch = _getch();
wch.push_back(tch);
}
#endif
} }
void clear_line() void clear_line()
@ -367,9 +437,19 @@ char* fc_readline()
} }
// Move cursor // Move cursor
int cur_pos{}; int cur_pos{};
#if defined(STRCODE_GBK)
for (int i = 0; i < buf.size() && i < wo; ++i) { for (int i = 0; i < buf.size() && i < wo; ++i) {
cur_pos += static_cast<int>(buf[i].size()); cur_pos += static_cast<int>(buf[i].size());
} }
#else
for (int i = 0; i < buf.size() && i < wo; ++i) {
if (buf[i].size() > 1) {
cur_pos += 2;
} else {
cur_pos += 1;
}
}
#endif
set_cursor_x(cur_pos + 1); set_cursor_x(cur_pos + 1);
// Read character from console // Read character from console
@ -397,18 +477,26 @@ 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();) {
std::vector<char> b; std::vector<char> b;
char curch = str_predict[i]; char curch = str_predict[i];
b.push_back(curch); b.push_back(curch);
if (curch >= 0 && curch < 128) { if (curch >= 0 && curch < 128) {
temp.push_back(b); temp.push_back(b);
++i;
} else { } else {
if ((i + 1) < str_predict.size()) { #if defined(STRCODE_GBK)
b.push_back(str_predict[i + 1]); int length = 2;
temp.push_back(b); #else
++i; int length = get_u8_len(curch);
#endif
for (int 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();
@ -487,7 +575,7 @@ void trans2buf(char* buffer)
buffer[j++] = '\0'; buffer[j++] = '\0';
} }
void color_print(const char* text, COLOR_TYPE color) void color_print(const char* text, const COLOR_TYPE color)
{ {
#if defined(OS_WINDOWS) #if defined(OS_WINDOWS)
HANDLE h_console = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE h_console = GetStdHandle(STD_OUTPUT_HANDLE);
@ -538,9 +626,12 @@ void color_print(const char* text, COLOR_TYPE color)
printf("%s", color); printf("%s", color);
printf("m"); printf("m");
// Print colored text if (!text) {
printf("%s", text); trans2buf(main_buf);
printf("%s", main_buf);
} else {
printf("%s", text);
}
// Resets the text to default color // Resets the text to default color
printf("\033[0m"); printf("\033[0m");
#endif #endif

View File

@ -1,7 +1,11 @@
#ifndef FILE_COMPLETE_H #ifndef FILE_COMPLETE_H
#define FILE_COMPLETE_H #define FILE_COMPLETE_H
/* ************************************************** /* ***************************************************
GBK,
STRCODE_GBK
UTF-8
******************************************************
GetFile someparm|tesa_fil_ GetFile someparm|tesa_fil_
|, tesa_fil |, tesa_fil