Merge pull request #137 from chelovechishko/ncurses_color_restore

terminal_ncurses: restore original colors on exit
master
karl 9 years ago committed by GitHub
commit dcc622b87d
  1. 31
      output/terminal_ncurses.c

@ -11,6 +11,11 @@ struct colors {
#define COLOR_REDEFINITION -2 #define COLOR_REDEFINITION -2
#define MAX_COLOR_REDEFINITION 2
static struct colors the_color_redefinitions[MAX_COLOR_REDEFINITION];
static void parse_color(char* color_string, struct colors* color) { static void parse_color(char* color_string, struct colors* color) {
if (color_string[0] == '#') { if (color_string[0] == '#') {
if (!can_change_color()) { if (!can_change_color()) {
@ -24,6 +29,17 @@ static void parse_color(char* color_string, struct colors* color) {
} }
} }
static void remember_color_definition(NCURSES_COLOR_T color_number) {
int index = color_number - 1; // array starts from zero and colors - not
if(the_color_redefinitions[index].color == 0) {
the_color_redefinitions[index].color = color_number;
color_content(color_number,
&the_color_redefinitions[index].R,
&the_color_redefinitions[index].G,
&the_color_redefinitions[index].B);
}
}
// ncurses use color range [0, 1000], and we - [0, 255] // ncurses use color range [0, 1000], and we - [0, 255]
#define CURSES_COLOR_COEFFICIENT( X ) (( X ) * 1000.0 / 0xFF + 0.5) #define CURSES_COLOR_COEFFICIENT( X ) (( X ) * 1000.0 / 0xFF + 0.5)
#define COLORS_STRUCT_NORMALIZE( X ) CURSES_COLOR_COEFFICIENT( X.R ), \ #define COLORS_STRUCT_NORMALIZE( X ) CURSES_COLOR_COEFFICIENT( X.R ), \
@ -35,6 +51,7 @@ char* const color_string, NCURSES_COLOR_T predef_color) {
parse_color(color_string, &color); parse_color(color_string, &color);
NCURSES_COLOR_T return_color_number = predef_color; NCURSES_COLOR_T return_color_number = predef_color;
if (color.color == COLOR_REDEFINITION) { if (color.color == COLOR_REDEFINITION) {
remember_color_definition(color_number);
init_color(color_number, COLORS_STRUCT_NORMALIZE(color)); init_color(color_number, COLORS_STRUCT_NORMALIZE(color));
return_color_number = color_number; return_color_number = color_number;
} }
@ -55,6 +72,8 @@ char* const bg_color_string, int predef_fg_color, int predef_bg_color) {
NCURSES_COLOR_T bg_color_number; NCURSES_COLOR_T bg_color_number;
bg_color_number = change_color_definition(2, bg_color_string, predef_bg_color); bg_color_number = change_color_definition(2, bg_color_string, predef_bg_color);
// do not forget to increase MAX_COLOR_REDEFINITION if you change more color
// definitions
NCURSES_COLOR_T color_pair_number = 1; NCURSES_COLOR_T color_pair_number = 1;
init_pair(color_pair_number, fg_color_number, bg_color_number); init_pair(color_pair_number, fg_color_number, bg_color_number);
@ -65,7 +84,6 @@ char* const bg_color_string, int predef_fg_color, int predef_bg_color) {
refresh(); refresh();
} }
void get_terminal_dim_ncurses(int* width, int* height) { void get_terminal_dim_ncurses(int* width, int* height) {
getmaxyx(stdscr, *height, *width); getmaxyx(stdscr, *height, *width);
clear(); // clearing in case of resieze clear(); // clearing in case of resieze
@ -134,15 +152,20 @@ int flastd[200]) {
return 0; return 0;
} }
// general: cleanup // general: cleanup
void cleanup_terminal_ncurses(void) { void cleanup_terminal_ncurses(void) {
echo(); echo();
system("setfont >/dev/null 2>&1"); system("setfont >/dev/null 2>&1");
system("setfont /usr/share/consolefonts/Lat2-Fixed16.psf.gz >/dev/null 2>&1"); system("setfont /usr/share/consolefonts/Lat2-Fixed16.psf.gz >/dev/null 2>&1");
system("setterm -blank 10"); system("setterm -blank 10");
init_color(1, 1000, 0, 0); for(int i = 0; i < MAX_COLOR_REDEFINITION; ++i) {
init_color(2, 0, 1000, 0); if(the_color_redefinitions[i].color) {
init_color(the_color_redefinitions[i].color,
the_color_redefinitions[i].R,
the_color_redefinitions[i].G,
the_color_redefinitions[i].B);
}
}
endwin(); endwin();
system("clear"); system("clear");
} }

Loading…
Cancel
Save