Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to clear colored string? #150

Closed
Charliego3 opened this issue Nov 8, 2021 · 2 comments
Closed

How to clear colored string? #150

Charliego3 opened this issue Nov 8, 2021 · 2 comments

Comments

@Charliego3
Copy link

Charliego3 commented Nov 8, 2021

At the same time, output the log in the terminal and the file. I want the terminal to output colors, the file has no color characters. Register an interceptor in the log frame. How can I clear the colored string?

golog.Handle(func(log *golog.Log) (handled bool) {
   // clear the colored string
    return false
})
// no color in file
// colored on terminal
golog.Info(color.RedString("color message with log"))
@fatih
Copy link
Owner

fatih commented Nov 8, 2021

You can create an individual color variable and do it on a per-color variable:

c := color.New(color.FgCyan)
c.Println("Prints cyan text")

c.DisableColor()
c.Println("This is printed without any color")

c.EnableColor()
c.Println("This prints again cyan...")

Or you can disable it globally with color.NoColor or clear it for existing colors via color.Unset(). It's in the readme.

@Charliego3
Copy link
Author

You can create an individual color variable and do it on a per-color variable:

c := color.New(color.FgCyan)
c.Println("Prints cyan text")

c.DisableColor()
c.Println("This is printed without any color")

c.EnableColor()
c.Println("This prints again cyan...")

Or you can disable it globally with color.NoColor or clear it for existing colors via color.Unset(). It's in the readme.

Sorry, maybe I made a mistake, color.NoColor or color.Unset() is global config. If it is set, all will not be displayed in color, which is not what I want. I meaning is color.RedString("color text") is converted to \x1b[31mcolor text \x1b[0m after, how to clear the color descriptor \x1b[\d+m in the string and replace it with regular? Can you provide an API to handle this. Thanks for your reply.

redText := color.RedString("color text")
fmt.Println(redText) // want to no color

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants