fix(utils/utils): use color.Output to handle escape sequence on Windows #826
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR focuses on the colorized outputs in Windows CMD and PowerShell.
But I'm not sure if it will break on non-Windows system. I have only tested on my windows and centos.
Issue
annie -v
doesn't print the colorized version info correctly in Windows CMD and PowerShell, but other commands such asannie -i
works well.Reason
annie -v
callsPrintVersion()
to print version info:https://github.com/iawia002/annie/blob/d0d473ae01e75b83a14066a37ab85da6a1a0f3c0/utils/utils.go#L258-L266
annie -i
callsprintStream()
to print stream info:https://github.com/iawia002/annie/blob/d0d473ae01e75b83a14066a37ab85da6a1a0f3c0/downloader/utils.go#L40-L50
In
PrintVersion()
, CMD can't handle escape sequence strings provided bycyan/blue.Sprintf()
. However, inprintStream()
, directly usingcyan/blue.Printf()
works well. That is because the packagecolor
invokesfmt.Fprintf(color.Output, format string, a ...interface{})
to print colorized strings:https://github.com/fatih/color/blob/v1.7.0/color.go#L224-L229
https://github.com/fatih/color/blob/v1.7.0/color.go#L25
So just use
Fprintf
withcolor.Output
instead ofPrintf
inPrintVersion()
:And similar issue to
printError()
:https://github.com/iawia002/annie/blob/d0d473ae01e75b83a14066a37ab85da6a1a0f3c0/main.go#L195-L200
Intuitive test
Here are some test codes and screenshots (from CMD):
PrintVersion()
PrintError()
Try to download a YouTube video without proxy:
References