cheggaaa / pb

Console progress bar for Golang
BSD 3-Clause "New" or "Revised" License
3.64k stars 269 forks source link

[Question]: How to use more colors? #193

Open thep0y opened 2 years ago

thep0y commented 2 years ago

Some colors are defined in defaultTemplateFuncs, but these are only some of the colors in the color package, how to use more colors quickly?

// Foreground text colors
const (
    FgBlack Attribute = iota + 30
    FgRed
    FgGreen
    FgYellow
    FgBlue
    FgMagenta
    FgCyan
    FgWhite
)

// Foreground Hi-Intensity text colors
const (
    FgHiBlack Attribute = iota + 90
    FgHiRed
    FgHiGreen
    FgHiYellow
    FgHiBlue
    FgHiMagenta
    FgHiCyan
    FgHiWhite
)
cheggaaa commented 1 year ago

You can register a new pb.Element

        pb.RegisterElement("myColor", pb.ElementFunc(func(state *pb.State, args ...string) string {
        for _, arg := range args {
            return color.New(color.BgHiGreen).Sprint(arg)
        }
        return ""
    }), false)
    tmpl := `{{counters . | myColor .}} {{bar . }} {{percent . }} {{ myColor . "one" }}`
    bar := pb.ProgressBarTemplate(tmpl)