Skip to content

Implement ColorFunc/TitleColorFunc/ContentColorFunc #49

@Delta456

Description

@Delta456

Is your feature request related to a problem? Please describe.
The feature isn't really related to a problem. For using color gradients with the box, title and content. One needs to do:

func main() {
	b := box.NewBox().Padding(2, 5).Style(box.Single).Color(box.Cyan).ContentAlign(box.Center)
	fmt.Println(b.MustRender(lolcat("Box CLI Maker"), lolcat("Render highly customizable boxes\n in the terminal"))
}

func lolcat(str string) string {
	var output string
	freq := float64(0.1)
	for s := range strings.SplitSeq(str, "") {
		output += normalStyle(freq, s)
		freq += 0.1
	}
	return output
}

func normalStyle(num float64, s string) string {
	red := uint8(math.Sin(num+0)*127 + 128)
	green := uint8(math.Sin(num+2)*127 + 128)
	blue := uint8(math.Sin(num+4)*127 + 128)

	c := &color.RGBA{R: red, G: green, B: blue, A: 255}
	style := ansi.Style{}.ForegroundColor(c)
	return style.Styled(s)
}

lolcat("Box CLI Maker") and lolcat("Render highly customizable boxes\n in the terminal") are verbose, explicit calls and don't look clean.

Describe the solution you'd like

Introduce functions like ColorFunc, TitleColorFunc, and ContentColorFunc, which take func (string) string callback functions. It would be syntax sugar for using color gradients. The code would then look like:

func main() {
	b := box.NewBox().Padding(2, 5).Style(box.Single).Color(box.Cyan).ContentAlign(box.Center).TitleColorFunc(lolcat).ContentColorFunc(lolcat)
	fmt.Println(b.MustRender("Box CLI Maker", "Render highly customizable boxes\n in the terminal")
}

func lolcat(str string) string {
	var output string
	freq := float64(0.1)
	for s := range strings.SplitSeq(str, "") {
		output += normalStyle(freq, s)
		freq += 0.1
	}
	return output
}

func normalStyle(num float64, s string) string {
	red := uint8(math.Sin(num+0)*127 + 128)
	green := uint8(math.Sin(num+2)*127 + 128)
	blue := uint8(math.Sin(num+4)*127 + 128)

	c := &color.RGBA{R: red, G: green, B: blue, A: 255}
	style := ansi.Style{}.ForegroundColor(c)
	return style.Styled(s)
}

Describe alternatives you've considered
Manually calling color gradient functions like lolcat on the title and content before passing them to the rendering functions' parameters.

Additional context
Example from https://github.com/box-cli-maker/box-cli-maker/blob/master/examples/lolcat/main.go

Metadata

Metadata

Assignees

Labels

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions