Skip to content

Control when color codes are sent to stdout  #19

@strboul

Description

@strboul

When running git-substatus with watch, the output is not clean due to the escape codes:

watch git-substatus
# Every 2.0s: git-substatus
# ^[90m directory: </home/myazici>^[0m
# • ^[1m^[34mdotfiles^[0m        ^[4m^[37mv0.4.4^[0m    ^[3m^[32m<sync>^[0m              ^[36m^[0m   ^[35m^[0m
# • ^[1m^[34mtmp-workbench^[0m   ^[4m^[37mupdates^[0m   ^[33m2 untracked & ahead 1^[0m   ^[36m^[0m   ^[35m^[0m

With watch git status the color is not there; but, when printed onto the screen, it is.

watch --color git-substatus shows the output with color, but that's a separate parameter added to the watch command.
If I add watch git -c color.status=always status, I see the git status with the escape code.


So how does git know when to add the escape codes?
Answer: there's a way to check if a file is opened on terminal

man test
...
-t FD file descriptor FD is opened on a terminal
...

echo '''
#!/bin/bash
text="Hello world"
if [ -t 1 ]; then
  echo "***stdout is a terminal***"
  text="$(printf "%s" "\e[31m$text\e[0m")"
fi
echo -e "$text"
''' > test.sh
chmod +x test.sh

./test.sh
# ***stdout is a terminal***
# Hello world
#

watch ./test.sh
# Every 2.0s: ./test.sh
# 
# Hello world
#

TODO

  • Implement that color codes are added if the stdout is going to the terminal
  • new arg: --color always prints color
  • new arg: --no-color never prints color

  • Add tests with watch

Watch 1 time & parse the output.

watch args git-substatus args color codes printed
--color --color false
--color --no-color false
`` --color true
`` --no-color false

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions