Giter Club home page Giter Club logo

gookit / color Goto Github PK

View Code? Open in Web Editor NEW
1.5K 19.0 85.0 4.15 MB

🎨 Terminal color rendering library, support 8/16 colors, 256 colors, RGB color rendering output, support Print/Sprintf methods, compatible with Windows. GO CLI 控制台颜色渲染工具库,支持16色,256色,RGB色彩渲染输出,使用类似于 Print/Sprintf,兼容并支持 Windows 环境的色彩渲染

Home Page: https://gookit.github.io/color/

License: MIT License

Go 98.02% Shell 1.54% HTML 0.44%
cli-color console-color color terminal-colors 256-colors rgb-color true-color gookit go-color color-rgb

color's People

Contributors

apexskier avatar calinou avatar codacy-badger avatar dependabot-preview[bot] avatar dependabot[bot] avatar georgijd-form3 avatar hellseher avatar inhere avatar jandedobbeleer avatar mehrdadep avatar microchi avatar mxmauro avatar rdnt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

color's Issues

No color after v1.4.0 upgrade

System:

  • OS: linux
  • GO Version: 1.16
  • Pkg Version: 1.4.0
TERM=xterm

Describe the bug

After upgrading to 1.4.0, no more color in output, without error.

To Reproduce

Upgrade to v1.4.0.

green := color.FgGreen.Render
fmt.Println(">>> Blah :", green("blahblah"))

Expected behavior

The color must be printed as expected.

colors don't work on windows with git for windows while using vscode

System (please complete the following information):

  • OS: Windows 10
  • GO Version: 1.1.5
  • Pkg Version: Latest

Describe the bug

A clear and concise description of what the bug is.

To Reproduce

Use my module and run go test -v .

Expected behavior

A clear and concise description of what you expected to happen.

Same output as Windows Terminal

image

Screenshots

What I get:

image

Additional context

The screenshot is from VsCode but the same problem doesn't happen in Git Bash.

Use terminfo instead of hardcoding a list of terminals

Hey!

You should use terminfo instead of relying on the terminal name in TERM. This can provide you the number of colors supported. Otherwise, you are missing a lot of terminals supporting colors, like rxvt or screen. In Go, it seems https://github.com/xo/terminfo can do that. It also seems to handle some edge cases for terminals not using the correct TERM. It also handles the special case of true color which was not available through terminfo until recently and is able to fallback to COLORTERM (but it should be using Tc if available).

color.Basic2hex returns empty string on background color

System (please complete the following information):

  • GO Version: 1.16.6
  • Pkg Version: 1.4.2

Describe the bug

color.Basic2hex returns an empty string on background colors

To Reproduce

fmt.Printf("'%s'\n", color.Basic2hex(uint8(color.BgRed)))
// > ''
fmt.Printf("'%s'\n", color.Basic2hex(uint8(color.FgRed)))
// > 'c51e14'

Expected behavior

I expect the same response on a background color like so:

fmt.Printf("'%s'\n", color.Basic2hex(uint8(color.BgRed)))
// > 'c51e14'
fmt.Printf("'%s'\n", color.Basic2hex(uint8(color.FgRed)))
// > 'c51e14'

tag-style color rendering fails if lines contain tag like content

System (please complete the following information):

  • OS: linux
  • GO Version: 1.18.1
  • Pkg Version: 1.5.2

ENV info on the terminal (by command env | grep -i TERM):

COLORTERM=truecolor
TERM_PROGRAM_VERSION=3.2a
TERM=tmux-256color
TERM_PROGRAM=tmux

Describe the bug

I am using tag-style color rendeing to colorize tabular data. If multiple lines contain something like this: <none> then only the first colorziation works, subsequent ones are not replaced but printed as is.

To Reproduce

package main

import (
	"github.com/gookit/color"
)

func main() {
	test1 := `FAILS:
one <bg=lightGreen;fg=black>two</> <three>
foo <bg=lightGreen;fg=black>two</> <four>

`

	test2 := `WORKS:
one <bg=lightGreen;fg=black>two <three></>
foo <bg=lightGreen;fg=black>two <four></>

`

	test3 := `WORKS:
one <bg=lightGreen;fg=black>two</> three
foo <bg=lightGreen;fg=black>two</> four
`

	color.Print(test1)
	color.Print(test2)
	color.Print(test3)
}

Expected behavior

The colorziation should work independently from other non color-tag content.

Screenshots

See screenshot attached.

colorfails

** Edited to add **

The problem occurs ONLY if the "tagged" content appears outside the colorization. If it's inside, it works. Sample code and screenshot updated.

panic when running on windows with multiple go routines

Running this with multiple go routines causes this crash (on windows):

runtime: bad pointer in frame github.com/gookit/color.EnableVirtualTerminalProcessing at 0xc000581838: 0x58
fatal error: invalid pointer found on stack

runtime stack:
runtime.throw(0xc6d2fe, 0x1e)
        C:/Program Files/go/src/runtime/panic.go:1116 +0x79 fp=0xb2f1bff4d0 sp=0xb2f1bff4a0 pc=0x659799
runtime.adjustpointers(0xc000581838, 0xb2f1bff5c0, 0xb2f1bff948, 0xfbcc00, 0x100e2a0)
        C:/Program Files/go/src/runtime/stack.go:605 +0x1e8 fp=0xb2f1bff520 sp=0xb2f1bff4d0 pc=0x66e888
runtime.adjustframe(0xb2f1bff858, 0xb2f1bff948, 0x100e2a0)
        C:/Program Files/go/src/runtime/stack.go:647 +0x36b fp=0xb2f1bff5f0 sp=0xb2f1bff520 pc=0x66ec0b
runtime.gentraceback(0xffffffffffffffff, 0xffffffffffffffff, 0x0, 0xc000084d80, 0x0, 0x0, 0x7fffffff, 0xc89118, 0xb2f1bff948, 0x0, ...)
        C:/Program Files/go/src/runtime/traceback.go:334 +0x11b5 fp=0xb2f1bff8c0 sp=0xb2f1bff5f0 pc=0x67acd5
runtime.copystack(0xc000084d80, 0x2000)
        C:/Program Files/go/src/runtime/stack.go:910 +0x2a7 fp=0xb2f1bffa78 sp=0xb2f1bff8c0 pc=0x66f387
runtime.shrinkstack(0xc000084d80)
        C:/Program Files/go/src/runtime/stack.go:1178 +0x149 fp=0xb2f1bffaa0 sp=0xb2f1bffa78 pc=0x670429
runtime.scanstack(0xc000084d80, 0xc00002de98)
        C:/Program Files/go/src/runtime/mgcmark.go:815 +0x56e fp=0xb2f1bffca8 sp=0xb2f1bffaa0 pc=0x640e4e
runtime.markroot.func1()
        C:/Program Files/go/src/runtime/mgcmark.go:245 +0xce fp=0xb2f1bffcf8 sp=0xb2f1bffca8 pc=0x681cee
runtime.markroot(0xc00002de98, 0x14)
        C:/Program Files/go/src/runtime/mgcmark.go:218 +0x310 fp=0xb2f1bffd80 sp=0xb2f1bffcf8 pc=0x63f910
runtime.gcDrain(0xc00002de98, 0x7)
        C:/Program Files/go/src/runtime/mgcmark.go:1109 +0x138 fp=0xb2f1bffdd8 sp=0xb2f1bffd80 pc=0x641578
runtime.gcBgMarkWorker.func2()
        C:/Program Files/go/src/runtime/mgc.go:1981 +0x176 fp=0xb2f1bffe18 sp=0xb2f1bffdd8 pc=0x681bd6
runtime.systemstack(0x0)
        C:/Program Files/go/src/runtime/asm_amd64.s:370 +0x6b fp=0xb2f1bffe20 sp=0xb2f1bffe18 pc=0x68a6ab
runtime.mstart()
        C:/Program Files/go/src/runtime/proc.go:1116 fp=0xb2f1bffe28 sp=0xb2f1bffe20 pc=0x65e920

There are some extra color code problems in different shells

There are some extra color code problems in different shells.
The Light Red and Light Blue has problem in Windows shell.
Extra background has problem in Linux shell.

有些高亮的色碼有問題
在windows中亮紅 亮藍 反了
在linux中 高亮背景 亂了

註解 2020-09-02 060255

feature: add hsl to and from methods

Hi! I think it will be good to also add methods to take hsl as input and convert it by different variants.

For reference, you can refer to this code.

I need this feature as I will be adding HSL format as input option for my box-cli-maker.

Question: How to escape color tags

I'm using the HTML like tags feature to pretty print some JSON, but I'm concerned that occasionally the JSON data might have tags like that would start getting rendered as a color. I did a search and replace and didn't see anything, so was wondering if there was a way to escape or encode tags to make it so that color tags aren't injected?

How to "ignore" html styles

I'd like to print raw HTML colored, and I don't want gookit/color to interpret the HTML in the string. Is there a way to ignore the HTML?

Color on Windows in cygwin, powershell, cmd

This tool looks really promising! So thanks in advance.

I'm running into some issues with your isSupportColor method on Windows, which affects RenderCode.

  1. Pure git-bash:
    • $MSYSTEM = MINGW64
    • $TERM = xterm
    • 256 coloration seems to work
  2. ConsoleZ git-bash (started via C:\Program Files\Git\bin\sh.exe --login -i)
    • $MSYSTEM = MINGW64
    • $TERM = cygwin
    • coloration is disabled
    • 16-color coloration is possible via github.com/fatih/color - Output
  3. CMD:
    • %CMDEXTVERSION% = 2
    • coloration is disabled
    • 16-color coloration is possible via github.com/fatih/color - Output
  4. Powershell:
    • Version: 5.1.16299.1146
    • coloration is disabled
    • 16-color coloration is possible via github.com/fatih/color - Output

My method for testing includes creating a color.C256 object and using it's Sprint method to output the string to write to a github.com/fatih/color - Output Writer.

Thanks!

undefined: EnableCmdColorRender

When I running go get github.com/gookit/color ., I found

go/src/github.com/gookit/color/tag.go:117:3: undefined: EnableCmdColorRender
go/src/github.com/gookit/color/tag.go:130:3: undefined: EnableCmdColorRender
go/src/github.com/gookit/color/tag.go:143:3: undefined: EnableCmdColorRender

Color not working on Manjaro

Here the output from the demo, I was expecting your nice colors: unfortunately it's all white.

image

What do you think the problem is ? Did you encounter such problem ?

  • NeoFetch output if needed:
OS: Manjaro Linux x86_64 
Kernel: 4.19.81-1-MANJARO
Shell: bash 5.0.11 
Resolution: 1920x1080 
WM: i3 
Theme: Adwaita [GTK3] 
Icons: Adwaita [GTK3] 
Terminal: urxvt 
Terminal Font: DejaVu Sans Mono

Thank you for your time,

You lost a "/n"

Hi, you'd better change color.Green.Print("Simple to use color") to color.Green.Print("Simple to use color\n")

`IsConsole` just checks whether the writer is stdin, stdout or stderr, which is not reliable

func IsConsole(out io.Writer) bool {
	o, ok := out.(*os.File)
	if !ok {
		return false
	}

	return o == os.Stdout || o == os.Stderr || o == os.Stdin
}
func TestOther(t *testing.T) {
	is := assert.New(t)

	is.True(IsConsole(os.Stdout))
	is.False(IsConsole(&bytes.Buffer{}))

Sometimes the standard input / output / error streams are redirected to files. In such cases, IsConsole will still return true, but the writer is not a console or terminal

Printf does not color the entire string

System (please complete the following information):

  • OS: Kubuntu 22.04.2 LTS x86_64
  • GO Version: 1.20.4
  • Pkg Version: v1.5.4

ENV info on the terminal (by command env | grep -i TERM)
TERM_PROGRAM_VERSION=1.80.0
COLORTERM=truecolor
TERM=xterm-256color
TERM_PROGRAM=vscode

Describe the bug

When Printf recieves a "colored" string as argument it does not colors the part of string after %s.

To Reproduce

package main

import (
	"github.com/gookit/color"
)

func main() {
	color.FgRed.Printf("first %s third\n", color.FgYellow.Render("second"));
}

Expected behavior

I would expect that "first" and "third" will be colored in red, second in yellow. Instead first is colored in red, second in yellow and third in white

Screenshots
VS Code integrated terminal:
Screenshot_20230806_135419
Konsole:
Screenshot_20230806_135535
Additional context
I also tried this on Konsole and i have encountered the same issue.

color don't work when using screen

System (please complete the following information):

  • OS: Ubuntu 20.04 and Debian 10.8
  • GO Version: 1.16.2
  • Pkg Version: latest

Describe the bug

When I used screen to start my go program, the color would loss. But fmt would not.

To Reproduce

// color code
import (
        "fmt"
        "time"
        "github.com/gookit/color"
)

func main() {
        // 简单快速的使用,跟 fmt.Print* 类似
        color.Red.Println("Simple to use color")
        color.Green.Print("Simple to use color\n")
        color.Cyan.Printf("Simple to use %s\n", "color")
        color.Yellow.Printf("Simple to use %s\n", "color")

        // use like func
        red := color.FgRed.Render
        green := color.FgGreen.Render
        fmt.Printf("%s line %s library\n", red("Command"), green("color"))
        color.Green.Println(time.Now().Format("[2006-01-02 15:04:05]  "), "Test")

        // 自定义颜色
        color.New(color.FgWhite, color.BgBlack).Println("custom color style")

        // 也可以:
        color.Style{color.FgCyan, color.OpBold}.Println("custom color style")
        
        // internal style:
        color.Info.Println("message")
        color.Warn.Println("message")
        color.Error.Println("message")
        
        // 使用颜色标签
        color.Print("<suc>he</><comment>llo</>, <cyan>wel</><red>come</>\n")

        // apply a style tag
        color.Tag("info").Println("info style text")

        // prompt message
        color.Info.Prompt("prompt style message")
        color.Warn.Prompt("prompt style message")

        // tips message
        color.Info.Tips("tips style message")
        color.Warn.Tips("tips style message")
        time.Sleep(time.Minute)
}
// fmt code
package main
  
import (
        "fmt"
        "time"
)

func main() {
        fmt.Printf("\x1b[%dmhello world 30: 黑 \x1b[0m\n", 30)
        fmt.Printf("\x1b[%dmhello world 31: 红 \x1b[0m\n", 31)
        fmt.Printf("\x1b[%dmhello world 32: 绿 \x1b[0m\n", 32)
        fmt.Printf("\x1b[%dmhello world 33: 黄 \x1b[0m\n", 33)
        fmt.Printf("\x1b[%dmhello world 34: 蓝 \x1b[0m\n", 34)
        fmt.Printf("\x1b[%dmhello world 35: 紫 \x1b[0m\n", 35)
        fmt.Printf("\x1b[%dmhello world 36: 深绿 \x1b[0m\n", 36)
        fmt.Printf("\x1b[%dmhello world 37: 白色 \x1b[0m\n", 37)
        time.Sleep(time.Minute)
}

Expected behavior

When I use screen to start my program, the color will not turn to white

Screenshots

color
fmt

Library makes the program slow

System (please complete the following information):

  • OS: macOS
  • GO Version: 14.3.1
  • Pkg Version: 1.5.4

ENV info on the terminal (by command env | grep -i TERM)

TERM=xterm-256color
TERMINAL_EMULATOR=JetBrains-JediTerm
TERM_SESSION_ID=xxx

Describe the bug

When using this library for printing messages, the program runs noticeably slower than when using just fmt.Print.

To Reproduce

entries := map[string]string{
	"test": "fest",
	"rest": "best",
}

i := 1

yellow := color.FgGreen.Render

for k := range entries {
	_, err := fmt.Println(yellow(strconv.Itoa(i)+". ", k))

	if err != nil {
		color.Errorln(err)
	}

	i++
}

Expected behavior

To run at similar speed compared to using the fmt package.

Rendering is broken in windows cmd and powershell

Using the .Render method of Styles like color.New(color.OpItalic, color.OpBold), color.New(color.Cyan), or color.New(color.BgGray) doesn't seem to work on Windows. The full code I wrote is here, please test it for yourself and let me know if I made a mistake. Is it because I'm using tabwriter? The colors work fine on Linux.

Concurrency issues

On Windows, if there are many goroutines that try to write on the console (by using the Fprint function, for example), there is good chance the output will be scrambled, like so.

Removing the lines 184-187 on the file color_windows.go seems to fix this issue.

Not sure why the color setting needs to be reverted, is that needed?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.