ansi

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2022 License: MIT, MIT Imports: 3 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 (
	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 (
	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 (
	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 (
	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 color codes

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 ANSI text colors

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 (
	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

This section is empty.

Functions

func Example

func Example()

func NewColor

func NewColor(foreground, background, effect string) string

func NewColorMake

func NewColorMake(foreground, background, effect string) string

Types

type Any

type Any = interface{}

Directories

Path Synopsis
cmd
sample_benchmarks
fib/fib
Package fib contains sample benchmarks for Fibonacci numbers.
Package fib contains sample benchmarks for Fibonacci numbers.

Jump to

Keyboard shortcuts

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