>From 810ae5ba1e25677eedd36ea25e519581512e0591 Mon Sep 17 00:00:00 2001 From: Brand Huntsman Date: Wed, 7 Feb 2018 16:46:30 -0700 Subject: [PATCH 1/5] get number of terminal colors Signed-off-by: Brand Huntsman --- src/color.c | 32 ++++++++++++++++++++++++++++++++ src/global.c | 2 ++ src/nano.c | 4 ++++ src/proto.h | 2 ++ 4 files changed, 40 insertions(+) diff --git a/src/color.c b/src/color.c index 131f23d8..7d0c07be 100644 --- a/src/color.c +++ b/src/color.c @@ -27,6 +27,7 @@ #endif #include #include +#include #ifdef ENABLE_COLOR @@ -434,4 +435,35 @@ 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. */ +void extended_color_init(void) +{ + 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 6209d119..9238a73c 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 3a413fa6..8c028e8c 100644 --- a/src/nano.c +++ b/src/nano.c @@ -1996,6 +1996,10 @@ int main(int argc, char **argv) on_a_vt = (ioctl(0, VT_GETSTATE, &dummy) == 0); #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 26f7d6d8..e656e636 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.16.1