Skip to content

Commit d0fe513

Browse files
committed
chore: update some comments code styles
1 parent ee046fd commit d0fe513

File tree

7 files changed

+82
-34
lines changed

7 files changed

+82
-34
lines changed

.github/workflows/go.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
runs-on: ${{ matrix.os }}
1515
strategy:
1616
matrix:
17-
go_version: [1.15, 1.16, 1.17, 1.18]
17+
go_version: [1.16, 1.17, 1.18, 1.19]
1818
os: [ubuntu-latest, windows-latest, macOS-latest]
1919

2020
steps:

color.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ import (
1515
"io"
1616
"os"
1717
"regexp"
18+
"strings"
1819

1920
"github.com/xo/terminfo"
2021
)
2122

2223
// color render templates
2324
//
2425
// ESC 操作的表示:
25-
// "\033"(Octal 8进制) = "\x1b"(Hexadecimal 16进制) = 27 (10进制)
26+
//
27+
// "\033"(Octal 8进制) = "\x1b"(Hexadecimal 16进制) = 27 (10进制)
2628
const (
2729
// StartSet chars
2830
StartSet = "\x1b["
@@ -32,6 +34,8 @@ const (
3234
SettingTpl = "\x1b[%sm"
3335
// FullColorTpl for build color code
3436
FullColorTpl = "\x1b[%sm%s\x1b[0m"
37+
// CodeSuffix string for color code.
38+
CodeSuffix = "[0m"
3539
)
3640

3741
// CodeExpr regex to clear color codes eg "\033[1;36mText\x1b[0m"
@@ -60,7 +64,7 @@ var (
6064
// if not in windows, it's always False.
6165
isLikeInCmd bool
6266
// the color support level for current terminal
63-
// needVTP - need enable VTP, only for windows OS
67+
// needVTP - need enable VTP, only for Windows OS
6468
colorLevel, needVTP = detectTermColorLevel()
6569
// match color codes
6670
codeRegex = regexp.MustCompile(CodeExpr)
@@ -159,7 +163,8 @@ func ForceOpenColor() terminfo.ColorLevel {
159163
}
160164

161165
// IsLikeInCmd check result
162-
// Deprecated
166+
//
167+
// Deprecated: please don't use
163168
func IsLikeInCmd() bool {
164169
return isLikeInCmd
165170
}
@@ -176,7 +181,8 @@ func InnerErrs() []error {
176181
// RenderCode render message by color code.
177182
//
178183
// Usage:
179-
// msg := RenderCode("3;32;45", "some", "message")
184+
//
185+
// msg := RenderCode("3;32;45", "some", "message")
180186
func RenderCode(code string, args ...interface{}) string {
181187
var message string
182188
if ln := len(args); ln == 0 {
@@ -216,7 +222,8 @@ func RenderWithSpaces(code string, args ...interface{}) string {
216222
// RenderString render a string with color code.
217223
//
218224
// Usage:
219-
// msg := RenderString("3;32;45", "a message")
225+
//
226+
// msg := RenderString("3;32;45", "a message")
220227
func RenderString(code string, str string) string {
221228
if len(code) == 0 || str == "" {
222229
return str
@@ -234,7 +241,11 @@ func RenderString(code string, str string) string {
234241
// ClearCode clear color codes.
235242
//
236243
// eg:
237-
// "\033[36;1mText\x1b[0m" -> "Text"
244+
//
245+
// "\033[36;1mText\x1b[0m" -> "Text"
238246
func ClearCode(str string) string {
247+
if !strings.Contains(str, CodeSuffix) {
248+
return str
249+
}
239250
return codeRegex.ReplaceAllString(str, "")
240251
}

color_test.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package color
33
import (
44
"bytes"
55
"fmt"
6-
"io/ioutil"
76
"os"
87
"runtime"
98
"strings"
@@ -202,13 +201,15 @@ func TestRenderCode(t *testing.T) {
202201
}
203202

204203
func TestClearCode(t *testing.T) {
205-
art := assert.New(t)
206-
art.Equal("Text", ClearCode("\033[36;1mText\x1b[0m"))
204+
is := assert.New(t)
205+
206+
is.Equal("Text", ClearCode("Text"))
207+
is.Equal("Text", ClearCode("\033[36;1mText\x1b[0m"))
207208
// 8bit
208-
art.Equal("Text", ClearCode("\x1b[38;5;242mText\x1b[0m"))
209+
is.Equal("Text", ClearCode("\x1b[38;5;242mText\x1b[0m"))
209210
// 24bit
210-
art.Equal("Text", ClearCode("\x1b[38;2;30;144;255mText\x1b[0m"))
211-
art.Equal("Text other", ClearCode("\033[36;1mText\x1b[0m other"))
211+
is.Equal("Text", ClearCode("\x1b[38;2;30;144;255mText\x1b[0m"))
212+
is.Equal("Text other", ClearCode("\033[36;1mText\x1b[0m other"))
212213
}
213214

214215
/*************************************************************
@@ -707,6 +708,7 @@ func resetColorRender() {
707708
ResetOutput()
708709
}
709710

711+
/*
710712
var oldStdout, newReader *os.File
711713
712714
// Usage:
@@ -741,6 +743,7 @@ func restoreStdout() string {
741743
742744
return string(out)
743745
}
746+
*/
744747

745748
// mockEnvValue will store old env value, set new val. will restore old value on end.
746749
func mockEnvValue(key, val string, fn func(nv string)) {

detect_windows.go

+26-20
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
//go:build windows
22
// +build windows
33

4-
// Display color on windows
4+
// Display color on Windows
5+
//
56
// refer:
6-
// golang.org/x/sys/windows
7-
// golang.org/x/crypto/ssh/terminal
8-
// https://docs.microsoft.com/en-us/windows/console
7+
//
8+
// golang.org/x/sys/windows
9+
// golang.org/x/crypto/ssh/terminal
10+
// https://docs.microsoft.com/en-us/windows/console
911
package color
1012

1113
import (
@@ -39,7 +41,7 @@ func init() {
3941
return
4042
}
4143

42-
// if at windows's ConEmu, Cmder, putty ... terminals not need VTP
44+
// if at Windows's ConEmu, Cmder, putty ... terminals not need VTP
4345

4446
// -------- try force enable colors on windows terminal -------
4547
tryEnableVTP(needVTP)
@@ -48,7 +50,7 @@ func init() {
4850
// err := getConsoleScreenBufferInfo(uintptr(syscall.Stdout), &defScreenInfo)
4951
}
5052

51-
// try force enable colors on windows terminal
53+
// try force enable colors on Windows terminal
5254
func tryEnableVTP(enable bool) bool {
5355
if !enable {
5456
return false
@@ -58,7 +60,7 @@ func tryEnableVTP(enable bool) bool {
5860

5961
initKernel32Proc()
6062

61-
// enable colors on windows terminal
63+
// enable colors on Windows terminal
6264
if tryEnableOnCONOUT() {
6365
return true
6466
}
@@ -71,7 +73,7 @@ func initKernel32Proc() {
7173
return
7274
}
7375

74-
// load related windows dll
76+
// load related Windows dll
7577
// https://docs.microsoft.com/en-us/windows/console/setconsolemode
7678
kernel32 = syscall.NewLazyDLL("kernel32.dll")
7779

@@ -112,8 +114,10 @@ var (
112114
)
113115

114116
// refer
115-
// https://github.com/Delta456/box-cli-maker/blob/7b5a1ad8a016ce181e7d8b05e24b54ff60b4b38a/detect_windows.go#L30-L57
116-
// https://github.com/gookit/color/issues/25#issuecomment-738727917
117+
//
118+
// https://github.com/Delta456/box-cli-maker/blob/7b5a1ad8a016ce181e7d8b05e24b54ff60b4b38a/detect_windows.go#L30-L57
119+
// https://github.com/gookit/color/issues/25#issuecomment-738727917
120+
//
117121
// detects the color level supported on Windows: cmd, powerShell
118122
func detectSpecialTermColor(termVal string) (tl Level, needVTP bool) {
119123
if os.Getenv("ConEmuANSI") == "ON" {
@@ -131,7 +135,7 @@ func detectSpecialTermColor(termVal string) (tl Level, needVTP bool) {
131135
// Detect if using ANSICON on older systems
132136
if os.Getenv("ANSICON") != "" {
133137
conVersion := os.Getenv("ANSICON_VER")
134-
// 8 bit Colors were only supported after v1.81 release
138+
// 8-bit Colors were only supported after v1.81 release
135139
if conVersion >= "181" {
136140
return terminfo.ColorLevelHundreds, false
137141
}
@@ -141,7 +145,7 @@ func detectSpecialTermColor(termVal string) (tl Level, needVTP bool) {
141145
return terminfo.ColorLevelNone, false
142146
}
143147

144-
// True Color is not available before build 14931 so fallback to 8 bit color.
148+
// True Color is not available before build 14931 so fallback to 8-bit color.
145149
if buildNumber < 14931 {
146150
return terminfo.ColorLevelHundreds, true
147151
}
@@ -152,7 +156,7 @@ func detectSpecialTermColor(termVal string) (tl Level, needVTP bool) {
152156
}
153157

154158
/*************************************************************
155-
* render full color code on windows(8,16,24bit color)
159+
* render full color code on Windows(8,16,24bit color)
156160
*************************************************************/
157161

158162
// docs https://docs.microsoft.com/zh-cn/windows/console/getconsolemode#parameters
@@ -167,9 +171,10 @@ const (
167171
// doc https://docs.microsoft.com/zh-cn/windows/console/console-virtual-terminal-sequences#samples
168172
//
169173
// Usage:
170-
// err := EnableVirtualTerminalProcessing(syscall.Stdout, true)
171-
// // support print color text
172-
// err = EnableVirtualTerminalProcessing(syscall.Stdout, false)
174+
//
175+
// err := EnableVirtualTerminalProcessing(syscall.Stdout, true)
176+
// // support print color text
177+
// err = EnableVirtualTerminalProcessing(syscall.Stdout, false)
173178
func EnableVirtualTerminalProcessing(stream syscall.Handle, enable bool) error {
174179
var mode uint32
175180
// Check if it is currently in the terminal
@@ -217,7 +222,7 @@ func EnableVirtualTerminalProcessing(stream syscall.Handle, enable bool) error {
217222
// }
218223

219224
/*************************************************************
220-
* render simple color code on windows
225+
* render simple color code on Windows
221226
*************************************************************/
222227

223228
// IsTty returns true if the given file descriptor is a terminal.
@@ -232,9 +237,10 @@ func IsTty(fd uintptr) bool {
232237
// IsTerminal returns true if the given file descriptor is a terminal.
233238
//
234239
// Usage:
235-
// fd := os.Stdout.Fd()
236-
// fd := uintptr(syscall.Stdout) // for windows
237-
// IsTerminal(fd)
240+
//
241+
// fd := os.Stdout.Fd()
242+
// fd := uintptr(syscall.Stdout) // for Windows
243+
// IsTerminal(fd)
238244
func IsTerminal(fd uintptr) bool {
239245
initKernel32Proc()
240246

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/gookit/color
22

3-
go 1.15
3+
go 1.16
44

55
require (
66
github.com/stretchr/testify v1.8.0

issues_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package color
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"testing"
7+
)
8+
9+
// https://github.com/gookit/color/issues/51
10+
func TestIssues_51(t *testing.T) {
11+
topBarRs := []rune{
12+
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,
13+
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,
14+
}
15+
16+
topBar := string(topBarRs)
17+
18+
titleRs := []rune{
19+
66, 111, 120, 32, 32, 32, 67, 76, 73, 32, 32, 32, 32, 32, 77, 97, 107, 101, 114, 32, 32, 32, 128230,
20+
}
21+
title := string(titleRs)
22+
23+
fmt.Printf("topBar:\n%q\n%q\n", topBar, ClearCode(topBar))
24+
fmt.Printf("title:\n%q\n%q\n", title, ClearCode(title))
25+
fmt.Printf("Split:\n%#v\n", strings.Split(ClearCode(topBar), ClearCode(title)))
26+
}

utils.go

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func Println(a ...interface{}) {
4747
}
4848

4949
// Fprint print rendered messages to writer
50+
//
5051
// Notice: will ignore print error
5152
func Fprint(w io.Writer, a ...interface{}) {
5253
_, err := fmt.Fprint(w, Render(a...))
@@ -86,6 +87,7 @@ func Lprint(l *log.Logger, a ...interface{}) {
8687
// Render parse color tags, return rendered string.
8788
//
8889
// Usage:
90+
//
8991
// text := Render("<info>hello</> <cyan>world</>!")
9092
// fmt.Println(text)
9193
func Render(a ...interface{}) string {

0 commit comments

Comments
 (0)