Categorygithub.com/leaanthony/go-ansi-parser
modulepackage
1.6.1
Repository: https://github.com/leaanthony/go-ansi-parser.git
Documentation: pkg.go.dev

# 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

# Packages

No description provided by the author

# Functions

Cleanse removes ANSI control symbols from the string.
HasEscapeCodes tests that input has escape codes.
Length calculates count of user-perceived characters in ANSI string.
Parse will convert an ansi encoded string and return a slice of StyledText structs that represent the text.
String builds an ANSI string for specified StyledText slice.
Truncate truncates text to length but preserves control symbols in ANSI string.
WithDefaultBackgroundColor specifies default foreground code (ANSI 49).
WithDefaultForegroundColor specifies default foreground code (ANSI 39).
WithIgnoreInvalidCodes disables returning an error on invalid ANSI code.

# Constants

Blinking Style.
Bold Style.
Bright Style.
No description provided by the author
Faint Style.
Inversed Style.
Invisible Style.
Italic Style.
Strikethrough Style.
No description provided by the author
No description provided by the author
Underlined Style.

# Variables

ColourMap maps ansi identifiers to a colour.
Cols represents the default colour definitions used by the library.

# Structs

Col represents a colour value.
Hsl represents the HSL value of the colour.
ParseOption specifies parse option.
Rgb represents an RGB colour value with 8bits per channel.
StyledText represents a single formatted string.

# Type aliases

No description provided by the author
TextStyle is a type representing the ansi text styles.