>From ba267757d0dd8bcae9bfa9afd789700645eee9b2 Mon Sep 17 00:00:00 2001 From: Brand Huntsman Date: Mon, 29 Jan 2018 16:51:47 -0700 Subject: [PATCH 1/2] get number of terminal colors Signed-off-by: Brand Huntsman --- src/color.c | 34 ++++++++++++++++++++++++++++++++++ src/global.c | 2 ++ src/nano.c | 4 ++++ src/proto.h | 2 ++ 4 files changed, 42 insertions(+) diff --git a/src/color.c b/src/color.c index 329ed41d..108af9f8 100644 --- a/src/color.c +++ b/src/color.c @@ -27,6 +27,7 @@ #endif #include #include +#include #ifdef ENABLE_COLOR @@ -431,4 +432,37 @@ void precalc_multicolorinfo(void) } } +#if defined(NCURSES_VERSION_MAJOR) && (NCURSES_VERSION_MAJOR >= 6) +#define HAS_EXTENDED_COLORS 1 +#else +#define HAS_EXTENDED_COLORS 0 +#endif + +/* Get number of colors supported by terminal and initialize lookup tables. */ +void extended_color_init(void) +{ + if (!has_colors()) + nr_term_colors = 0; + + if (tgetent(NULL, getenv("TERM")) != 1) { + nr_term_colors = 0; + return; + } + nr_term_colors = tgetnum("Co"); + + if (nr_term_colors > 16 && !HAS_EXTENDED_COLORS) { + /* No support for extended colors. */ + nr_term_colors = 16; + } else if (nr_term_colors > 256) { + nr_term_colors = 256; + } else if (nr_term_colors > 16) { + if (nr_term_colors != 256 && nr_term_colors != 88) + nr_term_colors = 16; + } else if (nr_term_colors > 8) { + if (nr_term_colors != 16) + nr_term_colors = 8; + } else if (nr_term_colors != 8) + nr_term_colors = 0; +} + #endif /* ENABLE_COLOR */ diff --git a/src/global.c b/src/global.c index fadd6fdc..afe565d2 100644 --- a/src/global.c +++ b/src/global.c @@ -183,6 +183,8 @@ char *syntaxstr = NULL; /* The color syntax name specified on the command line. */ bool have_palette = FALSE; /* Whether the colors for the current syntax have been initialized. */ +int nr_term_colors = 0; + /* Number of colors supported by terminal. */ #endif bool refresh_needed = FALSE; diff --git a/src/nano.c b/src/nano.c index 12543dd4..ab92726f 100644 --- a/src/nano.c +++ b/src/nano.c @@ -1992,6 +1992,10 @@ int main(int argc, char **argv) console = (getenv("DISPLAY") == NULL); #endif +#ifdef ENABLE_COLOR + extended_color_init(); +#endif + /* Back up the terminal settings so that they can be restored. */ tcgetattr(0, &oldterm); diff --git a/src/proto.h b/src/proto.h index a7d90689..c575a7a1 100644 --- a/src/proto.h +++ b/src/proto.h @@ -144,6 +144,7 @@ extern char *alt_speller; extern syntaxtype *syntaxes; extern char *syntaxstr; extern bool have_palette; +extern int nr_term_colors; #endif extern bool refresh_needed; @@ -248,6 +249,7 @@ void color_update(void); void check_the_multis(filestruct *line); void alloc_multidata_if_needed(filestruct *fileptr); void precalc_multicolorinfo(void); +void extended_color_init(void); #endif /* Most functions in cut.c. */ -- 2.13.6