A library for parsing ANSI encoded strings

Overview


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,
    },
}
You might also like...
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

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

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

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

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.

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.

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

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

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).

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.

    opened by davidhsingyuchen 9
  • 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.

    opened by jeffwilliams 3
  • 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).
    opened by pavelpatrin 3
  • 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:

    opened by fossabot 1
Releases(v1.6.0)
  • v1.6.0(Aug 6, 2022)

    What's Changed

    • Add fields to StyledText for offset and length in input string by @jeffwilliams in https://github.com/leaanthony/go-ansi-parser/pull/5

    New Contributors

    • @jeffwilliams made their first contribution in https://github.com/leaanthony/go-ansi-parser/pull/5

    Full Changelog: https://github.com/leaanthony/go-ansi-parser/compare/v1.5.0...v1.6.0

    Source code(tar.gz)
    Source code(zip)
  • v1.5.0(May 31, 2022)

  • v1.4.0(Mar 19, 2022)

    What's Changed

    • Options specifies how to parse by @pavelpatrin in https://github.com/leaanthony/go-ansi-parser/pull/2

    New Contributors

    • @pavelpatrin made their first contribution in https://github.com/leaanthony/go-ansi-parser/pull/2

    Full Changelog: https://github.com/leaanthony/go-ansi-parser/compare/v1.3.0...v1.4.0

    Source code(tar.gz)
    Source code(zip)
  • v1.3.0(Feb 26, 2022)

  • v1.2.0(Apr 13, 2021)

  • v1.1.0(Apr 13, 2021)

    • Support Truncate() for truncating the given string to a given number of maximum characters whilst preserving formatting
    • Support Cleanse() for removing escape codes from the given string
    • Support HasEscapeCodes() - returns true when there are ANSI Escape codes in the given string
    Source code(tar.gz)
    Source code(zip)
  • v1.0.1(Apr 10, 2021)

  • v1.0.0(Apr 10, 2021)

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

Reg 2 Feb 1, 2022
Raw ANSI sequence helpers

Raw ANSI sequence helpers

Christian Muehlhaeuser 17 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). It has support for Windows too! The API can be used in several ways, pick one that suits you.

Sensio 0 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

Richard Artoul 376 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.

HashiCorp 1.3k 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

Daniel G. Taylor 10 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

VOrishirne 4 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

MMK 4 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

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

romanparse Simple go package which converts roman strings

Caio Ribeiro Pereira 4 Aug 11, 2022