tint

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2019 License: Apache-2.0 Imports: 2 Imported by: 3

README

Tint aims to provide functions that you can use to color your cli text output. This project is originally written to be used in the project called orb which I'm currently working on. It resolves some of my concerns about the state of terminal styling implemented in Go.

GoDoc

Import

go get github.com/printzero/tint

Getting started

The below topics will get you started with the basics of this module and also some of the features that it provides.

Initialization

Initialize a pointer to Tint struct

t := tint.Init()

Functions

Tint has some functions that let you directly color your terminal output, Example:

t.Println("This is an error", t.Red)

this function is equivalent to fmt.Println(), but notice the extra parameter passed. This is a color constant.

A lot of times we need to use the raw string representation of colored string for various purposes and this is where t.Raw is used. Example:

err := writeCode()

if err != nil {
	panic(err)
}

func writeCode() error {
	return errors.New(t.Raw("This is a very bad error and it occurred while coding", tint.Red))
}

Higher-order color functions

It is possible with this module to create higher order functions with the help of Swatch. A swatch returns a function for a given color variable and print as well, Example:

package main

import (
	"time"
	"tint"
	)

var green func(text string)
var yellow func(text string)

func main() {
	t := tint.Init()
	
	green = t.Swatch(tint.Green)
	yellow = t.Swatch(tint.Yellow)
	
	yellow("Waiting for 2 seconds just for fun ...")
	time.Sleep(time.Second * 2)
	green("I give you a green light")
}

It is also possible for a swatch function to return a raw colored string using SwatchRaw but does not print it to stdout. So the above example becomes:

package main

import (
   "fmt"
   "time"
   "tint"
   )

var green func(text string) string
var yellow func(text string) string

func main() {
   t := tint.Init()
   green = t.SwatchRaw(tint.Green)
   yellow = t.SwatchRaw(tint.Yellow)
   y := yellow("Waiting for 2 seconds just for fun ...")
   fmt.Printf("Yellow string: %s", y)
   time.Sleep(time.Second * 2)
   g := green("I give you a green light")
   fmt.Printf("Green string: %s", g)
}

Contributing to this project

Take a look at this CONTIBUTING.md.

Github Project Board

I use this github project board to track changes for this project and any new feature, bug or ideas for upcoming releases are posted here.

License

This project is licensed under Apache 2.0

Documentation

Overview

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.

Example
package main

import (
	"tint"
)

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

Example (Attributes)

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.

package main

import (
	"tint"
)

func main() {
	// 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())
}
Output:

Example (Difference)

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

package main

import (
	"tint"
)

func main() {
	// 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)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

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

BgBlack applies Black Background color

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

BgBlue applies Blue Background color

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

BgCyan applies Cyan Background color

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

BgGreen applies Green Background color

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

BgLightGrey applies Light Grey Background color

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

BgMagenta applies Magenta Background color

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

BgRed applies Red Background color

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

BgWhite applies White Background color

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

BgYellow applies Yellow Background color

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

Black color

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

Blue color

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

Cyan color

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

Green color

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

Magenta color

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

Normal equates to no style

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

Red color

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

White color

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

Yellow color

Functions

This section is empty.

Types

type TerminalLevel

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

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

Tint struct holds the whole library

func Init

func Init() *Tint

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

func (*Tint) Exp

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.

Example
package main

import (
	"fmt"
	"tint"
)

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

func (*Tint) Log

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

Log text with the standard lib log module

func (*Tint) Print

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

Print single line of text with specified color

func (*Tint) Println

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

Println single line of text with enter character

func (*Tint) Raw

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

Raw returns the raw string with applied colors

Example
package main

import (
	"fmt"
	"tint"
)

func main() {
	// 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)
}
Output:

func (*Tint) Swatch

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

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

func (*Tint) SwatchRaw

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

SwatchRaw returns a functions that returns a raw colored string

Jump to

Keyboard shortcuts

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