colorfail
This commit is contained in:
parent
5baccdd8ae
commit
addb7e6663
@ -6,97 +6,93 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
|
#include <locale>
|
||||||
|
#include <cwchar>
|
||||||
#include "interactive.hpp"
|
#include "interactive.hpp"
|
||||||
|
|
||||||
namespace interactive {
|
namespace interactive {
|
||||||
|
|
||||||
// Helper function to convert ANSI color codes to ncurses attributes
|
|
||||||
int ansi_to_ncurses_attr(const std::string& ansi_code) {
|
|
||||||
if (ansi_code == "0") return A_NORMAL;
|
|
||||||
if (ansi_code == "1") return A_BOLD;
|
|
||||||
|
|
||||||
// Handle colors
|
|
||||||
if (ansi_code == "1;31") return A_BOLD | COLOR_PAIR(2); // Red
|
|
||||||
if (ansi_code == "1;32") return A_BOLD | COLOR_PAIR(3); // Green
|
|
||||||
if (ansi_code == "1;33") return A_BOLD | COLOR_PAIR(4); // Yellow
|
|
||||||
if (ansi_code == "1;34") return A_BOLD | COLOR_PAIR(5); // Blue
|
|
||||||
if (ansi_code == "1;35") return A_BOLD | COLOR_PAIR(6); // Magenta
|
|
||||||
if (ansi_code == "1;36") return A_BOLD | COLOR_PAIR(7); // Cyan
|
|
||||||
if (ansi_code == "1;37") return A_BOLD | COLOR_PAIR(8); // White
|
|
||||||
if (ansi_code == "90") return COLOR_PAIR(9); // Dark Grey
|
|
||||||
if (ansi_code == "38;5;142") return COLOR_PAIR(10); // Dark Yellow
|
|
||||||
if (ansi_code == "38;5;250") return COLOR_PAIR(11); // Light Grey
|
|
||||||
|
|
||||||
return A_NORMAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
int fullscreen_window::ncurses_streambuf::overflow(int c) {
|
int fullscreen_window::ncurses_streambuf::overflow(int c) {
|
||||||
if (c != EOF) {
|
if (c != EOF) {
|
||||||
buffer += static_cast<char>(c);
|
// Check for ANSI escape sequence
|
||||||
if (c == '\n') {
|
if (c == '\033') {
|
||||||
// Process ANSI escape sequences
|
is_escape_sequence = true;
|
||||||
std::string processed;
|
escape_sequence = "\033";
|
||||||
size_t pos = 0;
|
} else if (is_escape_sequence) {
|
||||||
while (pos < buffer.length()) {
|
escape_sequence += static_cast<char>(c);
|
||||||
if (buffer[pos] == '\033' && pos + 1 < buffer.length() && buffer[pos + 1] == '[') {
|
|
||||||
// Found ANSI escape sequence
|
// Check if the escape sequence is complete
|
||||||
size_t end = buffer.find('m', pos);
|
if (escape_sequence.size() >= 2 && escape_sequence[1] == '[' &&
|
||||||
if (end != std::string::npos) {
|
(c == 'm' || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))) {
|
||||||
std::string ansi_code = buffer.substr(pos + 2, end - pos - 2);
|
|
||||||
int ncurses_attr = ansi_to_ncurses_attr(ansi_code);
|
// Handle color codes
|
||||||
wattron(win, ncurses_attr);
|
if (escape_sequence.back() == 'm') {
|
||||||
pos = end + 1;
|
// Process color code
|
||||||
} else {
|
if (escape_sequence == "\033[0m") {
|
||||||
// Invalid ANSI sequence, just print it
|
wattrset(win, A_NORMAL); // Reset attributes
|
||||||
processed += buffer[pos++];
|
} else if (escape_sequence == "\033[1;31m") {
|
||||||
}
|
wattron(win, COLOR_PAIR(1)); // Red
|
||||||
} else {
|
} else if (escape_sequence == "\033[1;32m") {
|
||||||
processed += buffer[pos++];
|
wattron(win, COLOR_PAIR(2)); // Green
|
||||||
|
} else if (escape_sequence == "\033[1;33m") {
|
||||||
|
wattron(win, COLOR_PAIR(3)); // Yellow
|
||||||
|
} else if (escape_sequence == "\033[1;34m") {
|
||||||
|
wattron(win, COLOR_PAIR(4)); // Blue
|
||||||
|
} else if (escape_sequence == "\033[1;35m") {
|
||||||
|
wattron(win, COLOR_PAIR(5)); // Magenta
|
||||||
|
} else if (escape_sequence == "\033[1;36m") {
|
||||||
|
wattron(win, COLOR_PAIR(6)); // Cyan
|
||||||
|
} else if (escape_sequence == "\033[1;37m") {
|
||||||
|
wattron(win, COLOR_PAIR(7)); // White
|
||||||
|
} else if (escape_sequence == "\033[90m") {
|
||||||
|
wattron(win, A_DIM | COLOR_PAIR(7)); // Dark grey
|
||||||
|
} else if (escape_sequence == "\033[38;5;142m") {
|
||||||
|
wattron(win, COLOR_PAIR(3) | A_DIM); // Dark yellow
|
||||||
|
} else if (escape_sequence == "\033[38;5;250m") {
|
||||||
|
wattron(win, COLOR_PAIR(7) | A_DIM); // Light grey
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
wprintw(win, "%s", processed.c_str());
|
is_escape_sequence = false;
|
||||||
|
escape_sequence.clear();
|
||||||
|
}
|
||||||
|
} else if (c == '\n') {
|
||||||
|
wprintw(win, "%s", buffer.c_str());
|
||||||
wrefresh(win);
|
wrefresh(win);
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
|
} else {
|
||||||
|
buffer += static_cast<char>(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
fullscreen_window::fullscreen_window(std::string title) {
|
fullscreen_window::fullscreen_window(std::string title) {
|
||||||
|
// Set locale for wide character support
|
||||||
|
std::setlocale(LC_ALL, "");
|
||||||
|
|
||||||
// Initialize ncurses
|
// Initialize ncurses
|
||||||
initscr();
|
initscr();
|
||||||
start_color();
|
|
||||||
use_default_colors();
|
|
||||||
|
|
||||||
// Initialize color pairs
|
|
||||||
init_pair(1, COLOR_BLACK, -1); // Black
|
|
||||||
init_pair(2, COLOR_RED, -1); // Red
|
|
||||||
init_pair(3, COLOR_GREEN, -1); // Green
|
|
||||||
init_pair(4, COLOR_YELLOW, -1); // Yellow
|
|
||||||
init_pair(5, COLOR_BLUE, -1); // Blue
|
|
||||||
init_pair(6, COLOR_MAGENTA, -1); // Magenta
|
|
||||||
init_pair(7, COLOR_CYAN, -1); // Cyan
|
|
||||||
init_pair(8, COLOR_WHITE, -1); // White
|
|
||||||
init_pair(9, COLOR_BLACK, -1); // Dark Grey
|
|
||||||
init_pair(10, COLOR_YELLOW, -1); // Dark Yellow
|
|
||||||
init_pair(11, COLOR_WHITE, -1); // Light Grey
|
|
||||||
|
|
||||||
raw(); // Use raw mode for better control
|
raw(); // Use raw mode for better control
|
||||||
noecho();
|
noecho();
|
||||||
keypad(stdscr, TRUE); // Enable keypad
|
keypad(stdscr, TRUE); // Enable keypad
|
||||||
curs_set(0); // Hide cursor
|
curs_set(0); // Hide cursor
|
||||||
set_escdelay(25); // Set ESC delay to 25ms
|
set_escdelay(25); // Set ESC delay to 25ms
|
||||||
refresh();
|
|
||||||
|
|
||||||
// Initialize color pairs
|
// Enable color support
|
||||||
init_pair(12, COLOR_RED, -1);
|
start_color();
|
||||||
init_pair(13, COLOR_GREEN, -1);
|
use_default_colors();
|
||||||
init_pair(14, COLOR_YELLOW, -1);
|
|
||||||
init_pair(15, COLOR_BLUE, -1);
|
// Initialize color pairs for ANSI colors
|
||||||
init_pair(16, COLOR_MAGENTA, -1);
|
init_pair(1, COLOR_RED, -1); // Red
|
||||||
init_pair(17, COLOR_CYAN, -1);
|
init_pair(2, COLOR_GREEN, -1); // Green
|
||||||
init_pair(18, COLOR_WHITE, -1);
|
init_pair(3, COLOR_YELLOW, -1); // Yellow
|
||||||
|
init_pair(4, COLOR_BLUE, -1); // Blue
|
||||||
|
init_pair(5, COLOR_MAGENTA, -1); // Magenta
|
||||||
|
init_pair(6, COLOR_CYAN, -1); // Cyan
|
||||||
|
init_pair(7, COLOR_WHITE, -1); // White
|
||||||
|
|
||||||
|
refresh();
|
||||||
|
|
||||||
// Create display window (takes up all but bottom 4 lines)
|
// Create display window (takes up all but bottom 4 lines)
|
||||||
int max_y, max_x;
|
int max_y, max_x;
|
||||||
|
@ -23,6 +23,8 @@ class fullscreen_window {
|
|||||||
private:
|
private:
|
||||||
WINDOW* win;
|
WINDOW* win;
|
||||||
std::string buffer;
|
std::string buffer;
|
||||||
|
bool is_escape_sequence = false;
|
||||||
|
std::string escape_sequence;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual int overflow(int c) override;
|
virtual int overflow(int c) override;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user