Fossil SCM

Add support for the FOSSIL_COLOR environment variable to define the color VT escape to highlight CLI text, also similar to `ls', `grep' and other utilities.

florian 2025-04-18 07:19 standard-cli-colors
Commit 50e0931bc711da4acd9e9aae1978eeef0c5bd003e98903fd30e705c3a7467f10
+18
--- src/main.c
+++ src/main.c
@@ -236,10 +236,11 @@
236236
int useLocalauth; /* No login required if from 127.0.0.1 */
237237
int noPswd; /* Logged in without password (on 127.0.0.1) */
238238
int userUid; /* Integer user id */
239239
int isHuman; /* True if access by a human, not a spider or bot */
240240
int colorOutput; /* Control output of color VT escapes to CLI */
241
+ const char *cliColor; /* VT color code for CLI highlight; default: "91" */
241242
int comFmtFlags; /* Zero or more "COMMENT_PRINT_*" bit flags, should be
242243
** accessed through get_comment_format(). */
243244
const char *zSockName; /* Name of the unix-domain socket file */
244245
const char *zSockMode; /* File permissions for unix-domain socket */
245246
const char *zSockOwner; /* Owner, or owner:group for unix-domain socket */
@@ -641,10 +642,11 @@
641642
blob_reset(&msg);
642643
}
643644
644645
/*
645646
** Initialize the g.comFmtFlags and g.colorOutput global variables.
647
+** Also read the environment variable FOSSIL_COLOR into g.cliColor.
646648
**
647649
** The global command-line options --comfmtflags or --comment-format to
648650
** set the comment format are undocumented and deprecated, and are only
649651
** for backwards compatibility.
650652
**
@@ -684,10 +686,26 @@
684686
g.colorOutput = COLOR_VT_NEVER;
685687
}
686688
fossil_path_free(zEnvVar);
687689
}
688690
}
691
+ g.cliColor = "\033[91m";
692
+ zEnvVar = fossil_getenv("FOSSIL_COLOR");
693
+ if( zEnvVar ){
694
+ if( fossil_strcmp(zEnvVar,"none")==0 &&
695
+ g.colorOutput==COLOR_VT_UNSET ){
696
+ g.colorOutput = COLOR_VT_NEVER;
697
+ }else{
698
+ int i, fValid = 1;
699
+ /* Rudimentary sanity check: only allow digits and semicolon. */
700
+ for( i=0; zEnvVar[i]; i++ ){
701
+ if( !strchr("0123456789;",zEnvVar[i]) ) fValid = 0;
702
+ }
703
+ if( fValid ) g.cliColor = mprintf("\033[%sm",zEnvVar);
704
+ }
705
+ fossil_path_free(zEnvVar);
706
+ }
689707
}
690708
691709
/*
692710
** Check to see if the Fossil binary contains an appended repository
693711
** file using the appendvfs extension. If so, change command-line arguments
694712
--- src/main.c
+++ src/main.c
@@ -236,10 +236,11 @@
236 int useLocalauth; /* No login required if from 127.0.0.1 */
237 int noPswd; /* Logged in without password (on 127.0.0.1) */
238 int userUid; /* Integer user id */
239 int isHuman; /* True if access by a human, not a spider or bot */
240 int colorOutput; /* Control output of color VT escapes to CLI */
 
241 int comFmtFlags; /* Zero or more "COMMENT_PRINT_*" bit flags, should be
242 ** accessed through get_comment_format(). */
243 const char *zSockName; /* Name of the unix-domain socket file */
244 const char *zSockMode; /* File permissions for unix-domain socket */
245 const char *zSockOwner; /* Owner, or owner:group for unix-domain socket */
@@ -641,10 +642,11 @@
641 blob_reset(&msg);
642 }
643
644 /*
645 ** Initialize the g.comFmtFlags and g.colorOutput global variables.
 
646 **
647 ** The global command-line options --comfmtflags or --comment-format to
648 ** set the comment format are undocumented and deprecated, and are only
649 ** for backwards compatibility.
650 **
@@ -684,10 +686,26 @@
684 g.colorOutput = COLOR_VT_NEVER;
685 }
686 fossil_path_free(zEnvVar);
687 }
688 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
689 }
690
691 /*
692 ** Check to see if the Fossil binary contains an appended repository
693 ** file using the appendvfs extension. If so, change command-line arguments
694
--- src/main.c
+++ src/main.c
@@ -236,10 +236,11 @@
236 int useLocalauth; /* No login required if from 127.0.0.1 */
237 int noPswd; /* Logged in without password (on 127.0.0.1) */
238 int userUid; /* Integer user id */
239 int isHuman; /* True if access by a human, not a spider or bot */
240 int colorOutput; /* Control output of color VT escapes to CLI */
241 const char *cliColor; /* VT color code for CLI highlight; default: "91" */
242 int comFmtFlags; /* Zero or more "COMMENT_PRINT_*" bit flags, should be
243 ** accessed through get_comment_format(). */
244 const char *zSockName; /* Name of the unix-domain socket file */
245 const char *zSockMode; /* File permissions for unix-domain socket */
246 const char *zSockOwner; /* Owner, or owner:group for unix-domain socket */
@@ -641,10 +642,11 @@
642 blob_reset(&msg);
643 }
644
645 /*
646 ** Initialize the g.comFmtFlags and g.colorOutput global variables.
647 ** Also read the environment variable FOSSIL_COLOR into g.cliColor.
648 **
649 ** The global command-line options --comfmtflags or --comment-format to
650 ** set the comment format are undocumented and deprecated, and are only
651 ** for backwards compatibility.
652 **
@@ -684,10 +686,26 @@
686 g.colorOutput = COLOR_VT_NEVER;
687 }
688 fossil_path_free(zEnvVar);
689 }
690 }
691 g.cliColor = "\033[91m";
692 zEnvVar = fossil_getenv("FOSSIL_COLOR");
693 if( zEnvVar ){
694 if( fossil_strcmp(zEnvVar,"none")==0 &&
695 g.colorOutput==COLOR_VT_UNSET ){
696 g.colorOutput = COLOR_VT_NEVER;
697 }else{
698 int i, fValid = 1;
699 /* Rudimentary sanity check: only allow digits and semicolon. */
700 for( i=0; zEnvVar[i]; i++ ){
701 if( !strchr("0123456789;",zEnvVar[i]) ) fValid = 0;
702 }
703 if( fValid ) g.cliColor = mprintf("\033[%sm",zEnvVar);
704 }
705 fossil_path_free(zEnvVar);
706 }
707 }
708
709 /*
710 ** Check to see if the Fossil binary contains an appended repository
711 ** file using the appendvfs extension. If so, change command-line arguments
712
--- src/wikiformat.c
+++ src/wikiformat.c
@@ -2532,11 +2532,11 @@
25322532
25332533
#if INTERFACE
25342534
/*
25352535
** Allowed flag options for html_to_plaintext().
25362536
*/
2537
-#define HTOT_VT100 0x01 /* <mark> becomes ^[[91m */
2537
+#define HTOT_VT100 0x01 /* <mark> becomes ^[[<g.cliColor>m */
25382538
#define HTOT_FLOW 0x02 /* Collapse internal whitespace to a single space */
25392539
#define HTOT_TRIM 0x04 /* Trim off leading and trailing whitespace */
25402540
25412541
#endif /* INTERFACE */
25422542
@@ -2543,11 +2543,11 @@
25432543
/*
25442544
** Add <MARK> or </MARK> to the output, or similar VT-100 escape
25452545
** codes.
25462546
*/
25472547
static void addMark(Blob *pOut, int mFlags, int isClose){
2548
- static const char *az[4] = { "<MARK>", "</MARK>", "\033[91m", "\033[0m" };
2548
+ const char *az[4] = { "<MARK>", "</MARK>", g.cliColor, "\033[0m" };
25492549
int i = 0;
25502550
if( isClose ) i++;
25512551
if( mFlags & HTOT_VT100 ) i += 2;
25522552
blob_append(pOut, az[i], -1);
25532553
}
25542554
--- src/wikiformat.c
+++ src/wikiformat.c
@@ -2532,11 +2532,11 @@
2532
2533 #if INTERFACE
2534 /*
2535 ** Allowed flag options for html_to_plaintext().
2536 */
2537 #define HTOT_VT100 0x01 /* <mark> becomes ^[[91m */
2538 #define HTOT_FLOW 0x02 /* Collapse internal whitespace to a single space */
2539 #define HTOT_TRIM 0x04 /* Trim off leading and trailing whitespace */
2540
2541 #endif /* INTERFACE */
2542
@@ -2543,11 +2543,11 @@
2543 /*
2544 ** Add <MARK> or </MARK> to the output, or similar VT-100 escape
2545 ** codes.
2546 */
2547 static void addMark(Blob *pOut, int mFlags, int isClose){
2548 static const char *az[4] = { "<MARK>", "</MARK>", "\033[91m", "\033[0m" };
2549 int i = 0;
2550 if( isClose ) i++;
2551 if( mFlags & HTOT_VT100 ) i += 2;
2552 blob_append(pOut, az[i], -1);
2553 }
2554
--- src/wikiformat.c
+++ src/wikiformat.c
@@ -2532,11 +2532,11 @@
2532
2533 #if INTERFACE
2534 /*
2535 ** Allowed flag options for html_to_plaintext().
2536 */
2537 #define HTOT_VT100 0x01 /* <mark> becomes ^[[<g.cliColor>m */
2538 #define HTOT_FLOW 0x02 /* Collapse internal whitespace to a single space */
2539 #define HTOT_TRIM 0x04 /* Trim off leading and trailing whitespace */
2540
2541 #endif /* INTERFACE */
2542
@@ -2543,11 +2543,11 @@
2543 /*
2544 ** Add <MARK> or </MARK> to the output, or similar VT-100 escape
2545 ** codes.
2546 */
2547 static void addMark(Blob *pOut, int mFlags, int isClose){
2548 const char *az[4] = { "<MARK>", "</MARK>", g.cliColor, "\033[0m" };
2549 int i = 0;
2550 if( isClose ) i++;
2551 if( mFlags & HTOT_VT100 ) i += 2;
2552 blob_append(pOut, az[i], -1);
2553 }
2554
--- www/env-opts.md
+++ www/env-opts.md
@@ -144,10 +144,15 @@
144144
environment variable.
145145
146146
`FOSSIL_BREAK`: If set, an opportunity will be created to attach a
147147
debugger to the Fossil process prior to any significant work being
148148
performed.
149
+
150
+`FOSSIL_COLOR`: Define the color VT escape to highlight CLI text. For
151
+example, if set to `1;4`, call-outs are bold and underlined. With the
152
+value `none`, colors are disabled, but note that the `--color=always`
153
+option takes precedence. The default is `91` (bright foreground red).
149154
150155
`FOSSIL_FORCE_TICKET_MODERATION`: If set, *ALL* changes for tickets
151156
will be required to go through moderation (even those performed by the
152157
local interactive user via the command line). This can be useful for
153158
local (or remote) testing of the moderation subsystem and its impact
154159
--- www/env-opts.md
+++ www/env-opts.md
@@ -144,10 +144,15 @@
144 environment variable.
145
146 `FOSSIL_BREAK`: If set, an opportunity will be created to attach a
147 debugger to the Fossil process prior to any significant work being
148 performed.
 
 
 
 
 
149
150 `FOSSIL_FORCE_TICKET_MODERATION`: If set, *ALL* changes for tickets
151 will be required to go through moderation (even those performed by the
152 local interactive user via the command line). This can be useful for
153 local (or remote) testing of the moderation subsystem and its impact
154
--- www/env-opts.md
+++ www/env-opts.md
@@ -144,10 +144,15 @@
144 environment variable.
145
146 `FOSSIL_BREAK`: If set, an opportunity will be created to attach a
147 debugger to the Fossil process prior to any significant work being
148 performed.
149
150 `FOSSIL_COLOR`: Define the color VT escape to highlight CLI text. For
151 example, if set to `1;4`, call-outs are bold and underlined. With the
152 value `none`, colors are disabled, but note that the `--color=always`
153 option takes precedence. The default is `91` (bright foreground red).
154
155 `FOSSIL_FORCE_TICKET_MODERATION`: If set, *ALL* changes for tickets
156 will be required to go through moderation (even those performed by the
157 local interactive user via the command line). This can be useful for
158 local (or remote) testing of the moderation subsystem and its impact
159

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button