chalk

package module
v0.0.0-...-c565c00 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2026 License: MIT Imports: 8 Imported by: 0

README

Chalk

chalk

chalk is a lightweight Go package for styling terminal output using ANSI colors and attributes.

Install

go get github.com/erenworld/chalk
Features
  • Foreground and background colors (FgRed, BgBlue, etc...)
  • High-intensity colors (FgHiRed, FgHiBlue, etc.)
  • Text attributes like Bold
  • Color disabling support (via NoColor or DisableColor())
  • Printable and formattable methods (Print, Printf, Sprintf, etc.)
  • Cross-platform color support using github.com/mattn/go-colorable

Examples

Standard colors
// Chain SGR parameters
color.Green.Add(color.Bold).Println("Green with bold")
color.Red.Add(color.BgWhite, color.Underline).Printf("Red with Black background and underscore: %s\n", "format too!")

// These are using by default foreground colors.
color.Red("We have red")
color.Yellow("Yellow color too!")
color.Magenta("And many others ..")

// Windows supported too! Just don't forget to change the output to color.Output
fmt.Fprintf(color.Output, "Windows support: %s", color.GreenString("PASS"))
Mix and reuse colors
d := color.New(color.FgCyan).Add(color.Underline)
d.Println("Prints bold cyan.")

// Create custom color objects:
c := color.New(color.fgGreen, color.bgCyan, color.Italic)
c.Print("Italic green with cyan background")

// Mix up foreground and background colors, create new mixes!
red := color.New(color.FgRed)

boldRed := red.Add(color.Bold)
boldRed.Println("This will print text in bold red.")

whiteBg := red.Add(color.BgWhite)
whiteBg.Println("Red text with White background.")

Insert into noncolor strings
// Create Sprint__ functions to mix strings with other non-colorized strings:
yellow := New(FgYellow).SprintFunc()
red := New(FgRed).SprintFunc()
fmt.Printf("this is a %s and this is %s.\n", yellow("warning"), red("error"))

info := New(FgWhite, BgGreen).SprintFunc()
fmt.Printf("this %s rocks!\n", info("package"))
Plug into existing code
color.Set(color.FgYellow)
fmt.Println("Existing text in your codebase will be now in Yellow")
fmt.Printf("This one %s\n", "too")
color.Unset()

color.Set(color.FgMagenta, color.Bold)
defer color.Unset()

fmt.Println("All text will be now bold magenta.")

Credits

License

The MIT License (MIT) - see LICENSE.md for more details

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	NoColor = (!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd())) || os.Getenv("TERM") == "dumb"

	// Output defines the standard output of the print functions.
	// Any io.Writer can be used.
	Output io.Writer = colorable.NewColorableStdout()

	// Error defines a color support for os.Stderr
	Error io.Writer = colorable.NewColorableStderr()
)

This is a global variable and affects all colors. For more control over each color use the method DisableColor() individually.

Functions

func Black

func Black(format string, a ...interface{})

func BlackString

func BlackString(format string, a ...interface{}) string

func Blue

func Blue(format string, a ...interface{})

func BlueString

func BlueString(format string, a ...interface{}) string

func Cyan

func Cyan(format string, a ...interface{})

func CyanString

func CyanString(format string, a ...interface{}) string

func Green

func Green(format string, a ...interface{})

func GreenString

func GreenString(format string, a ...interface{}) string

func HiBlack

func HiBlack(format string, a ...interface{})

func HiBlackString

func HiBlackString(format string, a ...interface{}) string

func HiBlue

func HiBlue(format string, a ...interface{})

func HiBlueString

func HiBlueString(format string, a ...interface{}) string

func HiCyan

func HiCyan(format string, a ...interface{})

func HiCyanString

func HiCyanString(format string, a ...interface{}) string

func HiGreen

func HiGreen(format string, a ...interface{})

func HiGreenString

func HiGreenString(format string, a ...interface{}) string

func HiMagenta

func HiMagenta(format string, a ...interface{})

func HiMagentaString

func HiMagentaString(format string, a ...interface{}) string

func HiRed

func HiRed(format string, a ...interface{})

func HiRedString

func HiRedString(format string, a ...interface{}) string

func HiWhite

func HiWhite(format string, a ...interface{})

func HiWhiteString

func HiWhiteString(format string, a ...interface{}) string

func HiYellow

func HiYellow(format string, a ...interface{})

func HiYellowString

func HiYellowString(format string, a ...interface{}) string

func Magenta

func Magenta(format string, a ...interface{})

func MagentaString

func MagentaString(format string, a ...interface{}) string

func Red

func Red(format string, a ...interface{})

func RedString

func RedString(format string, a ...interface{}) string

func Unset

func Unset()

func White

func White(format string, a ...interface{})

func WhiteString

func WhiteString(format string, a ...interface{}) string

func Yellow

func Yellow(format string, a ...interface{})

func YellowString

func YellowString(format string, a ...interface{}) string

Types

type Attribute

type Attribute int

Attribute defines a single SGR Code

const (
	Reset Attribute = iota
	Bold
	Faint
	Italic
	Underline
	BlinkSlow
	BlinkRapid
	ReverseVideo
	Concealed
	CrossedOut
)
const (
	FgBlack Attribute = iota + 30
	FgRed
	FgGreen
	FgYellow
	FgBlue
	FgMagenta
	FgCyan
	FgWhite
)

Foreground text colors

const (
	FgHiBlack Attribute = iota + 90
	FgHiRed
	FgHiGreen
	FgHiYellow
	FgHiBlue
	FgHiMagenta
	FgHiCyan
	FgHiWhite
)

Foreground Hi-Intensity text colors

const (
	BgBlack Attribute = iota + 40
	BgRed
	BgGreen
	BgYellow
	BgBlue
	BgMagenta
	BgCyan
	BgWhite
)

Background colors

const (
	BgHiBlack Attribute = iota + 100
	BgHiRed
	BgHiGreen
	BgHiYellow
	BgHiBlue
	BgHiMagenta
	BgHiCyan
	BgHiWhite
)

Background Hi-Intensity text colors

type Color

type Color struct {
	// contains filtered or unexported fields
}

Color represents a color configuration, including its attributes and an optional flag for disabling color output.

func New

func New(value ...Attribute) *Color

func Set

func Set(p ...Attribute) *Color

func (*Color) Add

func (c *Color) Add(value ...Attribute) *Color

func (*Color) Bold

func (c *Color) Bold() *Color

func (*Color) DisableColor

func (c *Color) DisableColor()

DisableColor disables the color output. Useful to not change any existing code and still being able to output. Can be used for flags like "--no-color". To enable back use EnableColor() method.

func (*Color) EnableColor

func (c *Color) EnableColor()

func (*Color) Equal

func (c *Color) Equal(c2 *Color) bool

Equals returns a boolean value indicating whether two colors are equals.

func (*Color) Fprint

func (c *Color) Fprint(w io.Writer, a ...interface{}) (n int, err error)

On Windows, users should wrap w with colorable.NewColorable() if w is of type *os.File.

func (*Color) FprintFunc

func (c *Color) FprintFunc() func(w io.Writer, a ...interface{})

func (*Color) Fprintf

func (c *Color) Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)

func (*Color) FprintfFunc

func (c *Color) FprintfFunc() func(w io.Writer, format string, a ...interface{})

func (*Color) Fprintln

func (c *Color) Fprintln(w io.Writer, a ...interface{}) (n int, err error)

func (*Color) FprintlnFunc

func (c *Color) FprintlnFunc() func(w io.Writer, a ...interface{})

func (*Color) Print

func (c *Color) Print(a ...interface{}) (n int, err error)

func (*Color) PrintFunc

func (c *Color) PrintFunc() func(a ...interface{})

func (*Color) Printf

func (c *Color) Printf(format string, a ...interface{}) (n int, err error)

func (*Color) PrintfFunc

func (c *Color) PrintfFunc() func(format string, a ...interface{})

func (*Color) Println

func (c *Color) Println(a ...interface{}) (n int, err error)

func (*Color) PrintlnFunc

func (c *Color) PrintlnFunc() func(a ...interface{})

func (*Color) Set

func (c *Color) Set() *Color

func (*Color) Sprint

func (c *Color) Sprint(a ...interface{}) string

Sprint is just like Print, but returns a string instead of printing it.

func (*Color) SprintFunc

func (c *Color) SprintFunc() func(a ...interface{}) string

func (*Color) Sprintf

func (c *Color) Sprintf(format string, a ...interface{}) string

Sprintf is just like Printf, but returns a string instead of printing it.

func (*Color) SprintfFunc

func (c *Color) SprintfFunc() func(format string, a ...interface{}) string

func (*Color) Sprintln

func (c *Color) Sprintln(a ...interface{}) string

Sprintln is just like Println, but returns a string instead of printing it.

func (*Color) SprintlnFunc

func (c *Color) SprintlnFunc() func(a ...interface{}) string

Jump to

Keyboard shortcuts

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