chiark / gitweb /
@@@ tty mess
[mLib] / ui / ttycolour.h
index 4fb8472cbc54ef9f2c8ec3a8e61e3a39425265b1..535248f560a28f52d00d856cd917e04227b691b4 100644 (file)
 #  include "macros.h"
 #endif
 
-/*----- Functions provided ------------------------------------------------*/
+#ifndef MLIB_TTY_H
+#  include "tty.h"
+#endif
 
-/* Attributes for colour output.
- *
- * An attribute word holds a foreground colour in the low nibble, a
- * background colour in the next nibble, and some flags in the next few bits.
- * A colour is expressed in classic 1-bit-per-channel style, with red, green,
- * and blue in bits 0, 1, and 2, and a `bright' flag in bit 3.
- */
-typedef unsigned short ttycolour_attr; /* type of an attribute */
-
-#define TCAF_FGMASK 0x0f               /* foreground colour mask */
-#define TCAF_FGSHIFT 0                 /* foreground colour shift */
-#define TCAF_BGMASK 0xf0               /* background colour mask */
-#define TCAF_BGSHIFT 4                 /* background colour shift */
-#define TCAF_FG 256u                   /* set foreground? */
-#define TCAF_BG 512u                   /* set background? */
-#define TCAF_BOLD 1024u                        /* set bold? */
-
-#define TCCF_RED 1u                    /* red channel */
-#define TCCF_GREEN 2u                  /* green channel */
-#define TCCF_BLUE 4u                   /* blue channel */
-#define TCCF_RGBMASK (TCCF_RED | TCCF_GREEN | TCCF_BLUE)
-#define TCCF_BRIGHT 8u                 /* bright colour flag */
-
-#define TTYCOL_BLACK 0u                        /* colour codes... */
-#define TTYCOL_RED (TCCF_RED)
-#define TTYCOL_GREEN (TCCF_GREEN)
-#define TTYCOL_YELLOW (TCCF_RED | TCCF_GREEN)
-#define TTYCOL_BLUE (TCCF_BLUE)
-#define TTYCOL_MAGENTA (TCCF_RED | TCCF_BLUE)
-#define TTYCOL_CYAN (TCCF_GREEN | TCCF_BLUE)
-#define TTYCOL_WHITE (TCCF_RED | TCCF_GREEN | TCCF_BLUE)
-
-#define TTYCOL_BRBLACK (TTYCOL_BLACK | TCCF_BRIGHT)
-#define TTYCOL_BRRED (TTYCOL_RED | TCCF_BRIGHT)
-#define TTYCOL_BRGREEN (TTYCOL_GREEN | TCCF_BRIGHT)
-#define TTYCOL_BRYELLOW (TTYCOL_YELLOW | TCCF_BRIGHT)
-#define TTYCOL_BRBLUE (TTYCOL_BLUE | TCCF_BRIGHT)
-#define TTYCOL_BRMAGENTA (TTYCOL_MAGENTA | TCCF_BRIGHT)
-#define TTYCOL_BRCYAN (TTYCOL_CYAN | TCCF_BRIGHT)
-#define TTYCOL_BRWHITE (TTYCOL_WHITE | TCCF_BRIGHT)
-
-#define TC_FG(col) (TCAF_FG | (TTYCOL_##col) << TCAF_FGSHIFT) /* set fg */
-#define TC_BG(col) (TCAF_BG | (TTYCOL_##col) << TCAF_BGSHIFT) /* set bg */
+/*----- Functions provided ------------------------------------------------*/
 
 struct ttycolour_style {
   /* Table of style tokens and their defaults.  The table is terminated with
@@ -94,16 +54,9 @@ struct ttycolour_style {
    */
 
   const char *tok;                     /* style token name */
-  ttycolour_attr dflt;                 /* default attribute value */
+  const struct tty_attrlist *dflt;     /* default attribute value */
 };
 
-struct ttycolour_state {
-  /* State maintained by @ttycolour_setattr@. */
-
-  ttycolour_attr attr;                 /* current attribute value */
-};
-#define TTYCOLOUR_STATE_INIT { 0 }     /* initializer for state */
-
 /* Machinery for building the constants and tables.
  *
  * The caller should define the styles it supports in a macro taking two
@@ -150,6 +103,55 @@ struct ttycolour_state {
   { 0, 0 }                                                             \
 }
 
+/* --- @ttycolour_enablep@ --- *
+ *
+ * Arguments:  @unsigned f@ = flags
+ *
+ * Returns:    Nonzero if colours should be applied to output, otherwise
+ *             zero.
+ *
+ * Use:                This function determines whether it's generally a good idea
+ *             to produce output in pretty colours.  Set @TCEF_TTY@ if the
+ *             output stream is -- or should be considered to be --
+ *             interactive (e.g., according to @isatty@); set @TCEF_DFLT@ if
+ *             the application prefers to produce coloured output if
+ *             possible.
+ *
+ *             The detailed behaviour is as follows.  (Since the purpose of
+ *             this function is to abide by common conventions and to be
+ *             convenient for users, these details may change in future.)
+ *
+ *               * If the `NO_COLOR' environment variable is non-empty, then
+ *                 colour is disabled (%%\url{https://no-color.org/}%%).
+ *
+ *               * If the `TERM' variable is set to `dumb', then colour is
+ *                 disabled (Emacs).
+ *
+ *               * If the `FORCE_COLOR' environment variable is non-empty,
+ *                 then colour is enabled, unless the value is 0, in which
+ *                 case colour is disabled (apparently from the Node
+ *                 community, %%\url{%%https://force-color.org/}%% and
+ *                 %%\url{https://nodejs.org/api/tty.html#writestreamgetcolordepthenv}%%).
+ *
+ *               * If the `CLICOLOR_FORCE' environment variable is
+ *                 non-empty, then colour is enabled (apparently from
+ *                 Mac OS, (%%\url{http://bixense.com/clicolors/}%%).
+ *
+ *               * If the @TCEF_TTY@ flag is clear, then colour is disabled.
+ *
+ *               * If the @TCEF_DFLT@ flag is set, then colour is enabled.
+ *
+ *               * If the `CLICOLOR' environment variable is non-empty, then
+ *                 colour is enabled (again, apparently from Mac OS,
+ *                 (%%\url{http://bixense.com/clicolors/}%%).
+ *
+ *               * Otherwise, colour is disabled.
+ */
+
+#define TCEF_TTY 1u                    /*   output is interactive */
+#define TCEF_DFLT 2u                   /*   turn on by default */
+extern int ttycolour_enablep(unsigned /*f*/);
+
 /* --- @ttycolour_config@ --- *
  *
  * Arguments:  @ttycolour_attr *attr@ = pointer to attribute table
@@ -174,16 +176,11 @@ struct ttycolour_state {
 
 #define TCIF_GETENV 1u
 #define TCIF_REPORT 2u
-extern int ttycolour_config(ttycolour_attr */*attr*/,
+extern int ttycolour_config(struct tty_attr */*attr*/,
                            const char */*user*/, unsigned /*f*/,
+                           struct tty */*tty*/,
                            const struct ttycolour_style */*tab*/);
 
-extern void ttycolour_init(struct ttycolour_state */*tc*/);
-
-extern int ttycolour_setattr
-  (const struct gprintf_ops */*gops*/, void */*go*/,
-   struct ttycolour_state */*tc*/, ttycolour_attr /*attr*/);
-
 /*----- That's all, folks -------------------------------------------------*/
 
 #ifdef __cplusplus