Documentation ¶
Overview ¶
Package ansifmt deals with ansi escape code and there associated format.
Index ¶
- Constants
- func AnsiCode(from, to Format) (code string)
- func AnsiSize() (lines, cols int, err error)
- func LineWrap(txt string, length int) (lines []string)
- func ToTitle(txt string) (title string)
- type Coder
- type Color
- type Format
- func (f Format) Background() Color
- func (f Format) Coder() Coder
- func (f Format) Foreground() Color
- func (f Format) Reverse() bool
- func (f *Format) SetBackground(color Color) (c Color)
- func (f *Format) SetForeground(color Color) (c Color)
- func (f *Format) SetReverse(r bool) (c bool)
- func (f *Format) SetStrike(s bool) (c bool)
- func (f *Format) SetUnder(u bool) (c bool)
- func (f *Format) SetWeight(w Weight) (c Weight)
- func (f Format) Strike() bool
- func (f Format) Under() bool
- func (f Format) Weight() Weight
- type Weight
- type Writer
Examples ¶
Constants ¶
const ( //caveat: we need to start with Color because iota value matters for "colors" BlackColor = Color(iota) RedColor GreenColor YellowColor BlueColor MagentaColor CyanColor WhiteColor //the reset code Reset = "\x1b[0m" NormalWeight = Weight(iota) BoldWeight FaintWeight )
Variables ¶
This section is empty.
Functions ¶
func AnsiSize ¶
AnsiSize return the number of Lines and Columns in the current "terminal" (if available)
func LineWrap ¶
LineWrap executes the linewrap algorithm escaping ansi escape code. it returns a slice of lines.
'length' is the max numbers in a column. It is respected unless a single word is too big to fit in
Example ¶
lines := LineWrap("toto \x1b[1mis\x1b[0m a weird person", 6) for i, line := range lines { fmt.Printf("%v: %q\n", i, line) }
Output: 0: "toto " 1: "\x1b[1mis\x1b[0m a " 2: "weird " 3: "person"
func ToTitle ¶
ToTitle maps the strings.ToTitle but support text containing ansi escape code
strings.ToTitle("\x1b[1mHello") > "\x1b[1MHELLO"
This is not a valid ansi escaped code.
Example ¶
title := "this \x1b[1mis\x1b[0m a title" Title := ToTitle(title) fmt.Printf("%q", Title)
Output: "THIS \x1b[1mIS\x1b[0m A TITLE"
Types ¶
type Format ¶
type Format struct {
// contains filtered or unexported fields
}
Format describe a terminal Format. Limited capacity due to limited ansi escape capacity works nicely with the following pattern
defer f.SetWeight( f.SetWeight(BoldWeight) )
Because, `f.SetWeight(BoldWeight)` is evaluated immediatly, the format is now "Bold" It will be reset to previous value when the deferred call will happen
func (Format) Background ¶
func (Format) Coder ¶
Example ¶
var f Format f.SetStrike(true) striker := f.Coder() fmt.Printf("%q", striker("toto"))
Output: "\x1b[9mtoto\x1b[29m"
func (Format) Foreground ¶
func (*Format) SetBackground ¶
SetBackground and return previous value
func (*Format) SetForeground ¶
SetForeground and return previous value
func (*Format) SetReverse ¶
SetReverse and return previous value
type Writer ¶
type Writer struct { Format // contains filtered or unexported fields }
Writer implements Writer but it can change the output Format using ansi escape codes.
It also implements Formatter interface, it is easy to specify a Format.
Changes to the Format are buffered until the next Write. So it is not inefficient to toggle Format without writing anyting.
Only changes in Format are written down.
It has a method Reset that writedown immediatly a reset ansi code.
func (*Writer) Reset ¶
Reset send the ansi code \033[0m immediatly to the stream.
It does not change the current in memory Format. So the next call to Write() the ansi code will be recomputed.
For instance
w:= NewWriter(os.Stdout) w.SetWeight(BoldWeight) fmt.Fprintf("Bolded Text") fmt.Fprintf("Bolded Text")
Directories ¶
Path | Synopsis |
---|---|
renderer for russross's Blackfriday markdown processor.
|
renderer for russross's Blackfriday markdown processor. |