Support color output for the added/deleted lines in diff mode#388
Support color output for the added/deleted lines in diff mode#388mvdan merged 4 commits intomvdan:masterfrom
Conversation
|
Thanks for the PR. However, I disagree with always using colored output. Tools like I'll take a look at an alternative solution. |
|
Thank you for feedback. So how about using fatih/color? This package works well even if As you see, the author of this library has stopped maintaining, but it is already "done". (no need further maintenance) |
|
I'm still not convinced. That would be an extra dependency, which itself also pulls in another two dependencies, all just so we could have a bit of logic to color a few lines. But I do see the use of colored diffs. I'm ok with merging something like this, as long as colors are only enabled when:
I'll also leave a code review shortly. |
cmd/shfmt/main.go
Outdated
| return nil, err | ||
| } | ||
|
|
||
| output := make([][]byte, 0) |
There was a problem hiding this comment.
use a bytes.Buffer instead, and then the functions can use fmt.Fprint(w, ...). this also means we won't need to use bytes.Join.
cmd/shfmt/main.go
Outdated
|
|
||
| ansiFgRed = []byte("\u001b[31m") | ||
| ansiFgGreen = []byte("\u001b[32m") | ||
| ansiReset = []byte("\u001b[0m") |
There was a problem hiding this comment.
once you switch to a buffer, these can be simply strings
cmd/shfmt/main_test.go
Outdated
| - | ||
| ` | ||
| %s | ||
| `, markDeleted([]byte("- foo")), markAdded([]byte("+foo")), markDeleted([]byte("-"))) |
There was a problem hiding this comment.
Now that we'll have diff with and without colors, you could make color a global bool like diff, and have two subtests:
Diff, which is the old test as-is
DiffColored, which sets color = true, and ensures the escape codes are present.
Also, don't use fmt.Sprintf for the expected output. Just write the entire string as a single literal, escape codes and all.
|
Thanks for the detailed reviews. I’ll fix and improve them today! |
|
I fixed codes by reflecting the reviews. |
mvdan
left a comment
There was a problem hiding this comment.
Sorry, but see the os.File and TERM=dumb logic I explained earlier. This can't be a flag either.
cmd/shfmt/main.go
Outdated
| simple = flag.Bool("s", false, "") | ||
| find = flag.Bool("f", false, "") | ||
| diff = flag.Bool("d", false, "") | ||
| color = flag.Bool("c", false, "") |
There was a problem hiding this comment.
sorry, but no flag - this should be on by default on a terminal, and it can be turned off with TERM=dumb
There was a problem hiding this comment.
Oh, sorry I missed that comment. I fixed it.
This PR supports color output for the added/deleted lines in diff mode.
Current:
When PR is merged: