Skip to content

logo

Tint - a minimal terminal style module for go

Tint let's you implement terminal styling for your projects. It helps you construct higher-order functions as well as construct raw colored text for your use-case.

Getting started

The below code snippets provides you with a general understanding of how this module works.

Initialize Tint

The first step is to always initialize tint with Init function, like this:

t := tint.Init()

You can follow the further tutorial assuming that this is done.

Colored text with Println

Tint module has it's own Println function that is equivalent to fmt.Println. It takes in tint's color as spreaded argument after text.

t.Println("Welcome to tint go module.", tint.Magenta)

Output: eg1

Colored text with Print

Tint module also has it's fmt.Print equivalent.

t.Print("Welcome, ", tint.Yellow)
t.Print("to tint go module\n", tint.Dim)

Output: eg2

Raw returns a colored stirng

Tint has a function called Raw which returns a colored string that you can use with other functions that expects string.

printThis :=  t.Raw("t.Raw returns a raw ansi colored string", tint.BgMagenta)
fmt.Println(printThis)

Output: eg3

Set backgrounds and foregroud inside same function

Using the example above you can pass a foreground text color in any of the tint functions to set color of background and foreground at the same time.

printThis :=  t.Raw("t.Raw returns a raw ansi colored string", tint.BgMagenta, tint.Black)
fmt.Println(printThis)

Output: eg4

Use stdlib log function with Log

For ease of use tint has a function called Log to log the colored text using the standrard library.

t.Log("Something is awefully wrong here", tint.Red)

Output: eg5

Create a colored function with Swatch

Tint has a magic function called Swatch. It returns a higher order function that always prints a output with same color. A example usage is shown below

var warn func(s string)

func main() {
    warn = t.Swatch(tint.Yellow)
    warn("This is a warning function using t.Swatch")
}

Output: eg6

Tint's Palette and With

To create a sentence with different colors you can use Palette to combine and With to create a Mixin of text and color. Example:

    coloredString := t.Palette(
        t.With("Welcome", tint.BgRed),
        t.With("to this", tint.Normal),
        t.With("awesome", tint.Cyan),
        t.With("module created by", tint.Normal),
        t.With("codekidX", tint.BgGreen, tint.Black),
    )
    fmt.Println(coloredString)

Output: eg7

Have some ideas?

You can contribute and participate by reading this CONTRIBUTING.md.

Hope you enjoy using this module, leave comments on what you think about this module.

Comments