ansisgr

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2022 License: MIT Imports: 1 Imported by: 2

README

go-ansisgr

PkgGoDev GitHub Actions codecov

go-ansisgr provides a SGR (Select Graphic Rendition, a part of ANSI Escape Sequence) parser.

  • 16 colors, 256 colors and RGB colors support
  • All attributes support

Installation

go get github.com/ktr0731/go-fuzzyfinder

Usage

ansisgr.NewIterator is the only entry-point API. This function returns an iteratorw which consumes the passed string.

in := "a\x1b[1;31mb"
iter := ansisgr.NewIterator(in)

for {
	r, style, ok := iter.Next()
	if !ok {
		break
	}

        // do something.
}

r is a rune, and style is the foreground/background color and attributes r has.

if color, ok := style.Foreground(); ok {
	// Foreground color is specified.
}
if color, ok := style.Background(); ok {
	// Background color is specified.
}

The attribute method reports whether r has the attribute.

style.Bold()
style.Italic()

go-ansisgr with gdamore/tcell

go-ansisgr is useful when you construct a rich terminal user interface by using gdamore/tcell or others. Although gdamore/tcell and other libraries provides SGR functionality from their API, they doesn't support "raw" strings which contain ANSI Escape Sequence. Therefore, go-ansisgr translates these strings and makes it easy to use the TUI library's functionality.
For example, the following code displays colored string powered by gdamore/tcell.

func main() {
	screen, err := tcell.NewScreen()
	if err != nil {
		log.Fatal(err)
	}
	defer screen.Fini()

	if err := screen.Init(); err != nil {
		log.Fatal(err)
	}

	in := "\x1b[38;2;100;200;200mhello, \x1b[0;1;30;48;5;245mworld!"
	iter := ansisgr.NewIterator(in)
	for i := 0; ; i++ {
		r, rstyle, ok := iter.Next()
		if !ok {
			break
		}

		style := tcell.StyleDefault
		if color, ok := rstyle.Foreground(); ok {
			switch color.Mode() {
			case ansisgr.Mode16:
				style = style.Foreground(tcell.PaletteColor(color.Value() - 30))
			case ansisgr.Mode256:
				style = style.Foreground(tcell.PaletteColor(color.Value()))
			case ansisgr.ModeRGB:
				r, g, b := color.RGB()
				style = style.Foreground(tcell.NewRGBColor(int32(r), int32(g), int32(b)))
			}
		}
		if color, valid := rstyle.Background(); valid {
			switch color.Mode() {
			case ansisgr.Mode16:
				style = style.Background(tcell.PaletteColor(color.Value() - 40))
			case ansisgr.Mode256:
				style = style.Background(tcell.PaletteColor(color.Value()))
			case ansisgr.ModeRGB:
				r, g, b := color.RGB()
				style = style.Background(tcell.NewRGBColor(int32(r), int32(g), int32(b)))
			}
		}

		style = style.
			Bold(rstyle.Bold()).
			Dim(rstyle.Dim()).
			Italic(rstyle.Italic()).
			Underline(rstyle.Underline()).
			Blink(rstyle.Blink()).
			Reverse(rstyle.Reverse()).
			StrikeThrough(rstyle.Strikethrough())

		screen.SetContent(i, 0, r, nil, style)
	}

	screen.Show()

	time.Sleep(3 * time.Second)
}

Documentation

Overview

go-ansisgr provides an SGR (Select Graphic Rendition, a part of ANSI Escape Sequence) parser.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Color

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

Color represents color value specified by SGR.

func (Color) Mode

func (c Color) Mode() Mode

Mode returns the color's Mode.

func (Color) RGB

func (c Color) RGB() (int, int, int)

RGB returns red, green, and blue color values. It assumes that c.Mode() == ModeRGB.

func (Color) Value

func (c Color) Value() int

Value returns the color value represented by int. For example, '\x1b[31m' is 31, '\x1b[38;5;116m' is 116, and '\x1b[38;2;10;20;30' is 660510 (0x0a141e).

type Iterator

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

Iterator is an iterator over a parsed string.

func NewIterator

func NewIterator(s string) *Iterator

NewIterator returns a new Iterator.

Example
package main

import (
	"fmt"
	"strings"

	ansisgr "github.com/ktr0731/go-ansisgr"
)

func main() {
	in := "a\x1b[1;31;40mb\x1b[0;4;38;5;45;48;2;0;51;102mc"
	iter := ansisgr.NewIterator(in)

	var got []string

	for {
		r, style, ok := iter.Next()
		if !ok {
			break
		}

		var s []string
		s = append(s, fmt.Sprintf("rune: %c", r))
		if color, ok := style.Foreground(); ok {
			switch color.Mode() {
			case ansisgr.Mode16, ansisgr.Mode256:
				s = append(s, fmt.Sprintf("foreground: %d", color.Value()))
			case ansisgr.ModeRGB:
				red, green, blue := color.RGB()
				s = append(s, fmt.Sprintf("foreground: %d (%d, %d, %d)", color.Value(), red, green, blue))
			}
		}
		if color, ok := style.Background(); ok {
			switch color.Mode() {
			case ansisgr.Mode16, ansisgr.Mode256:
				s = append(s, fmt.Sprintf("background: %d", color.Value()))
			case ansisgr.ModeRGB:
				red, green, blue := color.RGB()
				s = append(s, fmt.Sprintf("background: 0x%06x (%d, %d, %d)", color.Value(), red, green, blue))
			}
		}
		if style.Bold() {
			s = append(s, "bold")
		}

		got = append(got, strings.Join(s, ", "))
	}

	fmt.Println(strings.Join(got, "\n"))

}
Output:

rune: a
rune: b, foreground: 31, background: 40, bold
rune: c, foreground: 45, background: 0x003366 (0, 51, 102)

func (*Iterator) Next

func (a *Iterator) Next() (rune, Style, bool)

Next returns a next rune and its style. the third return value indicates there is a next value.

type Mode

type Mode int

Mode represents a color syntax.

const (
	// ModeNone is used to when color is not specified.
	ModeNone Mode = iota
	// Mode16 represents 16 colors.
	Mode16
	// Mode256 represents 256 colors.
	Mode256
	// ModeRGB represents RGB colors (a.k.a. True Color).
	ModeRGB
)

type Style

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

Style represents a set of attributes, foreground color, and background color.

func (*Style) Background

func (s *Style) Background() (Color, bool)

Background returns the background color. the second return value indicates whether the color is valid or not.

func (s *Style) Blink() bool

Blink indicates whether blink is enabled.

func (*Style) Bold

func (s *Style) Bold() bool

Bold indicates whether bold is enabled.

func (*Style) Dim

func (s *Style) Dim() bool

Dim indicates whether dim (faint) is enabled.

func (*Style) Foreground

func (s *Style) Foreground() (Color, bool)

Foreground returns the foreground color. the second return value indicates whether the color is valid or not.

func (*Style) Invisible

func (s *Style) Invisible() bool

Invisible indicates whether invisible is enabled.

func (*Style) Italic

func (s *Style) Italic() bool

Italic indicates whether italic is enabled.

func (*Style) Reverse

func (s *Style) Reverse() bool

Reverse indicates whether reverse (hidden) is enabled.

func (*Style) Strikethrough

func (s *Style) Strikethrough() bool

Strikethrough indicates whether strikethrough is enabled.

func (*Style) Underline

func (s *Style) Underline() bool

Underline indicates whether underline is enabled.

Jump to

Keyboard shortcuts

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