ansi

package
v0.0.0-...-24ca9bf Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2022 License: MIT, MIT Imports: 5 Imported by: 0

README

ansi

Tricky and fun utilities for Go programs.


GitHub Workflow Status Codecov

Contributor Covenant

Twitter Follow GitHub followers


Getting Started

Prerequisites

Developed with $( go version; ). Go is extremely backwards compatible and semver stable. Nearly any v1.x should work fine.


Installation

To use this repo as a template for your own project:

gh repo create -y --public --template "https://github.com/skeptycal/ansi"

Clone this repo to test and contribute:

# add repo to $GOPATH (xxxxxx is your computer login username)
go get github.com/xxxxxx/ansi

cd ${GOPATH}/src/github.com/xxxxxx/ansi

# test results and coverage info
./go.test.sh

# install as a utility package
go install

Use the Issues and PR templates on the GitHub repo page to contribute.


Basic Usage

This is a copy of the example script available in the cmd/example/ansi folder:

package main

import "github.com/skeptycal/ansi"

func main() {
    ansi.Example()
}

To try it out:

# change to the sample folder
cd cmd/example/ansi

# run the main.go program
go run ./main.go

# to compile as an executable
go build

Code of Conduct and Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us. Please read the Code of Conduct for details before submitting anything.


Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.


Contributors and Inspiration

  • Michael Treanor (GitHub / Twitter) - Initial work, updates, maintainer
  • Francesc Campoy - Inspiration and great YouTube videos!

See also the list of contributors who participated in this project.


License

Licensed under the MIT https://opensource.org/licenses/MIT - see the LICENSE file for details.

Documentation

Overview

Package ansi provides an extensive set of ANSI escape codes for use in command line interfaces (CLIs) using industry standard well-documented terminal commands.

Index

Constants

View Source
const (
	BlackText          string = "\033[30m"
	RedText            string = "\033[31m"
	GreenText          string = "\033[32m"
	YellowText         string = "\033[33m"
	BlueText           string = "\033[34m"
	PurpleText         string = "\033[35m"
	CyanText           string = "\033[36m"
	WhiteText          string = "\033[37m"
	DefaultColorText   string = "\033[39m" // Normal foreground color
	BgBlackText        string = "\033[40m"
	BgRedText          string = "\033[41m"
	BgGreenText        string = "\033[42m"
	BgYellowText       string = "\033[43m"
	BgBlueText         string = "\033[44m"
	BgPurpleText       string = "\033[45m"
	BgCyanText         string = "\033[46m"
	BgWhiteText        string = "\033[47m"
	BhDefaultColorText string = "\033[49m" // Normal background color
)

Premade ANSI basic 3 bit ANSI color strings

Reference: https://en.wikipedia.org/wiki/ANSI_escape_code

View Source
const (
	BoldText         string = "\033[1m"
	BoldBlackText    string = "\033[1;30m"
	BoldRedText      string = "\033[1;31m"
	BoldGreenText    string = "\033[1;32m"
	BoldYellowText   string = "\033[1;33m"
	BoldBlueText     string = "\033[1;34m"
	BoldMagentaText  string = "\033[1;35m"
	BoldCyanText     string = "\033[1;36m"
	BoldWhiteText    string = "\033[1;37m"
	FaintText        string = "\033[2m"
	FaintBlackText   string = "\033[2;30m"
	FaintRedText     string = "\033[2;31m"
	FaintGreenText   string = "\033[2;32m"
	FaintYellowText  string = "\033[2;33m"
	FaintBlueText    string = "\033[2;34m"
	FaintMagentaText string = "\033[2;35m"
	FaintCyanText    string = "\033[2;36m"
	FaintWhiteText   string = "\033[2;37m"
)

Premade bold and dim 3 bit ANSI color strings

Reference: https://en.wikipedia.org/wiki/ANSI_escape_code

View Source
const (
	BLACK = iota
	RED
	GREEN
	YELLOW
	BLUE
	MAGENTA
	CYAN
	WHITE
)

All possible colors (3 bit ANSI)

View Source
const (
	Reset          string = "\033[0m"  // ANSI reset code
	ResetColor     string = "\033[32m" // Reset to default color
	ResetLineConst string = "\r\033[K" // Return cursor to start of line and clean it
	SetBold        string = "\033[1m"  // ANSI bold
	SetInverse     string = "\033[4m"  // ANSI inverse
)
View Source
const (
	NUL byte = iota //  0 = Null
	SOH             //	1 = Start of Heading
	STX             //	2 = Start of Text
	ETX             //	3 = End of Text
	EOT             //	4 = End of Transmission
	ENQ             //	5 = Enquiry
	ACK             //	6 = Acknowledge
	BEL             //	7 = Bell
	BS              //	8 = Backspace
	TAB             //	9 = Horizontal Tab
	LF              //	10 = Line Feed
	VT              //	11 = Vertical Tab
	FF              //	12 = Form Feed
	CR              //	13 = Carriage Return
	SO              //	14 = Shift Out
	SI              //	15 = Shift In
	DLE             //	16 = Data Link Escape
	DC1             //	17 = Device Control 1
	DC2             //	18 = Device Control 2
	DC3             //	19 = Device Control 3
	DC4             //	20 = Device Control 4
	NAK             //	21 = Negative Acknowledgement
	SYN             //	22 = Synchronous Idle
	ETB             //	23 = End of Transmission Block
	CAN             //	24 = Cancel
	EM              //	25 = End of Medium
	SUB             //	26 = Substitute
	ESC             //	27 = Escape
	FS              //	28 = File Separator
	GS              //	29 = Group Separator
	RS              //	30 = Record Separator
	US              //	31 = Unit Separator
)

Ansi Control Codes provide ASCII representation of nonprintable characters.

View Source
const (
	Normal byte = iota
	Bold        // bold or increased intensity
	Faint       // faint, decreased intensity or second color
	Italics
	Underline
	Blink
	FastBlink
	Inverse
	Conceal
	Strikeout
	// ANSI codes above 9 are very rare and many are not fully implemented.
	PrimaryFont
	AltFont1
	AltFont2
	AltFont3
	AltFont4
	AltFont5
	AltFont6
	AltFont7
	AltFont8
	AltFont9
	Gothic // fraktur
	DoubleUnderline
	NormalColor // normal color or normal intensity (neither bold nor faint)
	NotItalics  // not italicized, not fraktur
	NotUnderlined
	Steady     // not Blink or FastBlink
	Reserved26 // reserved for proportional spacing as specified in CCITT Recommendation T.61
	NotInverse // Positive
	NotHidden  // Revealed
	NotStrikeout
	Black
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	White
	SetForeground     // Next arguments are 5;n or 2;r;g;b, see below
	DefaultForeground // default display color (implementation-defined)
	BlackBackground
	RedBackground
	GreenBackground
	YellowBackground
	BlueBackground
	MagentaBackground
	CyanBackground
	WhiteBackground
	SetBackground              // Next arguments are 5;n or 2;r;g;b, see below
	DefaultBackground          // default background color (implementation-defined)
	DisableProportionalSpacing // reserved for cancelling the effect of parameter value 26
	Framed
	Encircled
	Overlined
	NotFramed // NotEncircled
	NotOverlined
	Reserved56
	Reserved57
	SetUnderlineColor // Next arguments are 5;n or 2;r;g;b, see below
	DefaultUnderlineColor
	IdeogramUnderline       // ideogram underline or right side line
	IdeogramDoubleUnderline // ideogram double underline or double line on the right side
	IdeogramOverline        // ideogram overline or left side line
	IdeogramDoubleOverline  // ideogram double overline or double line on the left side
	IdeogramStress          // ideogram stress marking
	IdeogramCancel          // reset the effects of all of 60–64
	Superscript             = 73
	Subscript               = 74
)

ANSI escape codes for text effects

These are the most commonly used. ANSI codes above 9 are very rare and many are not fully implemented.

Normal      = 0
Bold        = 1
Faint       = 2
Italics     = 3
Underline   = 4
Inverse     = 7
Conceal     = 8
Strikeout   = 9
View Source
const (
	FmtANSI           string = "\033[%dm"
	FmtANSIFG         string = "\033[3%dm"
	FmtANSIBG         string = "\033[4%dm"
	FmtANSIWithEffect string = "\033[%d;%dm"
	FmtANSIBright     string = "\033[1;%dm"
	FmtANSIDim        string = "\033[2;%dm"
)

Format Strings - 3/4 bit Ansi printf commands.

ESC[⟨e⟩;⟨x⟩⟨n⟩m

Select 3/4 bit color

n in [0..7]; basic colors

(x in [3, 4]); 3 = foreground; 4 = background

e can be any valid ANSI escape effect; these are common:

0 Normal            // may be left out to maintain current ANSI effect
1 Bold              // bold or increased intensity
2 Faint             // faint, decreased intensity or second color
3 Italics
4 Underline
7 Inverse
8 Conceal
9 Strikeout
View Source
const (
	Fmt8bit   string = "\033[%d8;5;%dm"
	Fmt8bitFG string = "\033[38;5;%dm"
	Fmt8bitBG string = "\033[48;5;%dm"
)
  • Select 8bit color

Format Strings 8 bit Ansi printf commands.

ESC[⟨x⟩8:5:⟨n⟩m

Select 8bit color

n in [0..255]; 0-231 are colors; 232-255 are grayscale

(x in [3, 4]); 3 = foreground; 4 = background

View Source
const (
	Fmt24bit   string = "\033[%d8;2;%d;%d;%dm"
	Fmt24bitFG string = "\033[38;2;%d;%d;%dm"
	Fmt24bitBG string = "\033[48;2;%d;%d;%dm"
)
  • Select RGB color

Format Strings 24 bit Ansi printf commands.

ESC[⟨x⟩8;2;⟨R⟩;⟨G⟩;⟨B⟩m

Select RGB color

R, G, B in [0..255]

(x in [3, 4]); 3 = foreground; 4 = background

View Source
const (
	SymCommand            string = `⌘`
	SymPlusMinus          string = `±` // Option Shift =
	SymDegree             string = `°` // Option Shift 8
	SymSquared            string = `²` // (no default macOS keymapping)
	SymCubed              string = `³` // (no default macOS keymapping)
	SymUpsideDownQuestion string = `¿` // Option Shift /
	SymCopyright          string = `©` // Option g
	SymRegistered         string = `®` // Option r
	SymTrademark          string = `™` // Option 2
	SymSummation          string = `∑` // Option w
	SymFrontTick          string = `´` // Option e
	SymDager              string = `†` // Option t
	SymEyes               string = `¨` // Option u
	SymHighCarrot         string = `ˆ` // Option i
	SymPhi                string = `ø` // Option o
	SymDiameter           string = `⌀` // (no default macOS keymapping)
	SymSlashO             string = `Ø`
	SymSmallSlashO        string = `ø`
	SymEmptySet           string = `∅`
)

Variables

View Source
var (
	ResetBytes   []byte = []byte(Reset)
	BoldBytes    []byte = []byte(SetBold)
	InverseBytes []byte = []byte(SetInverse)
)
View Source
var NewColor = newColorConcat

NewColor returns a new ansi color string function.

Several methods were profiled and the most efficient was aliased to NewColor. YMMV; change the alias as needed for your environment.

Functions

func BasicEncode

func BasicEncode(in string) string

func Example

func Example()

func NewColorMake

func NewColorMake(foreground, background, effect string) string

Types

type ANSI

type ANSI interface {
	String() string
}

ANSI implements the interface for ANSI encoded values typically used for control and output in CLI applications.

func NewAnsiColor

func NewAnsiColor(in string) ANSI

type AnsiColor

type AnsiColor struct {
	Color string
	// contains filtered or unexported fields
}

AnsiColor is a buffered, encoded ANSI color string typically used for CLI output. The encoded ANSI color code string is JIT buffered at the time of the first output request to eliminate repeated fmt.Sprintf (or similar) calls.

func (AnsiColor) String

func (a AnsiColor) String() string

String returns the ANSI formatted string representation of the AnsiColor byte.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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