package ui

import ""

Package ui contains types that may be used by different editor frontends.


Package Files

color.go key.go parse_sgr.go style.go styling.go text.go text_segment.go text_util.go ui.go


const (
    // DefaultBindingRune is a special value to represent a default binding.
    DefaultBindingRune rune = iota - functionKeyOffset




    // Function key names that are aliases for their ASCII representation.
    Tab       = '\t'
    Enter     = '\n'
    Backspace = 0x7f

Special negative runes to represent function keys, used in the Rune field of the Key struct. This also has a few function names that are aliases for simple runes. See keyNames below for mapping these values to strings.


var Default = Key{DefaultBindingRune, 0}

Default is used in the key binding table to indicate a default binding.

type Color

type Color interface {
    String() string
    // contains filtered or unexported methods

Color represents a color.

var (
    Black   Color = ansiColor(0)
    Red     Color = ansiColor(1)
    Green   Color = ansiColor(2)
    Yellow  Color = ansiColor(3)
    Blue    Color = ansiColor(4)
    Magenta Color = ansiColor(5)
    Cyan    Color = ansiColor(6)
    White   Color = ansiColor(7)

    BrightBlack   Color = ansiBrightColor(0)
    BrightRed     Color = ansiBrightColor(1)
    BrightGreen   Color = ansiBrightColor(2)
    BrightYellow  Color = ansiBrightColor(3)
    BrightBlue    Color = ansiBrightColor(4)
    BrightMagenta Color = ansiBrightColor(5)
    BrightCyan    Color = ansiBrightColor(6)
    BrightWhite   Color = ansiBrightColor(7)

Builtin ANSI colors.

func TrueColor

func TrueColor(r, g, b uint8) Color

TrueColor returns a 24-bit true color.

func XTerm256Color

func XTerm256Color(i uint8) Color

XTerm256Color returns a color from the xterm 256-color palette.

type Key

type Key struct {
    Rune rune
    Mod  Mod

Key represents a single keyboard input, typically assembled from a escape sequence.

func K

func K(r rune, mods ...Mod) Key

K constructs a new Key.

func ParseKey

func ParseKey(s string) (Key, error)

ParseKey parses a symbolic key. The syntax is:

Key = { Mod ('+' | '-') } BareKey

BareKey = FunctionKeyName | SingleRune

func (Key) Equal

func (k Key) Equal(other interface{}) bool

func (Key) Hash

func (k Key) Hash() uint32

func (Key) Kind

func (k Key) Kind() string

func (Key) Repr

func (k Key) Repr(int) string

func (Key) String

func (k Key) String() string

type Keys

type Keys []Key

Keys implements sort.Interface.

func (Keys) Len

func (ks Keys) Len() int

func (Keys) Less

func (ks Keys) Less(i, j int) bool

func (Keys) Swap

func (ks Keys) Swap(i, j int)

type Mod

type Mod byte

Mod represents a modifier key.

const (
    // Shift is the shift modifier. It is only applied to special keys (e.g.
    // Shift-F1). For instance 'A' and '@' which are typically entered with the
    // shift key pressed, are not considered to be shift-modified.
    Shift Mod = 1 << iota
    // Alt is the alt modifier, traditionally known as the meta modifier.

Values for Mod.

type RuneStylesheet

type RuneStylesheet map[rune]Styling

RuneStylesheet maps runes to stylings.

type Segment

type Segment struct {
    Text string

Segment is a string that has some style applied to it.

func StyleSegment

func StyleSegment(seg *Segment, ts ...Styling) *Segment

StyleSegment returns a new Segment with the given Styling's applied. It does not modify the given Segment.

func (*Segment) Clone

func (s *Segment) Clone() *Segment

Clone returns a copy of the Segment.

func (*Segment) Concat

func (s *Segment) Concat(v interface{}) (interface{}, error)

Concat implements Segment+string, Segment+float64, Segment+Segment and Segment+Text.

func (*Segment) CountRune

func (s *Segment) CountRune(r rune) int

CountRune counts the number of times a rune occurs in a Segment.

func (*Segment) Index

func (s *Segment) Index(k interface{}) (v interface{}, ok bool)

Index provides access to the attributes of a styled-segment.

func (*Segment) IterateKeys

func (*Segment) IterateKeys(fn func(v interface{}) bool)

IterateKeys feeds the function with all valid attributes of styled-segment.

func (*Segment) Kind

func (*Segment) Kind() string

Kind returns "styled-segment".

func (*Segment) RConcat

func (s *Segment) RConcat(v interface{}) (interface{}, error)

RConcat implements string+Segment and float64+Segment.

func (*Segment) Repr

func (s *Segment) Repr(int) string

Repr returns the representation of this Segment. The string can be used to construct an identical Segment. Unset or default attributes are skipped. If the Segment represents an unstyled string only this string is returned.

func (*Segment) SplitByRune

func (s *Segment) SplitByRune(r rune) []*Segment

SplitByRune splits a Segment by the given rune.

func (*Segment) String

func (s *Segment) String() string

String returns a string representation of the styled segment. This now always assumes VT-style terminal output. TODO: Make string conversion sensible to environment, e.g. use HTML when output is web.

func (*Segment) VTString

func (s *Segment) VTString() string

VTString renders the styled segment using VT-style escape sequences.

type Style

type Style struct {
    Foreground Color
    Background Color
    Bold       bool
    Dim        bool
    Italic     bool
    Underlined bool
    Blink      bool
    Inverse    bool

Style specifies how something (mostly a string) shall be displayed.

func ApplyStyling

func ApplyStyling(s Style, ts ...Styling) Style

ApplyStyling returns a new Style with the given Styling's applied.

func StyleFromSGR

func StyleFromSGR(s string) Style

StyleFromSGR builds a Style from an SGR sequence.

func (*Style) MergeFromOptions

func (s *Style) MergeFromOptions(options map[string]interface{}) error

MergeFromOptions merges all recognized values from a map to the current Style.

func (Style) SGR

func (s Style) SGR() string

SGR returns SGR sequence for the style.

type Styling

type Styling interface {
    // contains filtered or unexported methods

Styling specifies how to change a Style. It can also be applied to a Segment or Text.

var (
    Reset Styling = reset{}

    FgDefault Styling = setForeground{nil}

    FgBlack   Styling = setForeground{Black}
    FgRed     Styling = setForeground{Red}
    FgGreen   Styling = setForeground{Green}
    FgYellow  Styling = setForeground{Yellow}
    FgBlue    Styling = setForeground{Blue}
    FgMagenta Styling = setForeground{Magenta}
    FgCyan    Styling = setForeground{Cyan}
    FgWhite   Styling = setForeground{White}

    FgBrightBlack   Styling = setForeground{BrightBlack}
    FgBrightRed     Styling = setForeground{BrightRed}
    FgBrightGreen   Styling = setForeground{BrightGreen}
    FgBrightYellow  Styling = setForeground{BrightYellow}
    FgBrightBlue    Styling = setForeground{BrightBlue}
    FgBrightMagenta Styling = setForeground{BrightMagenta}
    FgBrightCyan    Styling = setForeground{BrightCyan}
    FgBrightWhite   Styling = setForeground{BrightWhite}

    BgDefault Styling = setBackground{nil}

    BgBlack   Styling = setBackground{Black}
    BgRed     Styling = setBackground{Red}
    BgGreen   Styling = setBackground{Green}
    BgYellow  Styling = setBackground{Yellow}
    BgBlue    Styling = setBackground{Blue}
    BgMagenta Styling = setBackground{Magenta}
    BgCyan    Styling = setBackground{Cyan}
    BgWhite   Styling = setBackground{White}

    BgBrightBlack   Styling = setBackground{BrightBlack}
    BgBrightRed     Styling = setBackground{BrightRed}
    BgBrightGreen   Styling = setBackground{BrightGreen}
    BgBrightYellow  Styling = setBackground{BrightYellow}
    BgBrightBlue    Styling = setBackground{BrightBlue}
    BgBrightMagenta Styling = setBackground{BrightMagenta}
    BgBrightCyan    Styling = setBackground{BrightCyan}
    BgBrightWhite   Styling = setBackground{BrightWhite}

    Bold       Styling = boolOn{boldField{}}
    Dim        Styling = boolOn{dimField{}}
    Italic     Styling = boolOn{italicField{}}
    Underlined Styling = boolOn{underlinedField{}}
    Blink      Styling = boolOn{blinkField{}}
    Inverse    Styling = boolOn{inverseField{}}

    NoBold       Styling = boolOff{boldField{}}
    NoDim        Styling = boolOff{dimField{}}
    NoItalic     Styling = boolOff{italicField{}}
    NoUnderlined Styling = boolOff{underlinedField{}}
    NoBlink      Styling = boolOff{blinkField{}}
    NoInverse    Styling = boolOff{inverseField{}}

    ToggleBold       Styling = boolToggle{boldField{}}
    ToggleDim        Styling = boolToggle{dimField{}}
    ToggleItalic     Styling = boolToggle{italicField{}}
    ToggleUnderlined Styling = boolToggle{underlinedField{}}
    ToggleBlink      Styling = boolToggle{blinkField{}}
    ToggleInverse    Styling = boolToggle{inverseField{}}

Common stylings.

func Bg

func Bg(c Color) Styling

Bg returns a Styling that sets the background color.

func Fg

func Fg(c Color) Styling

Fg returns a Styling that sets the foreground color.

func ParseStyling

func ParseStyling(s string) Styling

ParseStyling parses a text representation of Styling, which are kebab case counterparts to the names of the builtin Styling's. For example, ToggleInverse is expressed as "toggle-inverse".

Multiple stylings can be joined by spaces, which is equivalent to calling Stylings.

If the given string is invalid, ParseStyling returns nil.

func StylingFromSGR

func StylingFromSGR(s string) Styling

StylingFromSGR builds a Style from an SGR sequence.

func Stylings

func Stylings(ts ...Styling) Styling

Stylings joins several transformers into one.

type Text

type Text []*Segment

Text contains of a list of styled Segments.

func Concat

func Concat(texts ...Text) Text

Concat concatenates multiple Text's into one.

func MarkLines

func MarkLines(args ...interface{}) Text

MarkLines provides a way to construct a styled text by separating the content and the styling.

The arguments are groups of either

* A single string, in which case it represents an unstyled line;

* Three arguments that can be passed to MarkLine, in which case they are passed

to MarkLine and the return value is used as a styled line.

Lines represented by all the groups are joined together.

This function is mainly useful for constructing multi-line Text's with alignment across those lines. An example:

var stylesheet = map[rune]string{
    '-': Reverse,
    'x': Stylings(Blue, BgGreen),
var text = FromMarkedLines(
    "foo      bar      foobar", stylesheet,
    "---      xxx      ------"
    "lorem    ipsum    dolar",

func MarkText

func MarkText(line string, stylesheet RuneStylesheet, style string) Text

MarkText applies styles to all the runes in the line, using the runes in the style string. The stylesheet argument specifies which style each rune represents.

func ParseSGREscapedText

func ParseSGREscapedText(s string) Text

ParseSGREscapedText parses SGR-escaped text into a Text. It also removes non-SGR CSI sequences sequences in the text.

func StyleText

func StyleText(t Text, ts ...Styling) Text

StyleText returns a new Text with the given Styling's applied. It does not modify the given Text.

func T

func T(s string, ts ...Styling) Text

T constructs a new Text with the given content and the given Styling's applied.

func (Text) Clone

func (t Text) Clone() Text

Clone returns a deep copy of Text.

func (Text) Concat

func (t Text) Concat(rhs interface{}) (interface{}, error)

Concat implements Text+string, Text+float64, Text+Segment and Text+Text.

func (Text) CountLines

func (t Text) CountLines() int

CountLines counts the number of lines in a Text. It is equal to t.CountRune('\n') + 1.

func (Text) CountRune

func (t Text) CountRune(r rune) int

CountRune counts the number of times a rune occurs in a Text.

func (Text) Index

func (t Text) Index(k interface{}) (interface{}, error)

Index provides access to the underlying styled-segment.

func (Text) IterateKeys

func (t Text) IterateKeys(fn func(interface{}) bool)

IterateKeys feeds the function with all valid indices of the styled-text.

func (Text) Kind

func (Text) Kind() string

Kind returns "styled-text".

func (Text) Partition

func (t Text) Partition(indices []Text

Partition partitions the Text at n indices into n+1 Text values.

func (Text) RConcat

func (t Text) RConcat(lhs interface{}) (interface{}, error)

RConcat implements string+Text and float64+Text.

func (Text) Repr

func (t Text) Repr(indent int) string

Repr returns the representation of the current Text. It is just a wrapper around the containing Segments.

func (Text) SplitByRune

func (t Text) SplitByRune(r rune) []Text

SplitByRune splits a Text by the given rune.

func (Text) String

func (t Text) String() string

String returns a string representation of the styled text. This now always assumes VT-style terminal output.

TODO: Make string conversion sensible to environment, e.g. use HTML when output is web.

func (Text) TrimWcwidth

func (t Text) TrimWcwidth(wmax int) Text

TrimWcwidth returns the largest prefix of t that does not exceed the given visual width.

func (Text) VTString

func (t Text) VTString() string

VTString renders the styled text using VT-style escape sequences.

Package ui imports 8 packages (graph) and is imported by 10 packages. Updated 1 month ago.

Tools for package owners.