From d85f375fbef21c1cfb927eab1420e469d1bbedb6 Mon Sep 17 00:00:00 2001 From: Peisong Xiao Date: Sun, 7 Jul 2024 20:08:13 -0400 Subject: [PATCH] fixed ncurses output attributes --- src/.gitignore | 1 + src/console_output.cc | 7 ++++++- src/constants.h | 3 +++ src/curses_output.cc | 16 +++++++++++++++- src/cursor.cc | 12 ++++++++++++ 5 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 src/.gitignore diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..f4cb8e2 --- /dev/null +++ b/src/.gitignore @@ -0,0 +1 @@ +testing* diff --git a/src/console_output.cc b/src/console_output.cc index 7a5269d..d51f8c4 100644 --- a/src/console_output.cc +++ b/src/console_output.cc @@ -35,12 +35,15 @@ std::string console_output::get_code(const int attr) { std::string result = "\033[0m\033["; - if (attr & A_STANDOUT) + if (attr & A_BOLD) result += "1;"; if (attr & A_UNDERLINE) result += "4;"; + if (attr & A_STANDOUT) + result += "7;"; + if ((attr & COLOR_PAIR(COLOR_WHITE)) == COLOR_PAIR(COLOR_WHITE)) result += "37;"; else if ((attr & COLOR_PAIR(COLOR_CYAN)) == COLOR_PAIR(COLOR_CYAN)) @@ -55,6 +58,8 @@ std::string console_output::get_code(const int attr) { result += "31;"; else if ((attr & COLOR_PAIR(COLOR_GREEN)) == COLOR_PAIR(COLOR_GREEN)) result += "32;"; + else if ((attr & COLOR_PAIR(COLOR_BLACK_ON_WHITE)) == COLOR_BLACK_ON_WHITE) + result += "30;47;"; result[result.length() - 1] = 'm'; return result; diff --git a/src/constants.h b/src/constants.h index 43c56c2..f7ff3c3 100644 --- a/src/constants.h +++ b/src/constants.h @@ -1,6 +1,7 @@ #ifndef __CONSTANTS_H__ #define __CONSTANTS_H__ #include +#include #include "position.h" // IMPORTANT: panic is reserved for invalid results @@ -89,6 +90,8 @@ const feature FEATURE_LIST_ARGS = 1 << 31; const int RETURN_FINE = 0; const int RETURN_PANICKED = 1; +const int COLOR_BLACK_ON_WHITE = 8; + typedef std::vector position_list; typedef std::vector direction_list; diff --git a/src/curses_output.cc b/src/curses_output.cc index 9f347b3..a587343 100644 --- a/src/curses_output.cc +++ b/src/curses_output.cc @@ -13,10 +13,24 @@ void curses_output::clear() { void curses_output::print_char(const position &pos, const char ch, const int attr) { + if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT) + return; + curse->print_char(pos, ch, attr); } void curses_output::print_str(const position &pos, const std::string &str, const int attr) { - curse->print_str(pos, str, attr); + if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT) + return; + + position tmp = pos; + + for (std::size_t i = 0; i < str.length(); ++i) { + curse->print_char(tmp, str[i], attr); + tmp += {1, 0}; + + if (tmp.x >= DISPLAY_WIDTH) + tmp = {0, tmp.y + 1}; + } } diff --git a/src/cursor.cc b/src/cursor.cc index 6bcd485..c3d04f8 100644 --- a/src/cursor.cc +++ b/src/cursor.cc @@ -3,6 +3,18 @@ cursor::cursor() { initscr(); + + if (has_colors()) { + start_color(); + init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK); + init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK); + init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK); + init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK); + init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK); + init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK); + init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK); + init_pair(COLOR_BLACK_ON_WHITE, COLOR_BLACK, COLOR_WHITE); + } } cursor::~cursor() {