tint: github.com/printzero/tint Index | Examples | Files

package tint

import "github.com/printzero/tint"

This document demonstrates the working of tint: a minimal bare-bone version of terminal styling implemented for Go applications with no external dependencies. It provides you with different types of functions that you can use to style your terminal output with ease.

Tint was originally created to use in the 'orbpkg' project: https://github.com/orbpkg/orb and uses near to 0ms for processing color expressions: https://godoc.org/github.com/printzero/tint/#Tint.Exp. Although the time taken to process is directly propotional to the number of characters in the string input.

Code:

// Initialize tint
t := tint.Init()
// prints output in red
t.Println("This output is red", tint.Red)

This example demonstrates how attributes are defined in colors and when and how to use those attributes. As of `v0.0.2`tint has 5 attributes: Dim, Bold, Italic, Underline, Strike (strikethrough) These attributes are functions that are exposed over the color struct.

Code:

// create pointer to Tint
t := tint.Init()
// Dim attribute
t.Println("Dim sentence.", tint.Yellow.Dim())
// Bold attribute
t.Println("Bold sentence.", tint.Yellow.Bold())
// Italic attribute
t.Println("Italic sentence.", tint.Yellow.Italic())
// Underline attribute
t.Println("Underlined sentence.", tint.Yellow.Underline())
// Strike attribute
t.Println("Strikeout sentence.", tint.Yellow.Strike())

This example demonstrates how colors are accessed differently than tint functions.

Code:

// create pointer to Tint
t := tint.Init()

// Notice: how Raw function is accessed through pointer created by Init()
// Notice: how color Yellow are accessed, they are static variables defined inside tint module
// so you don't have to create any instance of a color
t.Println("This output is red", tint.Yellow)

Index

Examples

Package Files

tint.go

Variables

var BgBlack = color{
    // contains filtered or unexported fields
}

BgBlack applies Black Background color

var BgBlue = color{
    // contains filtered or unexported fields
}

BgBlue applies Blue Background color

var BgCyan = color{
    // contains filtered or unexported fields
}

BgCyan applies Cyan Background color

var BgGreen = color{
    // contains filtered or unexported fields
}

BgGreen applies Green Background color

var BgLightGrey = color{
    // contains filtered or unexported fields
}

BgLightGrey applies Light Grey Background color

var BgMagenta = color{
    // contains filtered or unexported fields
}

BgMagenta applies Magenta Background color

var BgRed = color{
    // contains filtered or unexported fields
}

BgRed applies Red Background color

var BgWhite = color{
    // contains filtered or unexported fields
}

BgWhite applies White Background color

var BgYellow = color{
    // contains filtered or unexported fields
}

BgYellow applies Yellow Background color

var Black = color{
    // contains filtered or unexported fields
}

Black color

var Blue = color{
    // contains filtered or unexported fields
}

Blue color

var Cyan = color{
    // contains filtered or unexported fields
}

Cyan color

var Green = color{
    // contains filtered or unexported fields
}

Green color

var Magenta = color{
    // contains filtered or unexported fields
}

Magenta color

var Normal = color{
    // contains filtered or unexported fields
}

Normal equates to no style

var Red = color{
    // contains filtered or unexported fields
}

Red color

var White = color{
    // contains filtered or unexported fields
}

White color

var Yellow = color{
    // contains filtered or unexported fields
}

Yellow color

type TerminalLevel Uses

type TerminalLevel int

TerminalLevel of color support for terminal and information of current terminal that is useful to tint.

const (
    // LevelNone for terminal that supports no colot
    LevelNone TerminalLevel = iota + 1
    // Level16bit for terminal that supports 16bit colors
    Level16bit
    // Level256 for terminal that supports 256 bit colors
    Level256
    // Level16m for terminal that supports 16 million colors (truecolor)
    Level16m
)

type Tint Uses

type Tint struct {
    Level         TerminalLevel
    SupportsColor bool
    LogInstance   *log.Logger
}

Tint struct holds the whole library

func Init Uses

func Init() *Tint

Init initializes variables that tint uses and then returns the pointer to a Tint struct

func (*Tint) Exp Uses

func (t *Tint) Exp(expStr string, colors ...color) string

Exp returns a string constructed from a series of color expressions given as an argument. The colors are passed as a replacement to each word that is wrapped around `@()`.

The string "@(Hello), @(World)!" where 'Hello' is inside a tint color expression and 'World' inside another, 'Hello' will get replaced by the first color and 'World' will get replaced by the second color passed inside this function.

Take a look at the below example.

Code:

// Initialize tint
t := tint.Init()
coloredString := t.Exp("@(Hello), @(World)!", tint.White.Bold(), tint.Blue)
fmt.Println(coloredString)

func (*Tint) Log Uses

func (t *Tint) Log(text string, colors ...color)

Log text with the standard lib log module

func (*Tint) Print Uses

func (t *Tint) Print(text string, colors ...color)

Print single line of text with specified color

func (*Tint) Println Uses

func (t *Tint) Println(text string, colors ...color)

Println single line of text with enter character

func (*Tint) Raw Uses

func (t *Tint) Raw(text string, colors ...color) string

Raw returns the raw string with applied colors

Code:

// Initialize tint
t := tint.Init()
// get a colored string to be used as terminal output
output := t.Raw("Rejoice fellow gopher, your test cases have passed.", tint.Green)
// use it anywhere as input - fmt, log ...
fmt.Println(output)

func (*Tint) Swatch Uses

func (t *Tint) Swatch(colors ...color) func(text string)

Swatch will return a function for specific colors given as a parameter.

func (*Tint) SwatchRaw Uses

func (t *Tint) SwatchRaw(colors ...color) func(text string) string

SwatchRaw returns a functions that returns a raw colored string

Package tint imports 2 packages (graph). Updated 2019-06-29. Refresh now. Tools for package owners.