process

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: GPL-3.0 Imports: 5 Imported by: 1

README

ANSI SGR Process

Extract ANSI SGR Sequences from string

[!NOTE] Tab\t will be replaced by 4 spaces

Usage

  • Extract(string) -> (*ANSITableList, string)
    split string with ansi into ansi sequences and raw string
  • Render(atl *ANSITableList, _s string, startIndex int) -> []byte
    Render string with ansi sequences, startIndex is the start index of _s which will be used to fetch ansi table
  • Search(bs []BoundStruct, position int) -> (res []int)
    binary search index given Bound [2]int, the result is index.
    len(res) <= 2 and -1 means reach the start/end of the slice
    len(res) == 1 means the position is within bs[res[0]].Bounds
    len(res) == 2 means the position is between bs[res[0]].Bounds[1] and bs[res[1]].Bounds[0]
  • *ANSITableList SetStyle(style []byte, startIndex, endIndex int)
    set style for string[startIndex:endIndex]

Documentation

Index

Constants

View Source
const (
	ESCAPE_SEQUENCE     = '\x1b'
	SGR_FUNC            = byte('\x6d')
	ESCAPE_SEQUENCE_END = string(ESCAPE_SEQUENCE) + "[0" + string(SGR_FUNC)
)
View Source
const TAB_RUNE = '\t'

NOTE: Planning to make these rune process function available to be set from outside

Variables

View Source
var EMPTY_ANSITABLELIST = make([]BoundsStruct, 0)
View Source
var TAB_BYTES = []byte{32, 32, 32, 32}

Functions

func IsEndOfSGR

func IsEndOfSGR(data []byte) bool

func IsEscEnd

func IsEscEnd(c rune) bool

func IsSGR

func IsSGR(data []byte) bool

func Render added in v0.1.1

func Render(atl *ANSITableList, _s string, startIndex int) []byte
func Search(list []BoundsStruct, pos int) []int

binary search index given `Bound [2]int`

func SliceFrom

func SliceFrom[S ~[]E, E comparable](src S, start, end int) S

if endIndex bigger than length, return copy of rest. mainly for cut slice easier

Types

type ANSIQueueItem

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

type ANSITable

type ANSITable struct {
	Sub   *ANSITable
	Data  []byte
	Bound [2]int // rune index
}

func (*ANSITable) AddStyle

func (a *ANSITable) AddStyle(data []byte, boundLeft int)

func (*ANSITable) GetBounds added in v0.1.2

func (a *ANSITable) GetBounds() [2]int

implement `BoundsStruct` for search

func (*ANSITable) Split

func (a *ANSITable) Split(index int) (*ANSITable, *ANSITable)

split ansi table given index(bounds), left: [:index] right: [index:]

type ANSITableList

type ANSITableList struct {
	L []BoundsStruct
}

func Extract

func Extract(s string) (*ANSITableList, string)

split `string with ansi` into `ansi sequences` and `raw string`, Ps: tab will be replaced by 4 spaces

func (*ANSITableList) GetSlice

func (a *ANSITableList) GetSlice(startIndex, endIndex int) []BoundsStruct

get a slice of ansi table, it will find all tables between `startIndex` and `endIndex`

func (*ANSITableList) SetStyle

func (a *ANSITableList) SetStyle(style []byte, startIndex, endIndex int)

type BoundsStruct

type BoundsStruct interface {
	GetBounds() [2]int
}

Jump to

Keyboard shortcuts

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