Fossil SCM

Change the `fossil system ls' command to work with the global `--color' option. Like this, colored output on Windows for `--color=auto' (the default if the option is omitted) is only enabled if the terminal supports VT100 escape codes. Additionally, the `NO_COLOR' environment variable allows turning off all colored output.

florian 2025-11-26 14:16 standard-cli-colors
Commit 07570b15b77b3cbf06f0161e9bbdeda3bdf066b295737fe005b9b5eb75cbed45
1 file changed +14 -28
+14 -28
--- src/xsystem.c
+++ src/xsystem.c
@@ -356,14 +356,13 @@
356356
*/
357357
void xsystem_ls(int argc, char **argv){
358358
int i, rc;
359359
sqlite3 *db;
360360
sqlite3_stmt *pStmt = 0;
361
- int mFlags = 0;
361
+ int mFlags = terminal_is_vt100() ? LS_COLOR : 0;
362362
int nFile = 0;
363363
int nDir = 0;
364
- int bAutoColor = 1;
365364
int needBlankLine = 0;
366365
rc = sqlite3_open(":memory:", &db);
367366
if( rc || db==0 ){
368367
fossil_fatal("Cannot open in-memory database");
369368
}
@@ -374,35 +373,23 @@
374373
fossil_fatal("Cannot prepare INSERT statement");
375374
}
376375
for(i=1; i<argc; i++){
377376
const char *z = argv[i];
378377
if( z[0]=='-' ){
379
- if( z[1]=='-' ){
380
- if( strncmp(z,"--color",7)==0 ){
381
- if( z[7]==0 || strcmp(&z[7],"=always")==0 ){
382
- mFlags |= LS_COLOR;
383
- }else if( strcmp(&z[7],"=never")==0 ){
384
- bAutoColor = 0;
385
- }
386
- }else{
387
- fossil_fatal("unknown option: %s", z);
388
- }
389
- }else{
390
- int k;
391
- for(k=1; z[k]; k++){
392
- switch( z[k] ){
393
- case 'a': mFlags |= LS_ALL; break;
394
- case 'd': mFlags |= LS_DIRONLY; break;
395
- case 'l': mFlags |= LS_LONG; break;
396
- case 'm': mFlags |= LS_COMMA; break;
397
- case 'r': mFlags |= LS_REVERSE; break;
398
- case 'S': mFlags |= LS_SIZE; break;
399
- case 't': mFlags |= LS_MTIME; break;
400
- case 'C': mFlags |= LS_COLUMNS; break;
401
- default: {
402
- fossil_fatal("unknown option: -%c", z[k]);
403
- }
378
+ int k;
379
+ for(k=1; z[k]; k++){
380
+ switch( z[k] ){
381
+ case 'a': mFlags |= LS_ALL; break;
382
+ case 'd': mFlags |= LS_DIRONLY; break;
383
+ case 'l': mFlags |= LS_LONG; break;
384
+ case 'm': mFlags |= LS_COMMA; break;
385
+ case 'r': mFlags |= LS_REVERSE; break;
386
+ case 'S': mFlags |= LS_SIZE; break;
387
+ case 't': mFlags |= LS_MTIME; break;
388
+ case 'C': mFlags |= LS_COLUMNS; break;
389
+ default: {
390
+ fossil_fatal("unknown option: -%c", z[k]);
404391
}
405392
}
406393
}
407394
}else{
408395
if( (mFlags & LS_DIRONLY)==0 && file_isdir(z, ExtFILE)==1 ){
@@ -412,11 +399,10 @@
412399
xsystem_ls_insert(pStmt, z, mFlags);
413400
}
414401
}
415402
}
416403
if( fossil_isatty(1) ){
417
- if( bAutoColor ) mFlags |= LS_COLOR;
418404
mFlags |= LS_COLUMNS;
419405
}
420406
if( nFile>0 ){
421407
xsystem_ls_render(db, mFlags);
422408
needBlankLine = 1;
423409
--- src/xsystem.c
+++ src/xsystem.c
@@ -356,14 +356,13 @@
356 */
357 void xsystem_ls(int argc, char **argv){
358 int i, rc;
359 sqlite3 *db;
360 sqlite3_stmt *pStmt = 0;
361 int mFlags = 0;
362 int nFile = 0;
363 int nDir = 0;
364 int bAutoColor = 1;
365 int needBlankLine = 0;
366 rc = sqlite3_open(":memory:", &db);
367 if( rc || db==0 ){
368 fossil_fatal("Cannot open in-memory database");
369 }
@@ -374,35 +373,23 @@
374 fossil_fatal("Cannot prepare INSERT statement");
375 }
376 for(i=1; i<argc; i++){
377 const char *z = argv[i];
378 if( z[0]=='-' ){
379 if( z[1]=='-' ){
380 if( strncmp(z,"--color",7)==0 ){
381 if( z[7]==0 || strcmp(&z[7],"=always")==0 ){
382 mFlags |= LS_COLOR;
383 }else if( strcmp(&z[7],"=never")==0 ){
384 bAutoColor = 0;
385 }
386 }else{
387 fossil_fatal("unknown option: %s", z);
388 }
389 }else{
390 int k;
391 for(k=1; z[k]; k++){
392 switch( z[k] ){
393 case 'a': mFlags |= LS_ALL; break;
394 case 'd': mFlags |= LS_DIRONLY; break;
395 case 'l': mFlags |= LS_LONG; break;
396 case 'm': mFlags |= LS_COMMA; break;
397 case 'r': mFlags |= LS_REVERSE; break;
398 case 'S': mFlags |= LS_SIZE; break;
399 case 't': mFlags |= LS_MTIME; break;
400 case 'C': mFlags |= LS_COLUMNS; break;
401 default: {
402 fossil_fatal("unknown option: -%c", z[k]);
403 }
404 }
405 }
406 }
407 }else{
408 if( (mFlags & LS_DIRONLY)==0 && file_isdir(z, ExtFILE)==1 ){
@@ -412,11 +399,10 @@
412 xsystem_ls_insert(pStmt, z, mFlags);
413 }
414 }
415 }
416 if( fossil_isatty(1) ){
417 if( bAutoColor ) mFlags |= LS_COLOR;
418 mFlags |= LS_COLUMNS;
419 }
420 if( nFile>0 ){
421 xsystem_ls_render(db, mFlags);
422 needBlankLine = 1;
423
--- src/xsystem.c
+++ src/xsystem.c
@@ -356,14 +356,13 @@
356 */
357 void xsystem_ls(int argc, char **argv){
358 int i, rc;
359 sqlite3 *db;
360 sqlite3_stmt *pStmt = 0;
361 int mFlags = terminal_is_vt100() ? LS_COLOR : 0;
362 int nFile = 0;
363 int nDir = 0;
 
364 int needBlankLine = 0;
365 rc = sqlite3_open(":memory:", &db);
366 if( rc || db==0 ){
367 fossil_fatal("Cannot open in-memory database");
368 }
@@ -374,35 +373,23 @@
373 fossil_fatal("Cannot prepare INSERT statement");
374 }
375 for(i=1; i<argc; i++){
376 const char *z = argv[i];
377 if( z[0]=='-' ){
378 int k;
379 for(k=1; z[k]; k++){
380 switch( z[k] ){
381 case 'a': mFlags |= LS_ALL; break;
382 case 'd': mFlags |= LS_DIRONLY; break;
383 case 'l': mFlags |= LS_LONG; break;
384 case 'm': mFlags |= LS_COMMA; break;
385 case 'r': mFlags |= LS_REVERSE; break;
386 case 'S': mFlags |= LS_SIZE; break;
387 case 't': mFlags |= LS_MTIME; break;
388 case 'C': mFlags |= LS_COLUMNS; break;
389 default: {
390 fossil_fatal("unknown option: -%c", z[k]);
 
 
 
 
 
 
 
 
 
 
 
 
391 }
392 }
393 }
394 }else{
395 if( (mFlags & LS_DIRONLY)==0 && file_isdir(z, ExtFILE)==1 ){
@@ -412,11 +399,10 @@
399 xsystem_ls_insert(pStmt, z, mFlags);
400 }
401 }
402 }
403 if( fossil_isatty(1) ){
 
404 mFlags |= LS_COLUMNS;
405 }
406 if( nFile>0 ){
407 xsystem_ls_render(db, mFlags);
408 needBlankLine = 1;
409

Keyboard Shortcuts

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