chalk: Index | Files | Directories

package chalk

import ""

Package chalk is a package for styling terminal/console output. There are three main components:


There are eight default colors: black, red, green, yellow, blue, magenta, cyan and white. You can use them in two main ways (note the need for the reset color if you don't use Color()):

fmt.Println(chalk.Red, "this is red text", chalk.ResetColor)
fmt.Println(chalk.Red.Color("this is red text")


There are seven default text styles: bold, dim, italic, underline, inverse, hidden and strikethrough. Unlike colors, you should only really use TextStyles in the following manner:

fmt.Println(chalk.Bold.TextStyle("this is bold text"))


Styles are where all the business really is. Styles can have a foreground color, a background color and a text style (sweet!). They're also pretty simply to make, you just need a starting point:

blue := chalk.Blue.NewStyle()
bold := chalk.Bold.NewStyle()

When a color is your starting point for a style, it will be the foreground color, when a style is your starting point, well, yeah, it's your style's text style. You can also alter a style's foreground, background or text style in a builder-esque pattern.

blueOnWhite := blue.WithBackground(chalk.White)
awesomeness := blueOnWhite.WithTextStyle(chalk.Underline).WithForeground(chalk.Green)

Like both Colors and TextStyles you can style specific segments of text with:

fmt.Println(awesomeness.Style("this is so pretty!"))

Like Colors, you can also print styles explicitly, but you'll need to reset your console's colors with chalk.Reset if you use them this way:

fmt.Println(awesomeness, "this is so pretty", chalk.Reset)

Be aware though, that this (second) way of using styles will not add the text style (as text styles require more specific end codes). So if you want to fully utilize styles, use myStyle.Style() (unless you only care about print your text with a specific foreground and background, then printing the style is awesome too!).

Have fun!


Package Files

chalk.go doc.go


var (
    Black      = Color{0}
    Red        = Color{1}
    Green      = Color{2}
    Yellow     = Color{3}
    Blue       = Color{4}
    Magenta    = Color{5}
    Cyan       = Color{6}
    White      = Color{7}
    ResetColor = Color{9}

    Bold          = TextStyle{1, 22}
    Dim           = TextStyle{2, 22}
    Italic        = TextStyle{3, 23}
    Underline     = TextStyle{4, 24}
    Inverse       = TextStyle{7, 27}
    Hidden        = TextStyle{8, 28}
    Strikethrough = TextStyle{9, 29}

    Reset = &style{
        foreground: ResetColor,
        background: ResetColor,

type Color Uses

type Color struct {
    // contains filtered or unexported fields

Color represents one of the ANSI color escape codes.

func (Color) Color Uses

func (c Color) Color(val string) string

Color colors the foreground of the given string (whatever the previous background color was, it is left alone).

func (Color) NewStyle Uses

func (c Color) NewStyle() Style

NewStyle creates a style with a foreground of the color we're creating the style from.

func (Color) String Uses

func (c Color) String() string

func (Color) Value Uses

func (c Color) Value() int

Value returns the individual value for this color (Actually it's really just its index in the list of color escape codes with the list being [black, red, green, yellow, blue, magenta, cyan, white].

type Style Uses

type Style interface {
    // Foreground sets the foreground of the style to the specific color.
    // Background sets the background of the style to the specific color.
    // Style styles the given string with the current style.
    Style(string) string
    // WithBackground allows us to set the background in a builder
    // pattern style.
    WithBackground(Color) Style
    // WithForeground allows us to set the foreground in a builder
    // pattern style.
    WithForeground(Color) Style
    // WithStyle allows us to set the text style in a builder pattern
    // style.
    WithTextStyle(TextStyle) Style
    String() string

A Style is how we want our text to look in the console. Consequently, we can set the foreground and background to specific colors, we can style specific strings and can also use this style in a builder pattern should we wish (these will be more useful once styles such as italics are supported).

type TextStyle Uses

type TextStyle struct {
    // contains filtered or unexported fields

A TextStyle represents the ways we can style the text: bold, dim, italic, underline, inverse, hidden or strikethrough.

func (TextStyle) NewStyle Uses

func (t TextStyle) NewStyle() Style

NewStyle creates a style starting with the current TextStyle as its text style.

func (TextStyle) String Uses

func (t TextStyle) String() string

NOTE: this function specifically does not work as desired because text styles must be wrapped around the text they are meant to style. As such, use TextStyle() or Style.Style() instead.

func (TextStyle) TextStyle Uses

func (t TextStyle) TextStyle(val string) string

TextStyle styles the given string using the desired text style.



Package chalk imports 1 packages (graph) and is imported by 162 packages. Updated 2018-10-12. Refresh now. Tools for package owners.