mk-ls-git
mk-ls-git copied to clipboard
Keep colored output for `--color=auto` on Windows
Since there is no termios
module on Windows, there is no way to emulate a terminal, which means when using --color=auto
option, we cannot trick subprocess
to think that it is connected to a terminal instead of a pipe. Thus, with --color=auto
option, there will be no colored output.
This trick is essential for --color=auto
or -G
for BSD.
One way to force colored output is to replace --color=auto
with --color=always
, however this will have undesired consequences when using a pipe or redirecting to a file. We can try to detect if os.stdout
is a terminal or not. However the current solution also uses termios
.
Any input is welcome.
I figured that we can simple use isatty
to detect is stdout is terminal or not. It is not *nix only.
However as I tested, isatty
always returns False in git-bash, although it works correctly on Windows cmd...
Right now, it already works and the same effect can still be obtained using
ls-git -l --color
(Not sure if this is already a known consequence.)
@abhiii5459 --color
is --color=always
, which means output is always colored, whether it's a terminal or a pipe/redirect. If you redirect the output to a file and edit it, you will actually see stuff like \033[01;31m
which means bold red...
The tricky thing here is not --color=always
, but --color=auto
, in which case the program needs to detect whether the output is terminal or not, and then decide if output should be colored. But I haven't found a way to do it when running python in git-bash.