add: grab linux ctrl_c handle

This commit is contained in:
taynpg 2025-02-18 04:01:55 +08:00
parent 86e71fbb46
commit 03208e4b97

View File

@ -39,6 +39,7 @@ namespace fs = std::filesystem;
#ifndef OS_UNIX
#define OS_UNIX
#include <csignal>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
@ -68,6 +69,7 @@ namespace fs = std::filesystem;
#else
#define ENTER 10
#endif
#define CTRL_C 3
#define BACKSPACE 127
#define LEFT 68
#define RIGHT 67
@ -94,6 +96,7 @@ static char* main_buf{};
static std::mutex mut;
static std::string header{};
static size_t header_len{};
static size_t unix_signal{};
void trans2buf(char* buffer);
void clear_line();
@ -236,7 +239,8 @@ int _getch()
// Disable echo
new_attr = old_attr;
new_attr.c_lflag &= ~(ICANON | ECHO);
new_attr.c_lflag &= ~(ICANON | ECHO | ISIG);
if (tcsetattr(STDIN_FILENO, TCSANOW, &new_attr) == -1) {
fprintf(stderr, "[ERROR] Couldn't set terminal attributes\n");
exit(1);
@ -244,7 +248,6 @@ int _getch()
// 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");
@ -598,12 +601,15 @@ char* fc_readline()
refresh_show();
append_his(main_buf);
return main_buf;
#if defined(OS_WINDOWS)
case CTRL_C: {
free(main_buf);
#ifdef OS_UNIX
fc_lock_print();
printf("\033[0m");
fc_unlock_print();
#endif
return nullptr;
}
#endif
case BACKSPACE: {
if (wo > 0) {
for (size_t i = wo - 1; i < buf.size() - 1; ++i) {
@ -830,3 +836,4 @@ void color_print(const char* text, const COLOR_TYPE color)
#endif
fc_unlock_print();
}