Documentation
¶
Overview ¶
Package ansibump converts ANSI escape sequences such as colors, cursor movements, and character deletions, into a HTML representation.
Index ¶
- Constants
- Variables
- func BasicHex(code int, bright bool, pal Palette) string
- func Bytes(r io.Reader, width int) ([]byte, error)
- func RGBHex(params []int, i int) string
- func String(r io.Reader, width int) (string, error)
- func WriteTo(r io.Reader, w io.Writer, width int) (int64, error)
- func XtermColor(code int) (int, int, int)
- func XtermColors(code int) (int, int, int)
- func XtermGray(code int) (int, int, int)
- func XtermHex(code int, pal Palette) string
- type Attribute
- type Color
- type Colors
- type Customizer
- type Decoder
- func (d *Decoder) ApplyCSI(final byte, params []int) error
- func (d *Decoder) CursorBack(params []int) error
- func (d *Decoder) CursorDown(params []int) error
- func (d *Decoder) CursorForward(params []int) error
- func (d *Decoder) CursorHorizontalAbsolute(params []int) error
- func (d *Decoder) CursorNextLine(params []int) error
- func (d *Decoder) CursorPosition(params []int) error
- func (d *Decoder) CursorPreviousLine(params []int) error
- func (d *Decoder) CursorUp(params []int) error
- func (d *Decoder) EraseInDisplay(params []int) error
- func (d *Decoder) EraseInLine(params []int) error
- func (d *Decoder) Lines(pal Palette) []string
- func (d *Decoder) Read(r io.Reader) error
- func (d *Decoder) RestoreCursorPosition(params []int) error
- func (d *Decoder) SaveCursorPosition(params []int) error
- func (d *Decoder) Write(w io.Writer) error
- type Palette
Examples ¶
Constants ¶
const ( NUL = 0x00 // NUL is an ASCII null character EOF = 0x1a // EOF is the MS-DOS end-of-file character value ESC = 0x1b // ESC is the escape control character code Reset = 0 Bold = 1 NotBold = 21 NotBoldFaint = 22 Underline = 4 NotUnderline = 24 Invert = 7 NotInvert = 27 DefaultFG = 39 DefaultBG = 49 FG1st = 30 FGEnd = 37 BG1st = 40 BGEnd = 47 BrightFG1st = 90 BrightFGEnd = 97 BrightBG1st = 100 BrightBGEnd = 107 SetFG = 38 SetBG = 48 )
Variables ¶
var ( ErrReader = errors.New("reader is nil") ErrUnexpected = errors.New("unexpected parameters") ErrRecognized = errors.New("unrecognised parameters") ErrParam = errors.New("encountered ';' without parameter") ErrExpect0or1 = errors.New("expected 0 or 1 parameters") ErrExpect0or2 = errors.New("expected 0 or 2 parameters") ErrExpect1 = errors.New("expected 1 parameter") ErrUnknownCSI = errors.New("unrecognized CSI final byte") ErrUnknownCtr = errors.New("unrecognized control byte") ErrUnknownEsc = errors.New("unrecognized ESC sequence after ESC") )
Functions ¶
func BasicHex ¶
BasicHex takes a standard color code and returns a corresponding hexadecimal string. When bright is toggled, a lighter color variant is used. Codes are values between 0 and 7, and any invalid codes returns a blank string.
func Bytes ¶
Bytes returns the HTML elements of the ANSI encoded text found in the Reader. It assumes the Reader is using IBM Code Page 437 encoding. If width is <= 0, an 80 columns value is used.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/bengarrett/ansibump"
)
func main() {
const ansi = "\x1b[0m\x1b[5;30;42mHI\x1b[0m"
r := strings.NewReader(ansi)
p, _ := ansibump.Bytes(r, 80)
fmt.Printf("%q", p)
}
Output: "<div style=\"color:#aaa;background-color:#000;\"><span style=\"color:#000;background-color:#0a0;\">HI</span></div>"
func RGBHex ¶ added in v1.0.1
RGBHex converts the params into a "true color", red, green, blue hex string.
func String ¶
String returns the HTML elements of the ANSI encoded text found in the Reader. It assumes the Reader is using IBM Code Page 437 encoding. If width is <= 0, an 80 columns value is used.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/bengarrett/ansibump"
)
func main() {
const ansi = "\x1b[0m\n\x1b[1;34m\x02\x1b[0m \x1b[1;34mA\x1b[36mN\x1b[33mS\x1b[37mI\x1b[35mbump\x1b[0;33m\x1b[37m"
r := strings.NewReader(ansi)
s, _ := ansibump.String(r, 80)
fmt.Printf("%q", s)
}
Output: "<div style=\"color:#aaa;background-color:#000;\">\n<span style=\"color:#55f;\">\x02</span><span style=\"color:#aaa;\"> </span><span style=\"color:#55f;\">A</span><span style=\"color:#5ff;\">N</span><span style=\"color:#ff5;\">S</span><span style=\"color:#fff;\">I</span><span style=\"color:#f5f;\">bump</span></div>"
Example (Rgb) ¶
package main
import (
"fmt"
"strings"
"github.com/bengarrett/ansibump"
)
func main() {
const ansi = "\x1b[0m\x1b[38;2;135;0;255;48;2;135;95;0mPurple on Orange4\x1b[0m"
r := strings.NewReader(ansi)
s, _ := ansibump.String(r, 80)
fmt.Printf("%q", s)
}
Output: "<div style=\"color:#aaa;background-color:#000;\"><span style=\"color:#8700ff;background-color:#875f00;\">Purple on Orange4</span></div>"
Example (Xterm256) ¶
package main
import (
"fmt"
"strings"
"github.com/bengarrett/ansibump"
)
func main() {
const ansi = "\x1b[0m\x1b[38;5;93mPurple\x1b[0m \x1b[38;5;94mOrange4\x1b[0m"
r := strings.NewReader(ansi)
s, _ := ansibump.String(r, 80)
fmt.Printf("%q", s)
}
Output: "<div style=\"color:#aaa;background-color:#000;\"><span style=\"color:#8700ff;\">Purple</span><span style=\"color:#aaa;\"> </span><span style=\"color:#875f00;\">Orange4</span></div>"
func WriteTo ¶
WriteTo writes to w the HTML elements of the ANSI encoded text found in the Reader. It assumes the Reader is using IBM Code Page 437 encoding. If width is <= 0, an 80 columns value is used.
The return int64 is the number of bytes written.
Example ¶
package main
import (
"bufio"
"bytes"
"fmt"
"strings"
"github.com/bengarrett/ansibump"
)
func main() {
const ansi = "\x1b[0m\x1b[5;30;42mHI\x1b[0m"
input := strings.NewReader(ansi)
var b bytes.Buffer
output := bufio.NewWriter(&b)
cnt, _ := ansibump.WriteTo(input, output, 80)
output.Flush()
fmt.Printf("%d bytes written\n%q", cnt, b.String())
}
Output: 110 bytes written "<div style=\"color:#aaa;background-color:#000;\"><span style=\"color:#000;background-color:#0a0;\">HI</span></div>"
func XtermColor ¶
XtermColor returns the RGB values for non-system Xterm colors.
func XtermColors ¶
XtermColors takes a Xterm non-system color code and returns the corresponding RGB values. The code values begin at 16 and finish at 255. If a code is out of range, then the returned RGB values will be -1, which are invalid.
Some helpful links, 256 colors cheat sheet, Xterm Colors, and 8-bit colors wiki.
Types ¶
type Attribute ¶
type Attribute struct {
FG string // FG is a foreground hex color like "rrggbb" or (no leading #) or empty for default
BG string // BG is a background hex color like "rrggbb"
Bold bool // Bold toggles a lighter color variation
Underline bool // Underline toggles a underline text decoration
Inverse bool // Inverse swaps the background and foreground colors
}
Attribute describes styling for a single character cell.
type Color ¶
type Color string
Color code represented as hexadecimal numeric value. These are often 6 digit values RRGGBB (red, green, blue), however, certain values can be shortened to 3 digit values.
For example, the code of CGA red "aa0000" (red: aa, green: 00, blue: 00) can shortened to "a00".
const ( CBlack Color = "000" // cga black CRed Color = "a00" // red CGreen Color = "0a0" // green CBrown Color = "a50" // yellow CBlue Color = "00a" // blue CMagenta Color = "a0a" // magenta CCyan Color = "0aa" // cyan CGray Color = "aaa" // white CDarkGray Color = "555" // bright black CLRed Color = "f55" // bright red CLGreen Color = "5f5" // bright green CYellow Color = "ff5" // bright yellow CLBlue Color = "55f" // bright blue CLMagenta Color = "f5f" // bright magenta CLCyan Color = "5ff" // bright cyan CWhite Color = "fff" // bright white XBlack Color = "000" // xterm black XMarron Color = "800000" // red XGreen Color = "008000" // green XOlive Color = "808000" // yellow XPurple Color = "800080" // magenta XTeal Color = "008080" // cyan XSilver Color = "c0c0c0" // white XGray Color = "808080" // bright black XRed Color = "f00" // bright red XLime Color = "0f0" // bright green XYellow Color = "ff0" // bright yellow XBlue Color = "00f" // bright blue XFuchsia Color = "f5f" // bright magenta XAqua Color = "0ff" // bright cyan XWhite Color = "fff" // bright white DPBlack Color = "000" // deluxe paint ii black DPRed Color = "a80000" // red DPGreen Color = "008800" // green DPBrown Color = "a85420" // brown DPBlue Color = "0000fc" // blue DPMagenta Color = "cc0088" // magenta DPCyan Color = "00a8fc" // cyan DPGray Color = "747474" // white DPDarkGray Color = "646464" // bright black DPLRed Color = "ec0000" // bright red DPLGreen Color = "88fc00" // bright green DPYellow Color = "fcec00" // bright yellow DPLBlue Color = "0074cc" // bright blue DPLMagenta Color = "cc00ec" // bright magenta DPLCyan Color = "00dcdc" // bright cyan DPWhite Color = "fcfcfc" // bright white )
func Bright ¶
Bright takes a palette color and swaps it for a lighter variant. For example, Color.CBlack (CGA black) returns Color.CDarkGray (CGA bright black).
type Colors ¶ added in v1.0.1
type Colors [16]Color
type Customizer ¶ added in v1.1.0
type Customizer struct {
// Width is the number of columns of the ANSI encoded text.
// If a value provided is <= 0, then a common an 80 columns value is used.
Width int
// The AmigaParser should be set to false except with edge cases where unusual
// Commodore Amiga specific encodings are to be parsed. When set to true:
// - character 0x0C "♀︎" is a form feed controller and will be replaced with a '<br>' element.
// - 0x1B,0x5B,0x1B,0x5B "←[←[" is treated as a single ANSI escape control.
AmigaParser bool
// Strict is a debug mode that will throw errors when the ANSI includes malformed and invalid data or values.
Strict bool
// Color Palette can either be CGA16, Xterm16, or DP2.
// - CGA16 is the default, it is the Color Graphics Adapter colorset defined by IBM for the PC in 1981.
// - Xterm16 is the Xterm terminal emulator program for the X Window System colorset from the mid-1980s.
// - DP2 is a Commodore Amiga era Deluxe Paint II colorset that mimics the colors of CGA16.
Color Palette
// CharSet is the simple character encoding used by the text.
//
// Generally the charset of ANSI art should be [charmap.CodePage437],
// however artworks for the Commodore Amiga are often [charmap.ISO8859_1].
// Modern artworks or terminal text will usually be in UTF-8 encoding
// which can be set with CharSet = nil or CharSet = [charmap.XUserDefined].
CharSet *charmap.Charmap
}
Customizer is optional, and is used to configure the parsing of the ANSI encoded text.
Usually, the defaults work for most texts. Instead, use: ansibump.Bytes, ansibump.String, or ansibump.WriteTo.
func (*Customizer) Buffer ¶ added in v1.1.0
Buffer creates a new Buffer containing the HTML elements of the ANSI encoded text found in the Reader.
The parser configurations and arguments are configured using the Customizer.
func (*Customizer) NewDecoder ¶ added in v1.1.0
func (c *Customizer) NewDecoder() *Decoder
NewDecoder creates a Decoder with the given Customizer.
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder maintains the screen buffer and cursor state while parsing ANSI.
func (*Decoder) ApplyCSI ¶
ApplyCSI handles cursor movement and erase sequences that alter the buffer or cursor. It follows standard ANSI/VT100 CSI final bytes used in the original request: A B C D E F G H f J K s u
func (*Decoder) CursorBack ¶
CursorBack moves cursor back. Attr: CUB.
func (*Decoder) CursorDown ¶
CursorDown moves cursor down. Attr: CUD.
func (*Decoder) CursorForward ¶
CursorForward moves cursor forward. Attr: CUF.
func (*Decoder) CursorHorizontalAbsolute ¶
CursorHorizontalAbsolute moves the cursor to column. Attr: CHA.
func (*Decoder) CursorNextLine ¶
CursorNextLine moves cursor down to the beginning of the line. Attr: CNL.
func (*Decoder) CursorPosition ¶
CursorPosition moves the cursor to row and column. Attr: CUP.
func (*Decoder) CursorPreviousLine ¶
CursorPreviousLine moves cursor up to the beginning of the line. Attr: CPL.
func (*Decoder) EraseInDisplay ¶
EraseInDisplay clears part of the screen. Attr: ED.
func (*Decoder) EraseInLine ¶
EraseInLine part of the line. Attr: EL.
func (*Decoder) Lines ¶
Lines renders each buffer line into a single HTML string. Each contiguous run of identical attributes is wrapped in a <span style="...">.
func (*Decoder) RestoreCursorPosition ¶
RestoreCursorPosition restores the saved cursor state. Abbr: SCP, SCOSC.
func (*Decoder) SaveCursorPosition ¶
SaveCursorPosition saves the cursor state for later use. Abbr: RCP, SCORC.
type Palette ¶
type Palette uint
Palette sets the ANSI 4-bit color codes to a colorset of RGB values. The ANSI standard never formalized color values and it was left to the system to determine. Wikipedia has a useful table of the common palettes.