colorstr

package
v1.16.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package colorstr interprets the input format containing color tokens like `[blue]hello [red]world` as the text "hello world" in two colors.

Just like tokens in the fmt package (e.g. '%s'), color tokens will only be effective when specified as the format parameter. Tokens not in the format parameter will not be interpreted.

colorstr.DefaultTokens.Printf("[blue]hello")   ==> (a blue hello)
colorstr.DefaultTokens.Printf("[ahh]")         ==> "[ahh]"

Color tokens in the Print arguments will never be interpreted. It can be useful to pass user inputs there.

Index

Constants

This section is empty.

Variables

View Source
var DefaultTokens = (func() colorTokens {
	colors := make(map[string]string)
	maps.Copy(colors, colorstring.DefaultColors)
	return colorTokens{
		Colorize: colorstring.Colorize{
			Colors:  colors,
			Disable: false,
			Reset:   true,
		},
	}
})()

DefaultTokens uses default color tokens.

Functions

func ColorEnabled added in v1.16.5

func ColorEnabled() bool

ColorEnabled reports whether ANSI styling output is enabled globally.

func Fprintf

func Fprintf(w io.Writer, format string, a ...any) (n int, err error)

Fprintf is a convenience wrapper for fmt.Fprintf with support for color codes. Only color codes in the format param will be respected.

func Printf

func Printf(format string, a ...any) (n int, err error)

Printf is a convenience wrapper for fmt.Printf with support for color codes. Only color codes in the format param will be respected.

func SetColorEnabled added in v1.16.5

func SetColorEnabled(enabled bool)

SetColorEnabled sets whether ANSI styling output is enabled globally.

This matches the behavior of common CLI color libraries (e.g. github.com/fatih/color): the decision is made once and then reused across all output.

func Sprintf

func Sprintf(format string, a ...any) string

Sprintf is a convenience wrapper for fmt.Sprintf with support for color codes. Only color codes in the format param will be respected.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL