Skip to content

Volte6/ansitags

Repository files navigation

ansitags

Overview

ansitags is a helper library that allows to you use common tags inside of text that result in ANSI escape code (color). Currently only one directional parsing is possible ( tagged stringscolor escaped strings )

  • ansitags.go Contains the code and structs for the basic parsing logic and flow of data, noteably ansitags.Parse() and ansitags.ParseStreaming().
  • ansiproperties.go handles basic ansi properties/tag parsing and conversion into valid escape codes.
  • tagmatcher.go basic helper struct to simplify finding ansi "tag" matches.
  • ansitags_test.go Contains unit tests, benchmarks, etc
  • testdata/ansitags_test.yaml Contains unit test data with input & expect output. The ANSI Control Sequence Introducer should be represented by a unicode escaped value - \u001b (Octal 33, Hexadecimal 1b, Decimal 27)

Quick Start

Import the module:

import "github.com/Volte6/ansitags"

Parse and print a string:

fmt.Println( ansitags.Parse("This is a <ansi fg='red' bg='blue'>white text on a blue background</ansi>") )

Result:

alt text

Note: You can switch between 256 color mode and 8 color mode (The default is 8):

ansitags.SetColorMode(ansitags.Color8)
ansitags.SetColorMode(ansitags.Color256)

Future plans (Short term)

  • CSI sequence support such as cursor position

Future plans (Long term)

  • Stripping out color codes / tags from strings
  • Generating color-styled HTML from color codes / tags

About

Simple tag structure for color coding strings and a function to parse it into ANSI escape codes

Resources

Stars

Watchers

Forks

Packages

No packages published