Documentation
¶
Index ¶
- Constants
- Variables
- func PickColor[color ColorType](opt Optional[color], def color) color
- type ANSIColor
- type Brush
- func (b Brush[color]) Embed(values ...any) Highlighted
- func (b Brush[color]) Highlight(s string, find *regexp.Regexp) Highlighted
- func (b Brush[color]) HighlightFunc(s string, find *regexp.Regexp, repl func(string) string) Highlighted
- func (b Brush[color]) Paint(values ...any) Painted
- func (b Brush[color]) Paintf(model string, values ...any) Painted
- func (b Brush[color]) Paintln(values ...any) Painted
- func (b Brush[color]) Print(values ...any)
- func (b Brush[color]) Printf(model string, values ...any)
- func (b Brush[color]) Println(values ...any)
- func (b *Brush[color]) Swap() *Brush[color]
- func (b *Brush[color]) UseBgColor(c color) *Brush[color]
- func (b *Brush[color]) UseBgTransparent() *Brush[color]
- func (b *Brush[color]) UseDefaultColor() *Brush[color]
- func (b *Brush[color]) UseFontColor(c color) *Brush[color]
- type ColorIntensity
- type ColorType
- type ExtendedANSIColor
- type Highlighted
- type Optional
- type Painted
- type TrueColor
Examples ¶
- Brush.Embed
- Brush.Highlight
- Brush.HighlightFunc
- Brush.Paint
- Brush.Paintf
- Brush.Paintln
- Brush.Print
- Brush.Printf
- Brush.Println
- Brush.Swap
- Brush.UseBgColor
- Brush.UseBgTransparent
- Brush.UseFontColor
- GrayScale
- Highlighted.Append
- Join
- Paint
- Painted.Append
- Painted.Prepend
- Painted.Replace
- Paintf
- Paintln
- ParseHex
- PickColor
- RGB
- TrueColor
- UseColor
Constants ¶
const MaxGrayScale uint8 = 25
MaxGrayScale represents the max value you can pass to the GrayScale
Variables ¶
var ( // DisableIfNotTTY disables Painted and Brush if it detects application is // not in a tty at a global level (for this library) DisableIfNotTTY = true // Disable colored output. By default it depends by DisableIfNotTTY Disable = DisableIfNotTTY && !isATTY )
Functions ¶
func PickColor ¶
PickColor is an utility that lets you take the color referenced on the first argument or in case it's nil the second as a default case
Example ¶
package main
import (
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variable
myBrush := brush.New(brush.Yellow, nil)
reversed := brush.New(
brush.PickColor(myBrush.Background, brush.Black),
brush.UseColor(myBrush.Foreground),
)
myBrush.Println("default brush example")
reversed.Println("reversed brush example")
}
Output: �[33mdefault brush example �[0m�[30;43mreversed brush example �[0m
Types ¶
type ANSIColor ¶
type ANSIColor int8
ANSIColor represents a color from the first 16 colors in the ANSI table
const ( Black ANSIColor = iota Red Green Yellow Blue Magenta Cyan White BrightBlack BrightRed BrightGreen BrightYellow BrightBlue BrightMagenta BrightCyan BrightWhite )
All the different 16 different colors on the ANSIColor table
func (ANSIColor) ToExtended ¶
func (color ANSIColor) ToExtended() ExtendedANSIColor
ToExtended transforms an ANSIColor to an ExtendedANSIColor representation
func (ANSIColor) ToTrueColor ¶
ToTrueColor transforms an ANSIColor to a standard TrueColor representation. Be aware that the actual color might be different from the original, because the visible color might be different from the one of your terminal.
type Brush ¶
type Brush[color ColorType] struct { Foreground color Background Optional[color] Disable bool // contains filtered or unexported fields }
Brush lets you paint some strings and change styling more freely
func New ¶
New creates a new Brush with the given default colors of a specified set. Disable will be set as false unless it detects application is not in a tty and DisableIfNotTTY is true
func (Brush[color]) Embed ¶
func (b Brush[color]) Embed(values ...any) Highlighted
Embed lets create an Highlighted item by joining the given values. Painted items will maintain their styling. Highlighted items will maintain their styling only on the subset that contains info about it, for other values (and the subset of Highlighted items that do not specify info) the brush style will be enforced
Example ¶
package main
import (
"fmt"
"regexp"
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables
var (
green = brush.New(brush.Green, nil)
blue = brush.New(brush.Blue, nil)
yellow = brush.New(brush.Yellow, nil)
)
fmt.Print(blue.Embed(
yellow.Highlight("Sun is yellow\n", regexp.MustCompile(`Sun|yellow`)),
green.Paint("Grass"), " is ", green.Paint("green"),
"\nAll the rest is blue",
))
}
Output: �[33mSun�[0m�[34m is �[0m�[33myellow�[0m�[34m �[0m�[32mGrass�[0m�[34m is �[0m�[32mgreen�[0m�[34m All the rest is blue�[0m
func (Brush[color]) Highlight ¶
func (b Brush[color]) Highlight(s string, find *regexp.Regexp) Highlighted
Highlight only the matching part of the given string with the color of the brush
Example ¶
package main
import (
"fmt"
"regexp"
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables
var (
marker = brush.New(brush.Black, brush.UseColor(brush.Yellow))
text = "bla bla something intresting, something intresting blah bla"
res = marker.Highlight(text, regexp.MustCompile("something intresting"))
)
fmt.Print(res)
}
Output: bla bla �[30;43msomething intresting�[0m, �[30;43msomething intresting�[0m blah bla
func (Brush[color]) HighlightFunc ¶
func (b Brush[color]) HighlightFunc(s string, find *regexp.Regexp, repl func(string) string) Highlighted
HighlightFunc is like Highlight but allows you to replace the part that will match the returned string of the repl function will then be highlighted using brush styling
Example ¶
package main
import (
"fmt"
"regexp"
"strings"
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables
marker := brush.New(brush.Black, brush.UseColor(brush.BrightYellow))
fmt.Print(marker.HighlightFunc(
"this is uppercase and yellow",
regexp.MustCompile("uppercase"),
strings.ToUpper,
))
}
Output: this is �[30;103mUPPERCASE�[0m and yellow
func (Brush[color]) Paint ¶
Paint some values (joined without separator) with the current brush colors. If a Painted and/or an Highlighted item is given, they will lose their previous style and the current styling of the brush will be enforced
Example ¶
package main
import (
"fmt"
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables
myBrush := brush.New(brush.Red, nil)
fmt.Println("I", myBrush.Paint("love"), "go")
}
Output: I �[31mlove�[0m go
func (Brush[color]) Paintf ¶
Paintf is like Paint but similarly to fmt.Sprintf it allows to use a model with some placeholders that will be replaced by the values. If a Painted and/or an Highlighted item is given, they will lose their previous style and the current styling of the brush will be enforced
Example ¶
package main
import (
"fmt"
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables
myBrush := brush.New(brush.White, brush.UseColor(brush.Blue))
fmt.Println("The sky is", myBrush.Paintf("%s", "blue"))
}
Output: The sky is �[37;44mblue�[0m
func (Brush[color]) Paintln ¶
Paintln like Paint but similarly to fmt.Sprintln it separates values with " " and it adds a "\n" at the end of all. If a Painted and/or an Highlighted item is given, they will lose their previous style and the current styling of the brush will be enforced
Example ¶
package main
import (
"fmt"
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables
myBrush := brush.New(brush.Black, brush.UseColor(brush.White))
fmt.Print(myBrush.Paintln("Hello", "World", "!"))
}
Output: �[30;47mHello World ! �[0m
func (Brush[color]) Print ¶
Print shows on stdout some values (joined without separator) enforcing the current font and background color of the brush
Example ¶
package main
import (
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables
myBrush := brush.New(brush.Black, brush.UseColor(brush.White))
myBrush.Print("Hello", " World")
}
Output: �[30;47mHello World�[0m
func (Brush[color]) Printf ¶
Printf shows on stdout some values by replacing the placeholders in the given model with the corresponding values enforcing the current font and background color of the brush
Example ¶
package main
import (
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables
myBrush := brush.New(brush.Black, brush.UseColor(brush.White))
myBrush.Printf("%s %s", "Hello", "World")
}
Output: �[30;47mHello World�[0m
func (Brush[color]) Println ¶
Println shows on stdout some values (joined with " ", and adding a "\n" at the end) enforcing the current font and background color of the brush
Example ¶
package main
import (
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables
myBrush := brush.New(brush.Black, brush.UseColor(brush.White))
myBrush.Println("Hello", "World")
}
Output: �[30;47mHello World �[0m
func (*Brush[color]) Swap ¶
Swap overrides font and background color by inverting them and gives back the same (now modified) brush If background was unset then the default foreground color will be used as foreground
Example ¶
package main
import (
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables
myBrush := brush.New(brush.Black, brush.UseColor(brush.White))
myBrush.Println("something")
myBrush.Swap().Println("something else")
}
Output: �[30;47msomething �[0m�[37;40msomething else �[0m
func (*Brush[color]) UseBgColor ¶
UseFontColor overrides the background color and gives back the same (now modified) brush
Example ¶
package main
import (
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables
myBrush := brush.New(brush.Black, brush.UseColor(brush.White))
myBrush.Println("something")
myBrush.UseBgColor(brush.Cyan).Println("something else")
}
Output: �[30;47msomething �[0m�[30;46msomething else �[0m
func (*Brush[color]) UseBgTransparent ¶
UseFontColor overrides the background color removing it and gives back the same (now modified) brush
Example ¶
package main
import (
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables
myBrush := brush.New(brush.Black, brush.UseColor(brush.White))
myBrush.Println("something")
myBrush.UseBgTransparent().Println("something else")
}
Output: �[30;47msomething �[0m�[30msomething else �[0m
func (*Brush[color]) UseDefaultColor ¶
UseDefaultColor overrides font and background color by using the default values and gives back the same (now modified) brush
func (*Brush[color]) UseFontColor ¶
UseFontColor overrides the font color and gives back the same (now modified) brush
Example ¶
package main
import (
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables
myBrush := brush.New(brush.Black, brush.UseColor(brush.White))
myBrush.Println("something")
myBrush.UseFontColor(brush.Blue).Println("something else")
}
Output: �[30;47msomething �[0m�[34;47msomething else �[0m
type ColorIntensity ¶
type ColorIntensity uint8
ColorIntensity rapresent a level on the scale of color color intensity from 0 to 5
const ( ZeroIntensity ColorIntensity = iota // Intensity lv. 0 = 0 LowIntensity // Intensity lv. 1 = 95 ModerateIntensity // Intensity lv. 2 = 135 MediumIntensity // Intensity lv. 3 = 175 HightIntensity // Intensity lv. 4 = 215 MaxIntensity // Intensity lv. 5 = 255 )
type ColorType ¶
type ColorType interface {
ANSIColor | ExtendedANSIColor | TrueColor
// contains filtered or unexported methods
}
ColorType represents a color from any set
type ExtendedANSIColor ¶
type ExtendedANSIColor uint8
ExtendedANSIColor represents a color from the extended ANSI table (256 colors)
func GrayScale ¶
func GrayScale(grayScale uint8) ExtendedANSIColor
GrayScale pick a color from the ExtendedANSIColor table on the grayscale (1-24) plus the values of Black (0) and White (25)
Example ¶
package main
import (
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables
var (
white = brush.GrayScale(brush.MaxGrayScale)
lightGray = brush.GrayScale(brush.MaxGrayScale - 5)
gray = brush.GrayScale(brush.MaxGrayScale / 2)
black = brush.GrayScale(0)
myBrush = brush.New(brush.Black.ToExtended(), nil)
)
myBrush.UseBgColor(white).Println("Sunny")
myBrush.UseBgColor(lightGray).Println("Cloudy")
myBrush.UseBgColor(gray).Println("Rainy")
myBrush.UseBgColor(black).Println("WTF")
}
Output: �[38;5;0;48;5;15mSunny �[0m�[38;5;0;48;5;251mCloudy �[0m�[38;5;0;48;5;243mRainy �[0m�[38;5;0;48;5;0mWTF �[0m
func RGB ¶
func RGB(red, green, blue ColorIntensity) ExtendedANSIColor
RGB picks a color from the ExtendedANSIColor table by mixing for each one of the primary colors, different levels of intensity from 0 to 5.
If you want all range from 0 to 255 use TrueColor (might not be supported in your terminal)
A set of contant is declared on the package as helper, in order: ZeroIntensity, LowIntensity, ModerateIntensity, MediumIntensity, HightIntensity, MaxIntensity
Example ¶
package main
import (
"fmt"
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables
pink := brush.RGB(
brush.MaxIntensity,
brush.MediumIntensity,
brush.HightIntensity,
)
fmt.Println(brush.Paint(0, &pink, "Flamingo"))
}
Output: �[38;5;0;48;5;218mFlamingo�[0m
func (ExtendedANSIColor) ToTrueColor ¶
func (c ExtendedANSIColor) ToTrueColor() TrueColor
ToTrueColor transforms an ExtendedANSIColor to a TrueColor representation
type Highlighted ¶
type Highlighted struct {
// contains filtered or unexported fields
}
Highlighted represents a string that contains informations about the foreground and background color output of some parts of it. the styling can also be different for different subset of the content
func Join ¶
func Join(values ...any) Highlighted
Join different values together into a single item maintaining all styling
Example ¶
package main
import (
"fmt"
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables
fmt.Print(brush.Join(
brush.Paint(brush.Red, nil, "Roses are red"),
",\n",
brush.Paint(brush.Blue, nil, "Violets are blue"),
",\nSugar is sweet,\nAnd so are you.",
))
}
Output: �[31mRoses are red�[0m, �[34mViolets are blue�[0m, Sugar is sweet, And so are you.
func (*Highlighted) Append ¶
func (h *Highlighted) Append(values ...any) *Highlighted
Append lets you add some items at the end of the Highlighted content
Example ¶
package main
import (
"fmt"
"regexp"
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables
var (
marker = brush.New(brush.Black, brush.UseColor(brush.Yellow))
h = marker.Highlight("Hello world!", regexp.MustCompile("Hello"))
)
fmt.Print(h.Append(" ", marker.Paint("Hi"), " everyone!"))
}
Output: �[30;43mHello�[0m world! �[30;43mHi�[0m everyone!
func (Highlighted) String ¶
func (h Highlighted) String() string
String evaluates the content by applying the different styling where specified
type Optional ¶
type Optional[color ColorType] *color
Optional represents an optional color
func UseColor ¶
UseColor is an utility that lets you transform a color in an Optional this can be especially useful on the New or Paint function when selecting a background
Example ¶
package main
import (
"fmt"
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variable
selectedBg := brush.UseColor(brush.Magenta)
fmt.Println(brush.Paint(brush.BrightMagenta, selectedBg, "Magenta"), "is cool")
}
Output: �[95;45mMagenta�[0m is cool
type Painted ¶
type Painted struct {
// contains filtered or unexported fields
}
Painted represents a string that contains information about it's foreground and background color output
func Paint ¶
Paint some values (joined without separator) with the specified font and background color. If a Painted and/or an Highlighted item is given, they will lose their previous style and provided font and background colors will be enforced
Example ¶
package main
import (
"fmt"
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables
fmt.Println("I", brush.Paint(brush.Red, nil, "love"), "go")
}
Output: I �[31mlove�[0m go
func Paintf ¶
func Paintf[color ColorType](font color, background Optional[color], model string, values ...any) Painted
Paintf is like Paint but similarly to fmt.Sprintf it allows to use a model with some placeholders that will be replaced by the values. If a Painted and/or an Highlighted item is given, they will lose their previous style and provided font and background colors will be enforced
Example ¶
package main
import (
"fmt"
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables
blue := brush.Paintf(brush.White, brush.UseColor(brush.Blue), "%s", "blue")
fmt.Println("The sky is", blue)
}
Output: The sky is �[37;44mblue�[0m
func Paintln ¶
Paintln is like Paint but similarly to fmt.Sprintln it separates values with " " and it adds a "\n" at the end of all. If a Painted and/or an Highlighted item is given, they will lose their previous style and provided font and background colors will be enforced
Example ¶
package main
import (
"fmt"
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables
painted := brush.Paintln(brush.Black, brush.UseColor(brush.White),
"Hello", "World", "!",
)
fmt.Print(painted)
}
Output: �[30;47mHello World ! �[0m
func (*Painted) Append ¶
Append a string at the end of the content of the painted item Warning: Do not use string containing styling
Example ¶
package main
import (
"fmt"
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables
banans := brush.Paint(brush.Yellow, nil, "banana")
fmt.Println(banans.Append("s"))
}
Output: �[33mbananas�[0m
func (*Painted) Prepend ¶
Prepend a string at the start of the content of the painted item Warning: Do not use string containing styling
Example ¶
Prepend a string at the start of the content of the painted item Warning: Do not use string containing styling
package main
import (
"fmt"
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables
banana := brush.Paint(brush.Yellow, nil, "banana")
fmt.Println(banana.Prepend("yellow "))
}
Output: �[33myellow banana�[0m
func (*Painted) Replace ¶
Replace the content of the painted item with another string Is possible to use the %s to refer to embed previous content Warning: Do not use string containing styling
Example ¶
Replace the content of the painted item with another string Is possible to use the %s to refer to embed previous content Warning: Do not use string containing styling
package main
import (
"fmt"
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables
banana := brush.Paint(brush.Yellow, nil, "banana")
fmt.Println(banana.Replace(`The name "%s", is funny`))
}
Output: �[33mThe name "banana", is funny�[0m
type TrueColor ¶
type TrueColor struct {
Red, Green, Blue uint8
}
TrueColor is a true RGB color representation. Be aware that not all terminal support this format
Example ¶
package main
import (
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variable
var (
pinkish = brush.TrueColor{Red: 255, Green: 82, Blue: 197}
brownish = brush.TrueColor{155, 106, 0}
myBrush = brush.New(pinkish, &brownish)
)
myBrush.Println("HELLO WORLD")
}
Output: �[38;2;255;82;197;48;2;155;106;0mHELLO WORLD �[0m
func ParseHex ¶
ParseHex parses a hexadecimal string representing a color and returns a TrueColor pointer representing that color and an error if the provided string cannot be parsed. The hex string can be in various formats (the "#" prefix is optional):
- "RRGGBB": Represents the red, green, and blue components respectively in the range 00 to FF.
- "RGB": Represents a shorthand version of "#RRGGBB" where each component is a single digit and duplicated, e.g, "ABC" is equivalent to "AABBCC"
Example ¶
package main
import (
"fmt"
"github.com/DazFather/brush"
)
func main() {
brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variable
var (
color *brush.TrueColor
err error
)
// Example with full RGB format (# is optional)
if color, err = brush.ParseHex("#FFA500"); err == nil {
fmt.Println("Yellow:", *color)
}
// Example with shorthand RGB format (# is optional)
if color, err = brush.ParseHex("F00"); err == nil {
fmt.Println("Red:", *color)
}
// Bad examples
_, err = brush.ParseHex("blue")
fmt.Println(err)
_, err = brush.ParseHex("daz")
fmt.Println(err)
}
Output: Yellow: {255 165 0} Red: {255 0 0} Cannot parse blue color: Invalid hex length, must be 3 or 6 digits long (excluding optional prefix '#') Cannot parse daz color: strconv.ParseUint: parsing "z": invalid syntax