Fossil SCM

Add the global --color option to control output of color VT escapes to CLI, similar to `ls', `grep' and other utilities. Useful when piping `fossil search' results through a pager utility.

florian 2025-04-18 07:16 trunk
Commit 210b7d2fe0bd4ae9dd3da6607bb7e963cd1b156ecb2d9d983fbcb44c63f1e299
--- src/dispatch.c
+++ src/dispatch.c
@@ -1388,10 +1388,11 @@
13881388
@
13891389
@ --args FILENAME Read additional arguments and options from FILENAME
13901390
@ --case-sensitive BOOL Set case sensitivity for file names
13911391
@ --cgitrace Active CGI tracing
13921392
@ --chdir PATH Change to PATH before performing any operations
1393
+@ --color WHEN Emit VT color escapes: 'never', 'always', or 'auto'
13931394
@ --errorlog FILENAME Log errors to FILENAME
13941395
@ --help Show help on the command rather than running it
13951396
@ --httptrace Trace outbound HTTP requests
13961397
@ --localtime Display times using the local timezone
13971398
@ --nocgi Do not act as CGI
13981399
--- src/dispatch.c
+++ src/dispatch.c
@@ -1388,10 +1388,11 @@
1388 @
1389 @ --args FILENAME Read additional arguments and options from FILENAME
1390 @ --case-sensitive BOOL Set case sensitivity for file names
1391 @ --cgitrace Active CGI tracing
1392 @ --chdir PATH Change to PATH before performing any operations
 
1393 @ --errorlog FILENAME Log errors to FILENAME
1394 @ --help Show help on the command rather than running it
1395 @ --httptrace Trace outbound HTTP requests
1396 @ --localtime Display times using the local timezone
1397 @ --nocgi Do not act as CGI
1398
--- src/dispatch.c
+++ src/dispatch.c
@@ -1388,10 +1388,11 @@
1388 @
1389 @ --args FILENAME Read additional arguments and options from FILENAME
1390 @ --case-sensitive BOOL Set case sensitivity for file names
1391 @ --cgitrace Active CGI tracing
1392 @ --chdir PATH Change to PATH before performing any operations
1393 @ --color WHEN Emit VT color escapes: 'never', 'always', or 'auto'
1394 @ --errorlog FILENAME Log errors to FILENAME
1395 @ --help Show help on the command rather than running it
1396 @ --httptrace Trace outbound HTTP requests
1397 @ --localtime Display times using the local timezone
1398 @ --nocgi Do not act as CGI
1399
+33 -4
--- src/main.c
+++ src/main.c
@@ -235,10 +235,11 @@
235235
#endif
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 */
240
+ int colorOutput; /* Control output of color VT escapes to CLI */
240241
int comFmtFlags; /* Zero or more "COMMENT_PRINT_*" bit flags, should be
241242
** accessed through get_comment_format(). */
242243
const char *zSockName; /* Name of the unix-domain socket file */
243244
const char *zSockMode; /* File permissions for unix-domain socket */
244245
const char *zSockOwner; /* Owner, or owner:group for unix-domain socket */
@@ -639,25 +640,53 @@
639640
fossil_warning("%s", blob_str(&msg));
640641
blob_reset(&msg);
641642
}
642643
643644
/*
644
-** Initialize the g.comFmtFlags global variable.
645
+** Initialize the g.comFmtFlags and g.colorOutput global variables.
645646
**
646
-** Global command-line options --comfmtflags or --comment-format can be
647
-** used for this. However, those command-line options are undocumented
648
-** and deprecated. They are here for backwards compatibility only.
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
+**
651
+** If the --color option isn't found, the NO_COLOR environment variable
652
+** may disable colored output (but is otherwise trumped by the option).
649653
*/
650654
static void fossil_init_flags_from_options(void){
655
+ char *zEnvVar;
651656
const char *zValue = find_option("comfmtflags", 0, 1);
652657
if( zValue==0 ){
653658
zValue = find_option("comment-format", 0, 1);
654659
}
655660
if( zValue ){
656661
g.comFmtFlags = atoi(zValue);
657662
}else{
658663
g.comFmtFlags = COMMENT_PRINT_UNSET; /* Command-line option not found. */
664
+ }
665
+ g.colorOutput = COLOR_VT_UNSET;
666
+ zValue = find_option("color", 0, 1);
667
+ if( zValue ){
668
+ if( fossil_strcmp(zValue,"never")==0 ){
669
+ g.colorOutput = COLOR_VT_NEVER;
670
+ }
671
+ else if( fossil_strcmp(zValue,"always")==0 ){
672
+ g.colorOutput = COLOR_VT_ALWAYS;
673
+ }
674
+ else if( fossil_strcmp(zValue,"auto")==0 ){
675
+ g.colorOutput = COLOR_VT_AUTO;
676
+ }else{
677
+ fossil_fatal("the --color option must be 'never', 'always', or 'auto'");
678
+ }
679
+ }
680
+ if( g.colorOutput==COLOR_VT_UNSET ){
681
+ zEnvVar = fossil_getenv("NO_COLOR");
682
+ if( zEnvVar ){
683
+ if( is_false(zEnvVar) ){
684
+ g.colorOutput = COLOR_VT_NEVER;
685
+ }
686
+ fossil_path_free(zEnvVar);
687
+ }
659688
}
660689
}
661690
662691
/*
663692
** Check to see if the Fossil binary contains an appended repository
664693
--- src/main.c
+++ src/main.c
@@ -235,10 +235,11 @@
235 #endif
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 comFmtFlags; /* Zero or more "COMMENT_PRINT_*" bit flags, should be
241 ** accessed through get_comment_format(). */
242 const char *zSockName; /* Name of the unix-domain socket file */
243 const char *zSockMode; /* File permissions for unix-domain socket */
244 const char *zSockOwner; /* Owner, or owner:group for unix-domain socket */
@@ -639,25 +640,53 @@
639 fossil_warning("%s", blob_str(&msg));
640 blob_reset(&msg);
641 }
642
643 /*
644 ** Initialize the g.comFmtFlags global variable.
645 **
646 ** Global command-line options --comfmtflags or --comment-format can be
647 ** used for this. However, those command-line options are undocumented
648 ** and deprecated. They are here for backwards compatibility only.
 
 
 
649 */
650 static void fossil_init_flags_from_options(void){
 
651 const char *zValue = find_option("comfmtflags", 0, 1);
652 if( zValue==0 ){
653 zValue = find_option("comment-format", 0, 1);
654 }
655 if( zValue ){
656 g.comFmtFlags = atoi(zValue);
657 }else{
658 g.comFmtFlags = COMMENT_PRINT_UNSET; /* Command-line option not found. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
659 }
660 }
661
662 /*
663 ** Check to see if the Fossil binary contains an appended repository
664
--- src/main.c
+++ src/main.c
@@ -235,10 +235,11 @@
235 #endif
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 */
@@ -639,25 +640,53 @@
640 fossil_warning("%s", blob_str(&msg));
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 **
651 ** If the --color option isn't found, the NO_COLOR environment variable
652 ** may disable colored output (but is otherwise trumped by the option).
653 */
654 static void fossil_init_flags_from_options(void){
655 char *zEnvVar;
656 const char *zValue = find_option("comfmtflags", 0, 1);
657 if( zValue==0 ){
658 zValue = find_option("comment-format", 0, 1);
659 }
660 if( zValue ){
661 g.comFmtFlags = atoi(zValue);
662 }else{
663 g.comFmtFlags = COMMENT_PRINT_UNSET; /* Command-line option not found. */
664 }
665 g.colorOutput = COLOR_VT_UNSET;
666 zValue = find_option("color", 0, 1);
667 if( zValue ){
668 if( fossil_strcmp(zValue,"never")==0 ){
669 g.colorOutput = COLOR_VT_NEVER;
670 }
671 else if( fossil_strcmp(zValue,"always")==0 ){
672 g.colorOutput = COLOR_VT_ALWAYS;
673 }
674 else if( fossil_strcmp(zValue,"auto")==0 ){
675 g.colorOutput = COLOR_VT_AUTO;
676 }else{
677 fossil_fatal("the --color option must be 'never', 'always', or 'auto'");
678 }
679 }
680 if( g.colorOutput==COLOR_VT_UNSET ){
681 zEnvVar = fossil_getenv("NO_COLOR");
682 if( zEnvVar ){
683 if( is_false(zEnvVar) ){
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
+15 -8
--- src/terminal.c
+++ src/terminal.c
@@ -40,10 +40,18 @@
4040
*/
4141
struct TerminalSize {
4242
unsigned int nColumns; /* Number of characters on a single line */
4343
unsigned int nLines; /* Number of lines */
4444
};
45
+
46
+/*
47
+** Values of g.ColorOutput to control output of color VT escapes to CLI.
48
+*/
49
+#define COLOR_VT_UNSET (-1) /* Not initialized. */
50
+#define COLOR_VT_NEVER (0) /* Never emit color VT escapes. */
51
+#define COLOR_VT_ALWAYS (1) /* Always emit color VT escapes. */
52
+#define COLOR_VT_AUTO (2) /* Only emit color VT escapes on terminal. */
4553
#endif
4654
4755
4856
/* Get the current terminal size by calling a system service.
4957
**
@@ -143,19 +151,18 @@
143151
144152
/*
145153
** Return true if it is reasonable is emit VT100 escape codes.
146154
*/
147155
int terminal_is_vt100(void){
148
- char *zNoColor;
156
+ if( g.colorOutput==COLOR_VT_NEVER) return 0;
157
+ if( g.colorOutput==COLOR_VT_ALWAYS) return 1;
158
+ /* Check the terminal if g.colorOutput is COLOR_VT_UNSET or COLOR_VT_AUTO. */
149159
#ifdef _WIN32
150
- if( !win32_terminal_is_vt100(1) ) return 0;
151
-#endif /* _WIN32 */
152
- if( !fossil_isatty(1) ) return 0;
153
- zNoColor =fossil_getenv("NO_COLOR");
154
- if( zNoColor==0 ) return 1;
155
- if( zNoColor[0]==0 ) return 1;
156
- if( is_false(zNoColor) ) return 1;
160
+ return win32_terminal_is_vt100(1);
161
+#else /* !_WIN32 */
162
+ return fossil_isatty(1);
163
+#endif /* !_WIN32 */
157164
return 0;
158165
}
159166
160167
#ifdef _WIN32
161168
/*
162169
--- src/terminal.c
+++ src/terminal.c
@@ -40,10 +40,18 @@
40 */
41 struct TerminalSize {
42 unsigned int nColumns; /* Number of characters on a single line */
43 unsigned int nLines; /* Number of lines */
44 };
 
 
 
 
 
 
 
 
45 #endif
46
47
48 /* Get the current terminal size by calling a system service.
49 **
@@ -143,19 +151,18 @@
143
144 /*
145 ** Return true if it is reasonable is emit VT100 escape codes.
146 */
147 int terminal_is_vt100(void){
148 char *zNoColor;
 
 
149 #ifdef _WIN32
150 if( !win32_terminal_is_vt100(1) ) return 0;
151 #endif /* _WIN32 */
152 if( !fossil_isatty(1) ) return 0;
153 zNoColor =fossil_getenv("NO_COLOR");
154 if( zNoColor==0 ) return 1;
155 if( zNoColor[0]==0 ) return 1;
156 if( is_false(zNoColor) ) return 1;
157 return 0;
158 }
159
160 #ifdef _WIN32
161 /*
162
--- src/terminal.c
+++ src/terminal.c
@@ -40,10 +40,18 @@
40 */
41 struct TerminalSize {
42 unsigned int nColumns; /* Number of characters on a single line */
43 unsigned int nLines; /* Number of lines */
44 };
45
46 /*
47 ** Values of g.ColorOutput to control output of color VT escapes to CLI.
48 */
49 #define COLOR_VT_UNSET (-1) /* Not initialized. */
50 #define COLOR_VT_NEVER (0) /* Never emit color VT escapes. */
51 #define COLOR_VT_ALWAYS (1) /* Always emit color VT escapes. */
52 #define COLOR_VT_AUTO (2) /* Only emit color VT escapes on terminal. */
53 #endif
54
55
56 /* Get the current terminal size by calling a system service.
57 **
@@ -143,19 +151,18 @@
151
152 /*
153 ** Return true if it is reasonable is emit VT100 escape codes.
154 */
155 int terminal_is_vt100(void){
156 if( g.colorOutput==COLOR_VT_NEVER) return 0;
157 if( g.colorOutput==COLOR_VT_ALWAYS) return 1;
158 /* Check the terminal if g.colorOutput is COLOR_VT_UNSET or COLOR_VT_AUTO. */
159 #ifdef _WIN32
160 return win32_terminal_is_vt100(1);
161 #else /* !_WIN32 */
162 return fossil_isatty(1);
163 #endif /* !_WIN32 */
 
 
 
164 return 0;
165 }
166
167 #ifdef _WIN32
168 /*
169
--- www/env-opts.md
+++ www/env-opts.md
@@ -32,10 +32,23 @@
3232
`--cgitrace`: Active CGI tracing.
3333
3434
`--chdir DIRECTORY`: Change to the named directory before processing
3535
any commands.
3636
37
+
38
+`--color WHEN`: Control output of color VT escapes to CLI (supersedes
39
+the `NO_COLOR` environment variable explained later in this document):
40
+
41
+ * _never_ — Never emit color VT escapes.
42
+
43
+ * _always_ — Always emit color VT escapes; useful when piping
44
+ Fossil command output through a pager utility.
45
+
46
+ * _auto_ — Only emit color VT escapes when output goes to a
47
+ terminal, and not if output is redirected to a pipe, a file, or any
48
+ other device.
49
+
3750
3851
`--comfmtflags NUMBER`: Specify flags that control how check-in comments
3952
and certain other text outputs are formatted for display. The flags are
4053
individual bits in `NUMBER`, which must be specified in base 10:
4154
@@ -230,10 +243,11 @@
230243
the discussion of Fossil Username below for a lot more detail.
231244
232245
`NO_COLOR`: If defined and not set to a `false` value (i.e. "off", "no",
233246
"false", "0"), the `fossil search` command skips colorization of console
234247
output using ANSI escape codes (VT100).
248
+Note the `--color` option takes precedence over the `NO_COLOR` variable.
235249
236250
`PATH`: Used by most platforms to locate programs invoked without a
237251
fully qualified name. Explicitly used by `fossil ui` on certain platforms
238252
to choose the browser to launch.
239253
240254
--- www/env-opts.md
+++ www/env-opts.md
@@ -32,10 +32,23 @@
32 `--cgitrace`: Active CGI tracing.
33
34 `--chdir DIRECTORY`: Change to the named directory before processing
35 any commands.
36
 
 
 
 
 
 
 
 
 
 
 
 
 
37
38 `--comfmtflags NUMBER`: Specify flags that control how check-in comments
39 and certain other text outputs are formatted for display. The flags are
40 individual bits in `NUMBER`, which must be specified in base 10:
41
@@ -230,10 +243,11 @@
230 the discussion of Fossil Username below for a lot more detail.
231
232 `NO_COLOR`: If defined and not set to a `false` value (i.e. "off", "no",
233 "false", "0"), the `fossil search` command skips colorization of console
234 output using ANSI escape codes (VT100).
 
235
236 `PATH`: Used by most platforms to locate programs invoked without a
237 fully qualified name. Explicitly used by `fossil ui` on certain platforms
238 to choose the browser to launch.
239
240
--- www/env-opts.md
+++ www/env-opts.md
@@ -32,10 +32,23 @@
32 `--cgitrace`: Active CGI tracing.
33
34 `--chdir DIRECTORY`: Change to the named directory before processing
35 any commands.
36
37
38 `--color WHEN`: Control output of color VT escapes to CLI (supersedes
39 the `NO_COLOR` environment variable explained later in this document):
40
41 * _never_ — Never emit color VT escapes.
42
43 * _always_ — Always emit color VT escapes; useful when piping
44 Fossil command output through a pager utility.
45
46 * _auto_ — Only emit color VT escapes when output goes to a
47 terminal, and not if output is redirected to a pipe, a file, or any
48 other device.
49
50
51 `--comfmtflags NUMBER`: Specify flags that control how check-in comments
52 and certain other text outputs are formatted for display. The flags are
53 individual bits in `NUMBER`, which must be specified in base 10:
54
@@ -230,10 +243,11 @@
243 the discussion of Fossil Username below for a lot more detail.
244
245 `NO_COLOR`: If defined and not set to a `false` value (i.e. "off", "no",
246 "false", "0"), the `fossil search` command skips colorization of console
247 output using ANSI escape codes (VT100).
248 Note the `--color` option takes precedence over the `NO_COLOR` variable.
249
250 `PATH`: Used by most platforms to locate programs invoked without a
251 fully qualified name. Explicitly used by `fossil ui` on certain platforms
252 to choose the browser to launch.
253
254

Keyboard Shortcuts

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