Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Control when color codes are sent to stdout #19

Open
4 tasks
strboul opened this issue Jul 27, 2022 · 0 comments
Open
4 tasks

Control when color codes are sent to stdout #19

strboul opened this issue Jul 27, 2022 · 0 comments
Assignees

Comments

@strboul
Copy link
Owner

strboul commented Jul 27, 2022

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
@strboul strboul self-assigned this Jul 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant