ansihtml

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2022 License: MIT Imports: 7 Imported by: 0

README

ansihtml

Travis codecov PkgGoDev

Go package to parse ANSI escape sequences to HTML.

Usage

html := ansihtml.ConvertToHTML([]byte("\x1b[33mThis text is yellow.\x1b[m"))
// html: `<span style="color:olive;">This text is yellow.</span>`

html := ansihtml.ConvertToHTMLWithClasses([]byte("\x1b[31mThis text is red."), "ansi-", false)
// html: `<span class="ansi-fg-red">This text is red.</span>`

Documentation

Overview

Package ansihtml parses text formatted with ANSI escape sequences and outputs text suitable for display in a HTML <pre> tag.

Text effects are encoded as <span> tags with style attributes or various classes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertToHTML

func ConvertToHTML(ansiBytes []byte) []byte

ConvertToHTML converts ansiBytes to HTML where ANSI escape sequences have been translated to <span> tags with appropriate style attributes.

func ConvertToHTMLWithClasses

func ConvertToHTMLWithClasses(ansiBytes []byte, classPrefix string, noStyles bool) []byte

ConvertToHTMLWithClasses converts ansiBytes to HTML where ANSI escape sequences have been translated to <span> tags with appropriate classes set.

classPrefix will be prefixed to the standard class names in the output.

If noStyles is true, no style tags will be emitted for 256-color and 24-bit color text; instead, these color sequences will have no effect.

A span in the output may have any combination of these classes:

  • 'bold' or 'faint'
  • 'italic' or 'fraktur'
  • 'double-underline' or 'underline'
  • 'strikethrough'
  • 'overline'
  • 'slow-blink' or 'fast-blink'
  • 'invert'
  • 'hide'
  • one of 'font-{n}' where n is between 1 and 9
  • 'proportional'
  • 'superscript' or 'subscript'
  • 'fg-{color}', 'bg-{color}', and 'underline-{color}' where color is one of
  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white
  • bright-black
  • bright-red
  • bright-green
  • bright-yellow
  • bright-blue
  • bright-magenta
  • bright-cyan
  • bright-white

Types

type Parser

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

Parser parses ANSI-encoded console output from an io.Reader.

func NewParser

func NewParser(rd io.Reader, w io.Writer) *Parser

NewParser creates a Parser which reads from rd and writes output with escape sequences removed to w.

func (*Parser) Parse

func (p *Parser) Parse(escapeHandler func(finalByte byte, intermediateBytes, parameterBytes []byte) error) error

Parse reads from the io.Reader, calling escapeHandler with any parsed ANSI escape sequences and writing normal output to the io.Writer, until either EOF is reached or an error occurs.

Writes to w and calls to escapeHandler are done in the same order as data is read from the io.Reader, meaning escapeHandler can write to the io.Writer to insert text formatting data as necessary.

escapeHandler takes the finalByte and intermediateBytes from the escape sequence and any parameterBytes from after the escape sequence as parameters. For example, the escape sequence '\x1b[0;33m' will result in escapeHandler being called with finalByte '[', intermediateBytes ”, and parameterBytes '0;33m'.

intermediateBytes is rarely present in ANSI escape sequences, with one example being the switching between JIS encodings done by ISO-2022-JP.

func (*Parser) ParseBuffer

func (p *Parser) ParseBuffer(buf []byte, escapeHandler func(finalByte byte, intermediateBytes, parameterBytes []byte) error) error

ParseBuffer performs the same action as Parse, but with a caller-supplied buffer for copying data from the reader to the writer.

Jump to

Keyboard shortcuts

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