Skip to content

Commit

Permalink
chore: update some comments code styles
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 29, 2022
1 parent ee046fd commit d0fe513
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Expand Up @@ -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:
Expand Down
23 changes: 17 additions & 6 deletions color.go
Expand Up @@ -15,14 +15,16 @@ import (
"io"
"os"
"regexp"
"strings"

"github.com/xo/terminfo"
)

// 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["
Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -159,7 +163,8 @@ func ForceOpenColor() terminfo.ColorLevel {
}

// IsLikeInCmd check result
// Deprecated
//
// Deprecated: please don't use
func IsLikeInCmd() bool {
return isLikeInCmd
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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, "")
}
15 changes: 9 additions & 6 deletions color_test.go
Expand Up @@ -3,7 +3,6 @@ package color
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"runtime"
"strings"
Expand Down Expand Up @@ -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"))
}

/*************************************************************
Expand Down Expand Up @@ -707,6 +708,7 @@ func resetColorRender() {
ResetOutput()
}

/*
var oldStdout, newReader *os.File
// Usage:
Expand Down Expand Up @@ -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)) {
Expand Down
46 changes: 26 additions & 20 deletions 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 (
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -58,7 +60,7 @@ func tryEnableVTP(enable bool) bool {

initKernel32Proc()

// enable colors on windows terminal
// enable colors on Windows terminal
if tryEnableOnCONOUT() {
return true
}
Expand All @@ -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")

Expand Down Expand Up @@ -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" {
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion 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
Expand Down
26 changes: 26 additions & 0 deletions 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)))
}
2 changes: 2 additions & 0 deletions utils.go
Expand Up @@ -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...))
Expand Down Expand Up @@ -86,6 +87,7 @@ func Lprint(l *log.Logger, a ...interface{}) {
// Render parse color tags, return rendered string.
//
// Usage:
//
// text := Render("<info>hello</> <cyan>world</>!")
// fmt.Println(text)
func Render(a ...interface{}) string {
Expand Down

0 comments on commit d0fe513

Please sign in to comment.