nano-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Nano-devel] [PATCH 1/2] tweaks: determine the number of colors that the


From: Benno Schulenberg
Subject: [Nano-devel] [PATCH 1/2] tweaks: determine the number of colors that the terminal supports
Date: Fri, 9 Feb 2018 11:31:56 +0100

From: Brand Huntsman <address@hidden>

Signed-off-by: Brand Huntsman <address@hidden>
---
 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 329ed41d..aaac83db 100644
--- a/src/color.c
+++ b/src/color.c
@@ -27,6 +27,7 @@
 #endif
 #include <string.h>
 #include <unistd.h>
+#include <term.h>
 
 #ifdef ENABLE_COLOR
 
@@ -431,4 +432,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 b13a65ac..fb2adb98 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 ca0c5657..548371eb 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -1999,6 +1999,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.14.3




reply via email to

[Prev in Thread] Current Thread [Next in Thread]