dolt icon indicating copy to clipboard operation
dolt copied to clipboard

dolt log output contains color information

Open PavelSafronov opened this issue 3 years ago • 2 comments

Repro steps on MacOS using zsh:

mkdir test
cd test
dolt init
commit=$(dolt log --oneline | head -n 1 | cut -d ' ' -f 1)
dolt reset --hard "$commit"

The above steps will result in an error:

error: Failed to reset changes.
cause: string is not a valid branch or hash

This happens because dolt log returns text with ANSI color codes. These need to be stripped from the result before using it, with sed or something similar. Here's a working MacOS verison:

mkdir test
cd test
dolt init
commit=$(dolt log --oneline | head -n 1 | cut -d ' ' -f 1 | sed 's/\x1b\[[0-9;]*m//g')
dolt reset --hard "$commit"

PavelSafronov avatar Dec 06 '22 00:12 PavelSafronov

I think a good approach would be to follow git's approach.

Here's the important bit:

The default setting is auto, which colors output when it’s going straight to a terminal, but omits the color-control codes when the output is redirected to a pipe or a file.

By introducing a similar config setting and by default removing colors from piped output, we can fix this issue but still continue to provide colors for basic cases.

PavelSafronov avatar Dec 07 '22 17:12 PavelSafronov

To figure out whether the output is going to the terminal or is being redirected, we can use this approach:

stat, _ := os.Stdout.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
    // output to a file, don't use colors
    } else {
    // output to the terminal, ok to use colors
}

PavelSafronov avatar Dec 07 '22 18:12 PavelSafronov