fatih / color

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

Nesting colors does not work #84

Closed leighmcculloch closed 6 years ago

leighmcculloch commented 6 years ago

Scenario

I'm using the color._String functions to color sub parts of a string that have already been colored.

package main                                                                                                                                                                                     

import (                                                                                                                                                                                         
    "fmt"                                                                                                                                                                                        

    "github.com/fatih/color"                                                                                                                                                                     
)                                                                                                                                                                                                

func main() {                                                                                                                                                                                    
    fmt.Println(color.CyanString("1" + color.RedString("2") + "3"))                                                                                                                           
}

Got

screen shot 2017-11-27 at 8 20 33 pm

Want

screen shot 2017-11-27 at 8 21 33 pm

Code

It looks like the existing behavior is probably intentional, because this piece of code clearly shows the wrap function unformating the color. I'm not sure if it would be feasible to support this scenario or even if it was if that'd be outside the intended scope of this package, so I'm not assuming this should change, but wanted to open a discussion about it. I'd be up for taking a stab at it if the consensus is this would be welcomed. Some similar packages in other languages do allow nesting of colors. e.g. https://github.com/junegunn/ansi256#nesting

https://github.com/fatih/color/blob/5df930a27be2502f99b292b7cc09ebad4d0891f4/color.go#L357-L363

https://github.com/fatih/color/blob/5df930a27be2502f99b292b7cc09ebad4d0891f4/color.go#L365-L367

https://github.com/fatih/color/blob/5df930a27be2502f99b292b7cc09ebad4d0891f4/color.go#L369-L371

fatih commented 6 years ago

This is a fairly complex situation. To support this each color function has to parse the incoming string and lock if it's what formatted before. And it's just a string. So this would mean that for every single input, there will be additional computation. I'm going to close this down as I don't think to support this. Thanks anyway.

leighmcculloch commented 6 years ago

Thanks for reading and explaining @fatih! Makes sense. ❤️