ansi

package module
v0.0.0-...-8c54266 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2021 License: MIT Imports: 4 Imported by: 2

README

ansi

ansi is a pluggable solution for parsing and interpreting streams of ANSI text. The intended use-case is for processing streams of log output for Concourse.

Usage

import (
    "encoding/json"

    "github.com/aoldershaw/ansi"
)

func main() {
    var lines ansi.Lines
    writer := ansi.NewWriter(&lines)

    writer.Write([]byte("\x1b[1mbold\x1b[m not bold"))
    writer.Write([]byte("\nline 2"))

    linesJSON, _ := json.MarshalIndent(lines, "", "  ")
    fmt.Println(string(linesJSON))
}

Output:

[
  [
    {
      "data": "bold",
      "style": {
        "bold": true
      }
    },
    {
      "data": " not bold",
      "style": {}
    }
  ],
  [
    {
      "data": "line 2",
      "style": {}
    }
  ]
]

Currently, the only provided output method is ansi.Lines, which stores all the lines of text in memory. A line is a slice of ansi.Chunk - a stylized chunk of text. ansi.Chunks are intended to be concatenated in order.

Parser

The parser can also be used independently of the interpreter.

import (
    "github.com/aoldershaw/ansi"
)

func main() {
    parser := ansi.NewParser()

    input := []byte("some bytes")
    for _, action := range parser.ParseAll(input) {
        switch v := action.(type) {
            case ansi.Print:
                fmt.Println(string(v))
        }
    }
}

Installation

go get -u github.com/aoldershaw/ansi

Documentation

Overview

Example
var lines ansi.Lines
writer := ansi.NewWriter(&lines)

writer.Write([]byte("\x1b[1mbold\x1b[m text"))
writer.Write([]byte("\nline 2"))

linesJSON, _ := json.Marshal(lines)
fmt.Println(string(linesJSON))
Output:

[[{"data":"bold","style":{"bold":true}},{"data":" text","style":{}}],[{"data":"line 2","style":{}}]]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action interface {
	ActionString() string
}

type CarriageReturn

type CarriageReturn struct{}

func (CarriageReturn) ActionString

func (a CarriageReturn) ActionString() string

func (CarriageReturn) String

func (a CarriageReturn) String() string

type Chunk

type Chunk struct {
	Data  Text  `json:"data"`
	Style Style `json:"style"`
}

type Color

type Color uint8
const (
	DefaultColor Color = iota
	Black
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	White
	BrightBlack
	BrightRed
	BrightGreen
	BrightYellow
	BrightBlue
	BrightMagenta
	BrightCyan
	BrightWhite
)

func (Color) MarshalJSON

func (c Color) MarshalJSON() ([]byte, error)

func (Color) String

func (c Color) String() string

type CursorBack

type CursorBack int

func (CursorBack) ActionString

func (a CursorBack) ActionString() string

func (CursorBack) String

func (a CursorBack) String() string

type CursorColumn

type CursorColumn int

func (CursorColumn) ActionString

func (a CursorColumn) ActionString() string

func (CursorColumn) String

func (a CursorColumn) String() string

type CursorDown

type CursorDown int

func (CursorDown) ActionString

func (a CursorDown) ActionString() string

func (CursorDown) String

func (a CursorDown) String() string

type CursorForward

type CursorForward int

func (CursorForward) ActionString

func (a CursorForward) ActionString() string

func (CursorForward) String

func (a CursorForward) String() string

type CursorPosition

type CursorPosition Pos

func (CursorPosition) ActionString

func (a CursorPosition) ActionString() string

func (CursorPosition) String

func (a CursorPosition) String() string

type CursorUp

type CursorUp int

func (CursorUp) ActionString

func (a CursorUp) ActionString() string

func (CursorUp) String

func (a CursorUp) String() string

type EraseDisplay

type EraseDisplay EraseMode

func (EraseDisplay) ActionString

func (a EraseDisplay) ActionString() string

func (EraseDisplay) String

func (a EraseDisplay) String() string

type EraseLine

type EraseLine EraseMode

func (EraseLine) ActionString

func (a EraseLine) ActionString() string

func (EraseLine) String

func (a EraseLine) String() string

type EraseMode

type EraseMode uint8
const (
	EraseToEnd EraseMode = iota
	EraseToBeginning
	EraseAll
)

func (EraseMode) String

func (e EraseMode) String() string

type Line

type Line = []Chunk

type LineDiscipline

type LineDiscipline int
const (
	Raw LineDiscipline = iota
	Cooked
)

type Linebreak

type Linebreak struct{}

func (Linebreak) ActionString

func (a Linebreak) ActionString() string

func (Linebreak) String

func (a Linebreak) String() string

type Lines

type Lines []Line

func (Lines) ClearRight

func (l Lines) ClearRight(pos Pos) error

func (*Lines) Print

func (l *Lines) Print(data []byte, style Style, pos Pos) error

type Output

type Output interface {
	// Print must not modify the slice data, not even temporarily.
	// Implementations must not retain a reference to data.
	Print(data []byte, style Style, pos Pos) error
	ClearRight(pos Pos) error
}

type Parser

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

func NewParser

func NewParser() *Parser

func (*Parser) Parse

func (p *Parser) Parse(input []byte) (Action, bool, []byte)

func (*Parser) ParseAll

func (p *Parser) ParseAll(input []byte) []Action

type Pos

type Pos struct {
	Line int
	Col  int
}

func (Pos) String

func (p Pos) String() string

type Print

type Print []byte

func (Print) ActionString

func (a Print) ActionString() string

func (Print) String

func (a Print) String() string

type Reset

type Reset struct{}

func (Reset) ActionString

func (a Reset) ActionString() string

func (Reset) String

func (a Reset) String() string

type RestoreCursorPosition

type RestoreCursorPosition struct{}

func (RestoreCursorPosition) ActionString

func (a RestoreCursorPosition) ActionString() string

func (RestoreCursorPosition) String

func (a RestoreCursorPosition) String() string

type SaveCursorPosition

type SaveCursorPosition struct{}

func (SaveCursorPosition) ActionString

func (a SaveCursorPosition) ActionString() string

func (SaveCursorPosition) String

func (a SaveCursorPosition) String() string

type SetBackground

type SetBackground Color

func (SetBackground) ActionString

func (a SetBackground) ActionString() string

func (SetBackground) String

func (a SetBackground) String() string
type SetBlink bool

func (SetBlink) ActionString

func (a SetBlink) ActionString() string

func (SetBlink) String

func (a SetBlink) String() string

type SetBold

type SetBold bool

func (SetBold) ActionString

func (a SetBold) ActionString() string

func (SetBold) String

func (a SetBold) String() string

type SetFaint

type SetFaint bool

func (SetFaint) ActionString

func (a SetFaint) ActionString() string

func (SetFaint) String

func (a SetFaint) String() string

type SetForeground

type SetForeground Color

func (SetForeground) ActionString

func (a SetForeground) ActionString() string

func (SetForeground) String

func (a SetForeground) String() string

type SetFraktur

type SetFraktur bool

func (SetFraktur) ActionString

func (a SetFraktur) ActionString() string

func (SetFraktur) String

func (a SetFraktur) String() string

type SetFramed

type SetFramed bool

func (SetFramed) ActionString

func (a SetFramed) ActionString() string

func (SetFramed) String

func (a SetFramed) String() string

type SetInverted

type SetInverted bool

func (SetInverted) ActionString

func (a SetInverted) ActionString() string

func (SetInverted) String

func (a SetInverted) String() string

type SetItalic

type SetItalic bool

func (SetItalic) ActionString

func (a SetItalic) ActionString() string

func (SetItalic) String

func (a SetItalic) String() string

type SetUnderline

type SetUnderline bool

func (SetUnderline) ActionString

func (a SetUnderline) ActionString() string

func (SetUnderline) String

func (a SetUnderline) String() string

type State

type State struct {
	Style          Style
	LineDiscipline LineDiscipline
	Position       Pos
	SavedPosition  *Pos

	MaxLine int
	MaxCol  int
}

type Style

type Style struct {
	Foreground Color
	Background Color
	Modifier   StyleModifier
}

func (Style) MarshalJSON

func (s Style) MarshalJSON() ([]byte, error)

func (*Style) UnmarshalJSON

func (s *Style) UnmarshalJSON(data []byte) error

type StyleModifier

type StyleModifier uint8
const (
	Bold StyleModifier = 1 << iota
	Faint
	Italic
	Underline
	Blink
	Inverted
	Fraktur
	Framed
)

type Text

type Text []byte

func (Text) MarshalJSON

func (t Text) MarshalJSON() ([]byte, error)

func (*Text) UnmarshalJSON

func (t *Text) UnmarshalJSON(data []byte) error

type Writer

type Writer struct {
	State
	Parser *Parser
	Output Output
}

func NewWriter

func NewWriter(output Output, opts ...WriterOption) *Writer

func (*Writer) Action

func (w *Writer) Action(act Action) error

func (*Writer) Write

func (w *Writer) Write(input []byte) (int64, error)

type WriterOption

type WriterOption func(*Writer)

func WithInitialScreenSize

func WithInitialScreenSize(lines, cols int) WriterOption

func WithLineDiscipline

func WithLineDiscipline(d LineDiscipline) WriterOption

Jump to

Keyboard shortcuts

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