From d0fe5131ad7d641b0e6b73e3e579fa0755ff10a6 Mon Sep 17 00:00:00 2001 From: Inhere Date: Mon, 29 Aug 2022 22:13:06 +0800 Subject: [PATCH] chore: update some comments code styles --- .github/workflows/go.yml | 2 +- color.go | 23 ++++++++++++++------ color_test.go | 15 +++++++------ detect_windows.go | 46 +++++++++++++++++++++++----------------- go.mod | 2 +- issues_test.go | 26 +++++++++++++++++++++++ utils.go | 2 ++ 7 files changed, 82 insertions(+), 34 deletions(-) create mode 100644 issues_test.go diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index c0a528f..06f74a8 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -14,7 +14,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - go_version: [1.15, 1.16, 1.17, 1.18] + go_version: [1.16, 1.17, 1.18, 1.19] os: [ubuntu-latest, windows-latest, macOS-latest] steps: diff --git a/color.go b/color.go index 29eb284..59e0b0b 100644 --- a/color.go +++ b/color.go @@ -15,6 +15,7 @@ import ( "io" "os" "regexp" + "strings" "github.com/xo/terminfo" ) @@ -22,7 +23,8 @@ import ( // color render templates // // ESC 操作的表示: -// "\033"(Octal 8进制) = "\x1b"(Hexadecimal 16进制) = 27 (10进制) +// +// "\033"(Octal 8进制) = "\x1b"(Hexadecimal 16进制) = 27 (10进制) const ( // StartSet chars StartSet = "\x1b[" @@ -32,6 +34,8 @@ const ( SettingTpl = "\x1b[%sm" // FullColorTpl for build color code FullColorTpl = "\x1b[%sm%s\x1b[0m" + // CodeSuffix string for color code. + CodeSuffix = "[0m" ) // CodeExpr regex to clear color codes eg "\033[1;36mText\x1b[0m" @@ -60,7 +64,7 @@ var ( // if not in windows, it's always False. isLikeInCmd bool // the color support level for current terminal - // needVTP - need enable VTP, only for windows OS + // needVTP - need enable VTP, only for Windows OS colorLevel, needVTP = detectTermColorLevel() // match color codes codeRegex = regexp.MustCompile(CodeExpr) @@ -159,7 +163,8 @@ func ForceOpenColor() terminfo.ColorLevel { } // IsLikeInCmd check result -// Deprecated +// +// Deprecated: please don't use func IsLikeInCmd() bool { return isLikeInCmd } @@ -176,7 +181,8 @@ func InnerErrs() []error { // RenderCode render message by color code. // // Usage: -// msg := RenderCode("3;32;45", "some", "message") +// +// msg := RenderCode("3;32;45", "some", "message") func RenderCode(code string, args ...interface{}) string { var message string if ln := len(args); ln == 0 { @@ -216,7 +222,8 @@ func RenderWithSpaces(code string, args ...interface{}) string { // RenderString render a string with color code. // // Usage: -// msg := RenderString("3;32;45", "a message") +// +// msg := RenderString("3;32;45", "a message") func RenderString(code string, str string) string { if len(code) == 0 || str == "" { return str @@ -234,7 +241,11 @@ func RenderString(code string, str string) string { // ClearCode clear color codes. // // eg: -// "\033[36;1mText\x1b[0m" -> "Text" +// +// "\033[36;1mText\x1b[0m" -> "Text" func ClearCode(str string) string { + if !strings.Contains(str, CodeSuffix) { + return str + } return codeRegex.ReplaceAllString(str, "") } diff --git a/color_test.go b/color_test.go index af20889..08c7abb 100644 --- a/color_test.go +++ b/color_test.go @@ -3,7 +3,6 @@ package color import ( "bytes" "fmt" - "io/ioutil" "os" "runtime" "strings" @@ -202,13 +201,15 @@ func TestRenderCode(t *testing.T) { } func TestClearCode(t *testing.T) { - art := assert.New(t) - art.Equal("Text", ClearCode("\033[36;1mText\x1b[0m")) + is := assert.New(t) + + is.Equal("Text", ClearCode("Text")) + is.Equal("Text", ClearCode("\033[36;1mText\x1b[0m")) // 8bit - art.Equal("Text", ClearCode("\x1b[38;5;242mText\x1b[0m")) + is.Equal("Text", ClearCode("\x1b[38;5;242mText\x1b[0m")) // 24bit - art.Equal("Text", ClearCode("\x1b[38;2;30;144;255mText\x1b[0m")) - art.Equal("Text other", ClearCode("\033[36;1mText\x1b[0m other")) + is.Equal("Text", ClearCode("\x1b[38;2;30;144;255mText\x1b[0m")) + is.Equal("Text other", ClearCode("\033[36;1mText\x1b[0m other")) } /************************************************************* @@ -707,6 +708,7 @@ func resetColorRender() { ResetOutput() } +/* var oldStdout, newReader *os.File // Usage: @@ -741,6 +743,7 @@ func restoreStdout() string { return string(out) } +*/ // mockEnvValue will store old env value, set new val. will restore old value on end. func mockEnvValue(key, val string, fn func(nv string)) { diff --git a/detect_windows.go b/detect_windows.go index dea671d..df538b2 100644 --- a/detect_windows.go +++ b/detect_windows.go @@ -1,11 +1,13 @@ //go:build windows // +build windows -// Display color on windows +// Display color on Windows +// // refer: -// golang.org/x/sys/windows -// golang.org/x/crypto/ssh/terminal -// https://docs.microsoft.com/en-us/windows/console +// +// golang.org/x/sys/windows +// golang.org/x/crypto/ssh/terminal +// https://docs.microsoft.com/en-us/windows/console package color import ( @@ -39,7 +41,7 @@ func init() { return } - // if at windows's ConEmu, Cmder, putty ... terminals not need VTP + // if at Windows's ConEmu, Cmder, putty ... terminals not need VTP // -------- try force enable colors on windows terminal ------- tryEnableVTP(needVTP) @@ -48,7 +50,7 @@ func init() { // err := getConsoleScreenBufferInfo(uintptr(syscall.Stdout), &defScreenInfo) } -// try force enable colors on windows terminal +// try force enable colors on Windows terminal func tryEnableVTP(enable bool) bool { if !enable { return false @@ -58,7 +60,7 @@ func tryEnableVTP(enable bool) bool { initKernel32Proc() - // enable colors on windows terminal + // enable colors on Windows terminal if tryEnableOnCONOUT() { return true } @@ -71,7 +73,7 @@ func initKernel32Proc() { return } - // load related windows dll + // load related Windows dll // https://docs.microsoft.com/en-us/windows/console/setconsolemode kernel32 = syscall.NewLazyDLL("kernel32.dll") @@ -112,8 +114,10 @@ var ( ) // refer -// https://github.com/Delta456/box-cli-maker/blob/7b5a1ad8a016ce181e7d8b05e24b54ff60b4b38a/detect_windows.go#L30-L57 -// https://github.com/gookit/color/issues/25#issuecomment-738727917 +// +// https://github.com/Delta456/box-cli-maker/blob/7b5a1ad8a016ce181e7d8b05e24b54ff60b4b38a/detect_windows.go#L30-L57 +// https://github.com/gookit/color/issues/25#issuecomment-738727917 +// // detects the color level supported on Windows: cmd, powerShell func detectSpecialTermColor(termVal string) (tl Level, needVTP bool) { if os.Getenv("ConEmuANSI") == "ON" { @@ -131,7 +135,7 @@ func detectSpecialTermColor(termVal string) (tl Level, needVTP bool) { // Detect if using ANSICON on older systems if os.Getenv("ANSICON") != "" { conVersion := os.Getenv("ANSICON_VER") - // 8 bit Colors were only supported after v1.81 release + // 8-bit Colors were only supported after v1.81 release if conVersion >= "181" { return terminfo.ColorLevelHundreds, false } @@ -141,7 +145,7 @@ func detectSpecialTermColor(termVal string) (tl Level, needVTP bool) { return terminfo.ColorLevelNone, false } - // True Color is not available before build 14931 so fallback to 8 bit color. + // True Color is not available before build 14931 so fallback to 8-bit color. if buildNumber < 14931 { return terminfo.ColorLevelHundreds, true } @@ -152,7 +156,7 @@ func detectSpecialTermColor(termVal string) (tl Level, needVTP bool) { } /************************************************************* - * render full color code on windows(8,16,24bit color) + * render full color code on Windows(8,16,24bit color) *************************************************************/ // docs https://docs.microsoft.com/zh-cn/windows/console/getconsolemode#parameters @@ -167,9 +171,10 @@ const ( // doc https://docs.microsoft.com/zh-cn/windows/console/console-virtual-terminal-sequences#samples // // Usage: -// err := EnableVirtualTerminalProcessing(syscall.Stdout, true) -// // support print color text -// err = EnableVirtualTerminalProcessing(syscall.Stdout, false) +// +// err := EnableVirtualTerminalProcessing(syscall.Stdout, true) +// // support print color text +// err = EnableVirtualTerminalProcessing(syscall.Stdout, false) func EnableVirtualTerminalProcessing(stream syscall.Handle, enable bool) error { var mode uint32 // Check if it is currently in the terminal @@ -217,7 +222,7 @@ func EnableVirtualTerminalProcessing(stream syscall.Handle, enable bool) error { // } /************************************************************* - * render simple color code on windows + * render simple color code on Windows *************************************************************/ // IsTty returns true if the given file descriptor is a terminal. @@ -232,9 +237,10 @@ func IsTty(fd uintptr) bool { // IsTerminal returns true if the given file descriptor is a terminal. // // Usage: -// fd := os.Stdout.Fd() -// fd := uintptr(syscall.Stdout) // for windows -// IsTerminal(fd) +// +// fd := os.Stdout.Fd() +// fd := uintptr(syscall.Stdout) // for Windows +// IsTerminal(fd) func IsTerminal(fd uintptr) bool { initKernel32Proc() diff --git a/go.mod b/go.mod index bf64ac8..e911ba1 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/gookit/color -go 1.15 +go 1.16 require ( github.com/stretchr/testify v1.8.0 diff --git a/issues_test.go b/issues_test.go new file mode 100644 index 0000000..74d78e2 --- /dev/null +++ b/issues_test.go @@ -0,0 +1,26 @@ +package color + +import ( + "fmt" + "strings" + "testing" +) + +// https://github.com/gookit/color/issues/51 +func TestIssues_51(t *testing.T) { + topBarRs := []rune{ + 9484, 32, 66, 111, 120, 32, 32, 32, 32, 32, 67, 76, 73, 32, 32, 32, 32, 32, 77, 97, 107, 101, 114, 32, 32, 32, 128230, 32, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, + 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9472, 9488, + } + + topBar := string(topBarRs) + + titleRs := []rune{ + 66, 111, 120, 32, 32, 32, 67, 76, 73, 32, 32, 32, 32, 32, 77, 97, 107, 101, 114, 32, 32, 32, 128230, + } + title := string(titleRs) + + fmt.Printf("topBar:\n%q\n%q\n", topBar, ClearCode(topBar)) + fmt.Printf("title:\n%q\n%q\n", title, ClearCode(title)) + fmt.Printf("Split:\n%#v\n", strings.Split(ClearCode(topBar), ClearCode(title))) +} diff --git a/utils.go b/utils.go index d4bb78d..4554b27 100644 --- a/utils.go +++ b/utils.go @@ -47,6 +47,7 @@ func Println(a ...interface{}) { } // Fprint print rendered messages to writer +// // Notice: will ignore print error func Fprint(w io.Writer, a ...interface{}) { _, err := fmt.Fprint(w, Render(a...)) @@ -86,6 +87,7 @@ func Lprint(l *log.Logger, a ...interface{}) { // Render parse color tags, return rendered string. // // Usage: +// // text := Render("hello world!") // fmt.Println(text) func Render(a ...interface{}) string {