github.com/kortschak/ct

Terminal text decoration


License
BSD-3-Clause
Install
go get github.com/kortschak/ct

Documentation

ct

import "github.com/kortschak/ct"

Package ct provides ANSI terminal text coloring and decoration support.

type Color

type Color int

Color is a basic color list selector. Not all colors are available on all platforms.

const (
    Black Color = iota
    Red
    Green
    Yellow
    Blue
    Magenta
    Cyan
    LightGray
    Gray
    BoldRed
    BoldGreen
    BoldYellow
    BoldBlue
    BoldMagenta
    BoldCyan
    White
)

type Mode

type Mode uint64

Mode specifies terminal rendering modes via its Render method. Modes should be bitwise or'd together. Not all modes are available on all platforms.

A Mode is essentially a bit field of colors and decoration flags. The layout of a Mode is as follows:

bits 0-3    - color of foreground
bit 4       - set foreground color
bits 5-7    - color of background
bit 8       - set background color
bits 9-16   - XTerm color of foreground
bit 17      - set XTerm foreground color
bits 18-25  - XTerm color of background
bit 26      - set XTerm background color
bits 27-    - text decoration flags
highest bit - do not reset terminal after rendering

XTerm colors take priority over normal terminal colors if supported by the platform.

const (
    Reset Mode = 1 << (iota + colorBits)
    Bold
    Faint
    Italic
    Underline
    BlinkSlow
    BlinkRapid
    Negative
    Conceal
    CrossedOut
    NoResetAfter Mode = 1 << 63
)

func Bg

func Bg(c Color) Mode

Bg returns a background color Mode based on the provided Color.

func Fg

func Fg(c Color) Mode

Fg returns a foreground color Mode based on the provided Color.

func XTermBg

func XTermBg(c byte) Mode

XTermBg returns an XTerm background color Mode based on the provided XTerm color.

func XTermFg

func XTermFg(c byte) Mode

XTermFg returns an XTerm foreground color Mode based on the provided XTerm color.

func (Mode) Paint

func (m Mode) Paint(v ...interface{}) fmt.Formatter

Paint returns a fmt.Formatter that will apply the mode to the printed values of the parameters.

package main

import (
    "fmt"
    "github.com/kortschak/ct"
)

var (
    info = (ct.Fg(ct.Black) | ct.XTermFg(16) | ct.Bold).Paint
    warn = (ct.Fg(ct.White) | ct.Bg(ct.Red)).Paint
)

func main() {
    fmt.Println(warn("WARNING:"), "Danger, Will Robinson! Danger! ")
    fmt.Println(info("INFO:"), "Doctor Smith, please. You're making the Robot very unhappy!")
}

Generated by godoc2md