fatih / color

Color package for Go (golang)
https://pkg.go.dev/github.com/fatih/color
MIT License
7.26k stars 615 forks source link

Suggestion: in string color assignments #67

Closed ttofis closed 7 years ago

ttofis commented 7 years ago

Have a function that will read the string and it changes the color whenever inside the string there is a $ and one color. I already did this (pretty crappy and slow) but you can improve on this idea:

var awaitingColor bool
colorPrint := color.New(color.FgWhite)
for _, character := range sError {
    if awaitingColor {
        switch character {
        case 'w':
            colorPrint = color.New(color.FgWhite)
            break
        case 'r':
            colorPrint = color.New(color.FgRed)
            break
        case 'y':
            colorPrint = color.New(color.FgYellow)
            break
        case 'b':
            colorPrint = color.New(color.FgBlue)
                break
        }
        awaitingColor = false
    } else if character == '$' {
        awaitingColor = true
    } else {
        colorPrint.Print(string(character))
    }
}
ttofis commented 7 years ago

Never mind, it is already possible with SprintFunc