ansi

package module
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2023 License: MIT Imports: 4 Imported by: 9

README


A library for parsing ANSI encoded strings

CodeFactor

Go ANSI Parser converts strings with ANSI escape codes into a slice of structs that represent styled text. Features:

  • Can parse ANSI 16, 256 and TrueColor
  • Supports all styles: Regular, Bold, Faint, Italic, Blinking, Inversed, Invisible, Underlined, Strikethrough
  • Provides RGB, Hex, HSL, ANSI ID and Name for parsed colours
  • Truncation - works with emojis and grapheme clusters
  • Length - works with emojis and grapheme clusters
  • Cleanse - removes the ansi escape codes
  • Configurable colour map for customisation
  • 100% Test Coverage

Installation

go get github.com/leaanthony/go-ansi-parser

Usage

Parse
text, err := ansi.Parse("\u001b[1;31;40mHello World\033[0m")

// is the equivalent of...

text := []*ansi.StyledText{
    {
        Label: "Hello World",
        FgCol: &ansi.Col{
            Id:   9,
            Hex:  "#ff0000",
            Rgb:  &ansi.Rgb{ R: 255, G: 0, B: 0 },
            Hsl:  &ansi.Hsl{ H: 0, S: 100, L: 50 },
            Name: "Red",
        },
        BgCol: &ansi.Col{
            Id:   0,
            Hex:  "#000000",
            Rgb:  &ansi.Rgb{0, 0, 0},
            Hsl:  &ansi.Hsl{0, 0, 0},
            Name: "Black",
        },
        Style: 1,
    },
}
Truncating
shorter, err := ansi.Truncate("\u001b[1;31;40mHello\033[0m \u001b[0;30mWorld!\033[0m", 8)

// is the equivalent of...

shorter := "\u001b[1;31;40mHello\033[0m \u001b[0;30mWo\033[0m"
Cleanse
cleaner, err := ansi.Cleanse("\u001b[1;31;40mHello\033[0m \u001b[0;30mWorld!\033[0m")

// is the equivalent of...

cleaner := "Hello World!"
Length
length, err := ansi.Length("\u001b[1;31;40mHello\033[0m \u001b[0;30mWorld!\033[0m")

// is the equivalent of...

length := 12

// Works with grapheme clusters and emoji
length, err := ansi.Length("\u001b[1;31;40m👩🏽‍🔧😎\033[0m") // 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ColourMap = map[string]map[string]*Col{
	"Regular": {
		"30":  Cols[0],
		"31":  Cols[1],
		"32":  Cols[2],
		"33":  Cols[3],
		"34":  Cols[4],
		"35":  Cols[5],
		"36":  Cols[6],
		"37":  Cols[7],
		"90":  Cols[8],
		"91":  Cols[9],
		"92":  Cols[10],
		"93":  Cols[11],
		"94":  Cols[12],
		"95":  Cols[13],
		"96":  Cols[14],
		"97":  Cols[15],
		"100": Cols[8],
		"101": Cols[9],
		"102": Cols[10],
		"103": Cols[11],
		"104": Cols[12],
		"105": Cols[13],
		"106": Cols[14],
		"107": Cols[15],
	},
	"Bold": {
		"30":  Cols[8],
		"31":  Cols[9],
		"32":  Cols[10],
		"33":  Cols[11],
		"34":  Cols[12],
		"35":  Cols[13],
		"36":  Cols[14],
		"37":  Cols[15],
		"90":  Cols[8],
		"91":  Cols[9],
		"92":  Cols[10],
		"93":  Cols[11],
		"94":  Cols[12],
		"95":  Cols[13],
		"96":  Cols[14],
		"97":  Cols[15],
		"100": Cols[8],
		"101": Cols[9],
		"102": Cols[10],
		"103": Cols[11],
		"104": Cols[12],
		"105": Cols[13],
		"106": Cols[14],
		"107": Cols[15],
	},
	"Faint": {
		"30": Cols[0],
		"31": Cols[1],
		"32": Cols[2],
		"33": Cols[3],
		"34": Cols[4],
		"35": Cols[5],
		"36": Cols[6],
		"37": Cols[7],
	},
}

ColourMap maps ansi identifiers to a colour

View Source
var Cols = []*Col{}/* 256 elements not displayed */

Cols represents the default colour definitions used by the library. This may be overridden.

Functions

func Cleanse added in v1.1.0

func Cleanse(input string, options ...ParseOption) (string, error)

Cleanse removes ANSI control symbols from the string.

func HasEscapeCodes added in v1.1.0

func HasEscapeCodes(input string) bool

HasEscapeCodes tests that input has escape codes.

func Length added in v1.2.0

func Length(input string, options ...ParseOption) (int, error)

Length calculates count of user-perceived characters in ANSI string.

func String added in v1.1.0

func String(input []*StyledText) string

String builds an ANSI string for specified StyledText slice.

func Truncate added in v1.1.0

func Truncate(input string, maxChars int, options ...ParseOption) (string, error)

Truncate truncates text to length but preserves control symbols in ANSI string.

Types

type Col

type Col struct {
	Id   int    `json:"id"`
	Hex  string `json:"hex"`
	Rgb  Rgb    `json:"rgb"`
	Hsl  Hsl    `json:"hsl"`
	Name string `json:"name"`
}

Col represents a colour value. The Id is the ANSI colour ID.

type ColourMode added in v1.5.0

type ColourMode int
const (
	Default    ColourMode = 0
	TwoFiveSix ColourMode = 1
	TrueColour ColourMode = 2
)

type Hsl

type Hsl struct {
	H float64 `json:"h"`
	S float64 `json:"s"`
	L float64 `json:"l"`
}

Hsl represents the HSL value of the colour

type ParseOption added in v1.4.0

type ParseOption struct {
	// contains filtered or unexported fields
}

ParseOption specifies parse option.

func WithDefaultBackgroundColor added in v1.4.0

func WithDefaultBackgroundColor(ansiColor string) ParseOption

WithDefaultBackgroundColor specifies default foreground code (ANSI 49). See ColourMap variable and foreground color codes 30-37.

func WithDefaultForegroundColor added in v1.4.0

func WithDefaultForegroundColor(ansiColor string) ParseOption

WithDefaultForegroundColor specifies default foreground code (ANSI 39). See ColourMap variable and foreground color codes 30-37.

func WithIgnoreInvalidCodes added in v1.4.0

func WithIgnoreInvalidCodes() ParseOption

WithIgnoreInvalidCodes disables returning an error on invalid ANSI code.

type Rgb

type Rgb struct {
	R uint8 `json:"r"`
	G uint8 `json:"g"`
	B uint8 `json:"b"`
}

Rgb represents an RGB colour value with 8bits per channel

type StyledText

type StyledText struct {
	Label      string
	FgCol      *Col
	BgCol      *Col
	Style      TextStyle
	ColourMode ColourMode
	// Offset is the offset into the input string where the StyledText begins
	Offset int
	// Len is the length in bytes of the substring of the input text that
	// contains the styled text
	Len int
}

StyledText represents a single formatted string

func Parse

func Parse(input string, options ...ParseOption) ([]*StyledText, error)

Parse will convert an ansi encoded string and return a slice of StyledText structs that represent the text. If parsing is unsuccessful, an error is returned.

func (*StyledText) Blinking

func (s *StyledText) Blinking() bool

Blinking will return true if the text has a Blinking style

func (*StyledText) Bold

func (s *StyledText) Bold() bool

Bold will return true if the text has a Bold style

func (*StyledText) Bright added in v1.5.0

func (s *StyledText) Bright() bool

Bright will return true if the text has a Bright style

func (*StyledText) Faint

func (s *StyledText) Faint() bool

Faint will return true if the text has a Faint style

func (*StyledText) Inversed

func (s *StyledText) Inversed() bool

Inversed will return true if the text has an Inversed style

func (*StyledText) Invisible

func (s *StyledText) Invisible() bool

Invisible will return true if the text has an Invisible style

func (*StyledText) Italic

func (s *StyledText) Italic() bool

Italic will return true if the text has an Italic style

func (*StyledText) Strikethrough

func (s *StyledText) Strikethrough() bool

Strikethrough will return true if the text has a Strikethrough style

func (*StyledText) String added in v1.1.0

func (s *StyledText) String() string

func (*StyledText) Underlined

func (s *StyledText) Underlined() bool

Underlined will return true if the text has an Underlined style

type TextStyle

type TextStyle int

TextStyle is a type representing the ansi text styles

const (
	// Bold Style
	Bold TextStyle = 1 << 0
	// Faint Style
	Faint TextStyle = 1 << 1
	// Italic Style
	Italic TextStyle = 1 << 2
	// Blinking Style
	Blinking TextStyle = 1 << 3
	// Inversed Style
	Inversed TextStyle = 1 << 4
	// Invisible Style
	Invisible TextStyle = 1 << 5
	// Underlined Style
	Underlined TextStyle = 1 << 6
	// Strikethrough Style
	Strikethrough TextStyle = 1 << 7
	// Bright Style
	Bright TextStyle = 1 << 8
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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