A library for parsing ANSI encoded strings


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
  • Configurable colour map for customisation
  • 100% Test Coverage

Installation

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

Usage

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

// is the equivalent of...

var 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,
    },
}
Owner
Lea Anthony
Creator of Wails: https://wails.app. Co-creator of xbar: https://getxbar.com. Hardcore Gopher.
Lea Anthony
Similar Resources

Golang source code parsing, usage like reflect package

gotype Golang source code parsing, usage like reflect package English 简体中文 Usage API Documentation Examples License Pouch is licensed under the MIT Li

Dec 9, 2022

Small utility to allow simpler, quicker testing of parsing files in crowdsec

cs_parser_test Small utility to allow simpler, quicker testing of parsing files in crowdsec Usage $ sudo cs_parser_test -t syslog /var/log/mail.log N

Jul 13, 2021

Gene parsing package for Axie Infinity

agp Package agp is a gene parsing package for Axie Infinity. The name agp stands for "Axie Gene Parser" which decodes the hex representation of an Axi

Apr 18, 2022

Go-path - A helper package that provides utilities for parsing and using ipfs paths

go-path is a helper package that provides utilities for parsing and using ipfs paths

Jan 18, 2022

Govalid is a data validation library that can validate most data types supported by golang

Govalid is a data validation library that can validate most data types supported by golang. Custom validators can be used where the supplied ones are not enough.

Apr 22, 2022

Maintain a lower-bitrate copy of a music library in sync with the main copy.

msync Maintain a lower-bitrate copy of your music library, in sync with the main copy.

Mar 6, 2022

Golang library to act on structure fields at runtime. Similar to Python getattr(), setattr(), hasattr() APIs.

go-attr Golang library to act on structure fields at runtime. Similar to Python getattr(), setattr(), hasattr() APIs. This package provides user frien

Dec 16, 2022

Go library for HTTP content type negotiation

Content-Type support library for Go This library can be used to parse the value Content-Type header (if one is present) and select an acceptable media

Jul 10, 2022

A tool and library for using structural regular expressions.

Structural Regular Expressions sregx is a package and tool for using structural regular expressions as described by Rob Pike (link).

Dec 7, 2022
Comments
  • str != Parse.(str).String() when color ID is in [8,16)

    str != Parse.(str).String() when color ID is in [8,16)

    Prologue

    Thank you for this fantastic library! It saves me bunch of time :) Also, I'm not an expert of ANSI escape sequences, so it's highly possible I'm missing something, and please don't hesitate to point that out, thanks!

    Issue

    The issue is caused by this block. For example, when the list of parameters is 38;5;15 (i.e., Foreground Color Code: White), it gets converted to 45 (i.e., Background Color Code: Magenta). The meaning of the parameters is drew from here.

    I assume that this behavior is not wanted because parsing a string and converting the result back to a string should not change the appearance of it.

    Example

    I'm referring to something like the following part in the Processed image:

    image

    Original

    image

    Processed

    Parse it, do nothing to it, and print it.

    image

    Notes

    For the references, I generate the original .ans file by uploading this image to ASCII Art Converter.

  • Add fields to StyledText for offset and length in input string

    Add fields to StyledText for offset and length in input string

    This allows the caller to find out what part of the input string the StyledText corresponds to. It's useful when you have some text you want to display as-is, but styled with colors.

  • Options specifies how to parse

    Options specifies how to parse

    Added possibility to specify parse options while calling global module functions, e.g. Parse().

    • WithIgnoreInvalidCodes - switches off the error on unexpected ANSI code;
    • WithDefaultForegroundColor - allows us to specify default foreground color (code 39);
    • WithDefaultBackgroundColor - allows us to specify default background color (code 49).
  • Add license scan report and status

    Add license scan report and status

    Your FOSSA integration was successful! Attached in this PR is a badge and license report to track scan status in your README.

    Below are docs for integrating FOSSA license checks into your CI:

A program to create assembly 8086 strings to print without using any printing/strings related function but only mov-xchg-int and loops

Assembly String builder tool A program to create assembly 8086 strings to print without using any printing/strings related function but only mov-xchg-

Feb 1, 2022
Raw ANSI sequence helpers

Raw ANSI sequence helpers

Oct 23, 2022
Color lets you use colorized outputs in terms of ANSI Escape Codes in Go (Golang)
Color lets you use colorized outputs in terms of ANSI Escape Codes in Go (Golang)

Color lets you use colorized outputs in terms of ANSI Escape Codes in Go (Golang). It has support for Windows too! The API can be used in several ways, pick one that suits you.

Feb 5, 2022
Molecule is a Go library for parsing protobufs in an efficient and zero-allocation manner

Molecule Molecule is a Go library for parsing protobufs in an efficient and zero-allocation manner. The API is loosely based on this excellent Go JSON

Jan 5, 2023
A Go (golang) library for parsing and verifying versions and version constraints.

go-version is a library for parsing versions and version constraints, and verifying versions against a set of constraints. go-version can sort a collection of versions properly, handles prerelease/beta versions, can increment versions, etc.

Jan 9, 2023
A small & fast dependency-free library for parsing micro expressions.

MicroExpr A small & fast dependency-free library for parsing micro expressions. This library was originally built for use in templating languages (e.g

Nov 25, 2022
A utility library to do files/io/bytes processing/parsing in file-system or network.

goreader A utility library to do files/io/bytes processing/parsing in file-system or network. These features are really common to be implemented for a

Nov 1, 2021
This library provides an ASTERIX Frame(binary data) decoding/parsing(json,xml) capabilities for Go.

GoAsterix This library provides an ASTERIX Frame(binary data) decoding/parsing(json,xml) capabilities for Go. ASTERIX ASTERIX (All Purpose Structured

Dec 13, 2022
randstr is a module that contains functions for generating random strings.

randstr is a module that contains functions for generating random strings. The functions in this module uses the crypto/rand package. Installa

Nov 13, 2021
Simple go package which converts roman strings to integer

romanparse Simple go package which converts roman strings

Aug 11, 2022