ansi

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2021 License: Apache-2.0 Imports: 4 Imported by: 2

Documentation

Overview

Package ansi provides tools for generating terminal output containing ANSI escape codes.

Index

Constants

View Source
const (
	// Reset is the ANSI reset sequence.
	Reset = "\033[0m"

	// EraseToEOL erases from the cursor to the end of the current line.
	EraseToEOL = "\033[K"
)

Variables

View Source
var EscapeSequencePattern = regexp.MustCompile(`(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]`)

EscapeSequencePattern is the pattern used for matching (and removing) ANSI escape sequences. From https://stackoverflow.com/questions/14693701/how-can-i-remove-the-ansi-escape-sequences-from-a-string-in-python

Functions

func CursorBack

func CursorBack(columns int) string

func CursorColumn

func CursorColumn(column int) string

CursorColumn moves the cursor to an absolute position, with the leftmost column being 1.

func CursorDown

func CursorDown(lines int) string

func CursorForward

func CursorForward(columns int) string

func CursorUp

func CursorUp(lines int) string

func Len

func Len(str string) int

Len returns the character (rune) length of str with all ANSI escape sequences removed.

func PadRight

func PadRight(str string, length int) string

PadRight pads the right side of the given string using space characters, up to length, as measured by Len.

func PadRightRune

func PadRightRune(str string, length int, padding rune) string

PadRightRune pads the right side of the given string using rune, up to length, as measured by Len.

func Strip

func Strip(str string) string

Strip returns str with all ANSI escape sequences removed.

func Truncate

func Truncate(str string, maxLength int) string

Truncate a string containing ANSI escape sequences, to maxLength characters (runes), excluding escape sequences. All escape sequences are preserved.

Types

type EscapeSequence

type EscapeSequence uint

EscapeSequence represents a basic ANSI escape sequence, in a way that allows bitwise combination. For example:

fmt.Print((Yellow | BlueBG | Bold).String())
# => \033[1;33;44m
const (
	// Black foreground
	Black EscapeSequence = iota

	// Red foreground
	Red

	// Green foreground
	Green

	// Yellow foreground
	Yellow

	// Blue foreground
	Blue

	// Magenta foreground
	Magenta

	// Cyan foreground
	Cyan

	// White foreground
	White
)
const (
	// BlackBG background text
	BlackBG EscapeSequence = iota << 3

	// RedBG background text
	RedBG

	// GreenBG background text
	GreenBG

	// YellowBG background text
	YellowBG

	// BlueBG background text
	BlueBG

	// MagentaBG background text
	MagentaBG

	// CyanBG background text
	CyanBG

	// WhiteBG background text
	WhiteBG
)
const (
	// Bright text
	Bright EscapeSequence = 1 << (iota + 6)

	// BrightBG background
	BrightBG

	// Bold text
	Bold

	// Underline text
	Underline
)

func (EscapeSequence) Bold

func (s EscapeSequence) Bold() EscapeSequence

func (EscapeSequence) Bright

func (s EscapeSequence) Bright() EscapeSequence

func (EscapeSequence) BrightBG

func (s EscapeSequence) BrightBG() EscapeSequence

func (EscapeSequence) Bytes

func (s EscapeSequence) Bytes() (bytes []byte)

Bytes converts the EscapeSequence to a byte array, for writing to a terminal.

func (EscapeSequence) IsBold

func (s EscapeSequence) IsBold() bool

func (EscapeSequence) IsBright

func (s EscapeSequence) IsBright() bool

func (EscapeSequence) IsBrightBG

func (s EscapeSequence) IsBrightBG() bool

func (EscapeSequence) IsUnderline

func (s EscapeSequence) IsUnderline() bool

func (EscapeSequence) NotBold

func (s EscapeSequence) NotBold() EscapeSequence

func (EscapeSequence) NotBright

func (s EscapeSequence) NotBright() EscapeSequence

func (EscapeSequence) NotBrightBG

func (s EscapeSequence) NotBrightBG() EscapeSequence

func (EscapeSequence) NotUnderline

func (s EscapeSequence) NotUnderline() EscapeSequence

func (EscapeSequence) String

func (s EscapeSequence) String() (str string)

String converts the EscapeSequence to a string, for writing to a terminal.

func (EscapeSequence) Underline

func (s EscapeSequence) Underline() EscapeSequence

func (EscapeSequence) Wrap

func (s EscapeSequence) Wrap(stringToWrap string) string

Wrap the given string with the escape sequence as a prefix, and Reset as a suffix.

Jump to

Keyboard shortcuts

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