Documentation
¶
Overview ¶
Package termtools provides basic functionality to style console output on Linux
v1.0.0
Copyright 2021 Dmitry Fedotov ¶
See package README for usage examples: github.com/dmfed/termtools
General note concerning module usage: Whenever color value of type interface{} is required by signature of a method or function, either string or int may be supplied. Valid color names (to be passed as string) are: "black", "blue", "cyan", "green", "magenta", "red", "white", "yellow", "brightblack", "brightblue", "brightcyan", "brightgreen", "brightmagenta", "brightred", "brightwhite", "brightyellow". Valid color IDs (to be passed as int) are from 0 to 255 inclusive.
Index ¶
- Constants
- Variables
- func ClearLine()
- func ClearLineLeft()
- func ClearLineRight()
- func ClearScreen()
- func ClearScreenDown()
- func ClearScreenUp()
- func Csprint(color interface{}, a ...interface{}) string
- func Csprintf(color interface{}, format string, a ...interface{}) string
- func GetBackgroundCode(color interface{}) (string, error)
- func GetColorCode(color interface{}) (string, error)
- func GetTermSize() (columns int, rows int, err error)
- func MoveCursorDown(rows int)
- func MoveCursorHome()
- func MoveCursorLeft(columns int)
- func MoveCursorRight(columns int)
- func MoveCursorTo(column, row int)
- func MoveCursorToNextRow()
- func MoveCursorToRow(row int)
- func MoveCursorUp(rows int)
- func PrintAtPosition(column, row int, a ...interface{})
- func PrintAtPositionAndReturn(column, row int, a ...interface{})
- func RestoreCursorPosition()
- func SaveCursorPosition()
- type PrintSuite
- func (suite *PrintSuite) AddPrinter(printername string, p *Printer) error
- func (suite *PrintSuite) Configure(configs ...PrinterConfig) error
- func (suite *PrintSuite) SwitchTo(printername string) error
- func (suite *PrintSuite) SwitchToDefault()
- func (suite *PrintSuite) Use(printername string) *Printer
- func (suite *PrintSuite) UseDefault() *Printer
- type Printer
- func (p *Printer) Errorf(format string, a ...interface{}) error
- func (p *Printer) Fprint(w io.Writer, a ...interface{}) (n int, err error)
- func (p *Printer) Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)
- func (p *Printer) Fprintln(w io.Writer, a ...interface{}) (n int, err error)
- func (p *Printer) MoveDown(rows int)
- func (p *Printer) MoveHome()
- func (p *Printer) MoveLeft(columns int)
- func (p *Printer) MoveRight(columns int)
- func (p *Printer) MoveTo(column, row int)
- func (p *Printer) MoveToNextRow()
- func (p *Printer) MoveToRow(row int)
- func (p *Printer) MoveUp(rows int)
- func (p *Printer) Print(a ...interface{}) (n int, err error)
- func (p *Printer) PrintAtPosition(column, row int, a ...interface{}) (n int, err error)
- func (p *Printer) PrintAtPositionAndReturn(column, row int, a ...interface{}) (n int, err error)
- func (p *Printer) Printf(format string, a ...interface{}) (n int, err error)
- func (p *Printer) Println(a ...interface{}) (n int, err error)
- func (p *Printer) Reset()
- func (p *Printer) SetBackground(color interface{}) error
- func (p *Printer) SetColor(color interface{}) error
- func (p *Printer) SetPrefixSuffix(prefix, suffix string)
- func (p *Printer) Sprint(a ...interface{}) string
- func (p *Printer) Sprintf(format string, a ...interface{}) string
- func (p *Printer) Sprintln(a ...interface{}) string
- func (p *Printer) ToggleBlinking()
- func (p *Printer) ToggleBold()
- func (p *Printer) ToggleReversed()
- func (p *Printer) ToggleUnderline()
- type PrinterConfig
Constants ¶
const ( // The following constants hold ANSI escape sequences setting color and style of output Esc string = "\x1b" //Basic 8 colors Black string = Esc + "[30m" Red = Esc + "[31m" Green = Esc + "[32m" Yellow = Esc + "[33m" Blue = Esc + "[34m" Magenta = Esc + "[35m" Cyan = Esc + "[36m" White = Esc + "[37m" //Additional 8 bright colors BrightBlack string = Esc + "[30;1m" BrightRed = Esc + "[31;1m" BrightGreen = Esc + "[32;1m" BrightYellow = Esc + "[33;1m" BrightBlue = Esc + "[34;1m" BrightMagenta = Esc + "[35;1m" BrightCyan = Esc + "[36;1m" BrightWhite = Esc + "[37;1m" //Basic 8 background colors BBlack string = Esc + "[40m" BRed = Esc + "[41m" BGreen = Esc + "[42m" BYellow = Esc + "[43m" BBlue = Esc + "[44m" BMagenta = Esc + "[45m" BCyan = Esc + "[46m" BWhite = Esc + "[47m" //Additional 8 bright background colors BBrightBlack string = Esc + "[40;1m" BBrightRed = Esc + "[41;1m" BBrightGreen = Esc + "[42;1m" BBrightYellow = Esc + "[43;1m" BBrightBlue = Esc + "[44;1m" BBrightMagenta = Esc + "[45;1m" BBrightCyan = Esc + "[46;1m" BBrightWhite = Esc + "[47;1m" // Color format string to use with 256 color codes. Needs int in range [0;255]. ColorIDTemplate string = Esc + "[38;5;%vm" BackgroundIDTemplate = Esc + "[48;5;%vm" //Styles. Can be used separately or together with color and background codes. Bold string = Esc + "[1m" Underline = Esc + "[4m" Blinking = Esc + "[5m" Reversed = Esc + "[7m" // Reset escape sequence Reset string = Esc + "[0m" // Cursor maniputation CursorSave string = Esc + "[s" CursorRestore = Esc + "[u" CursorHome = Esc + "[H" CursorGotoTemplate = Esc + "[%v;%vH" CursorMoveUpTemplate = Esc + "[%vA" CursorMoveDownTemplate = Esc + "[%vB" CursorMoveRightTemplate = Esc + "[%vC" CursorMoveLeftTemplate = Esc + "[%vD" CursorMoveToNextRowTemplate = Esc + "[E" CursorMoveToRowTemplate = Esc + "[%vH" // Clear screen codes Clear string = Esc + "[2J" ClearUp = Esc + "[1J" ClearDown = Esc + "[0J" // Clear line codes ClearL string = Esc + "[2K" ClearLLeft = Esc + "[1K" ClearLRight = Esc + "[0K" )
Variables ¶
var ( ErrUnknownColor = errors.New("error: unknown color name or color id out of range [0;255]") ErrUnknownTermSize = errors.New("error: could not find out terminal size") )
var ( ErrFailedToSetColor = errors.New("error: failed to set printer color") ErrFailedToSetBackground = errors.New("error: failed to set printer background") )
var ( ErrUnknownPrinter = errors.New("error: no such printer") ErrFailedToAdd = errors.New("error: failed to add printer: name already taken or nil pointer passed") ErrFailedToProcessPrinterConfig = errors.New("error: failed to process PrinterConfig: field Name must not be empty when adding to PrintSuite") ErrEmptyName = errors.New("error: printer name may not be empty string") )
Functions ¶
func ClearScreenDown ¶
func ClearScreenDown()
ClearScreenDown clears screen from current cursor position down
func ClearScreenUp ¶
func ClearScreenUp()
ClearScreenUp clears screen from current cursor position up
func Csprint ¶ added in v1.0.0
func Csprint(color interface{}, a ...interface{}) string
Csprint formats using the default formats for its operands and returns the resulting string. It accepts color color identifier (string or int). If color is invalid the function will return fmt.Sprint(a).
func Csprintf ¶ added in v1.0.0
Csprintf formats according to a format specifier and returns the resulting string. It accepts color color identifier (string or int). If color is invalid the function will return fmt.Sprintf(format, a).
func GetBackgroundCode ¶ added in v1.0.0
GetBackgroundCode accepts color identifier (string or int) and returns ANSI escape sequence for requested background color. If color is invalid the function will return empty string and an error.
func GetColorCode ¶ added in v1.0.0
GetColorCode accepts color identifier (string or int) and returns ANSI escape sequence for requested color. If color is invalid the function will return empty string and an error.
func GetTermSize ¶
GetTermSize returns current terminal size (number of columns and rows). It may fail to get correct values and will return -1, -1 in this case. If you're relying on output to precisely position cursor on screen always check error.
func MoveCursorDown ¶ added in v0.8.0
func MoveCursorDown(rows int)
MoveCursorDown moves cursor down specified number of rows
func MoveCursorHome ¶ added in v1.0.0
func MoveCursorHome()
MoveCursorHome moves cursor to the upper left corner of the screen. Essentially the same as MoveCursorTo(0, 0).
func MoveCursorLeft ¶ added in v0.8.0
func MoveCursorLeft(columns int)
MoveCursorLeft moves cursor left specified number of columns
func MoveCursorRight ¶ added in v0.8.0
func MoveCursorRight(columns int)
MoveCursorRight moves cursor left specified number of columns
func MoveCursorTo ¶ added in v0.8.0
func MoveCursorTo(column, row int)
MoveCursorTo moves cursor to the specified position in terminal. (0, 0) is upper left. Will do nothing if x or y are out of bounds or we can not get size of terminal.
func MoveCursorToNextRow ¶ added in v0.8.0
func MoveCursorToNextRow()
MoveCursorToNextRow moves cursor to next row
func MoveCursorToRow ¶ added in v0.8.0
func MoveCursorToRow(row int)
MoveCursorToRow places cursor at the beginning of specified row
func MoveCursorUp ¶ added in v0.8.0
func MoveCursorUp(rows int)
MoveCursorUp moves cursor up specified number of rows
func PrintAtPosition ¶ added in v0.8.0
func PrintAtPosition(column, row int, a ...interface{})
PrintAtPosition moves cursor in the current terminal to the specified position and prints. It does not return the cursor to the initial position so subsequent call to Print/Println etc. will output immediately after the previous output. Will print at current cursor position if terminal size is unavailable or supplied column and row are out of range.
func PrintAtPositionAndReturn ¶
func PrintAtPositionAndReturn(column, row int, a ...interface{})
PrintAtPositionAndReturn moves cursor in the current terminal to the specified position, prints, and then returns cursor to the inital position. Will print at current cursor position if terminal size is unavailable or supplied column and row are out of range.
func RestoreCursorPosition ¶ added in v0.8.0
func RestoreCursorPosition()
RestoreCursorPosition places cursor to original position when SaveCursorPosition was called and restores attributes.
func SaveCursorPosition ¶ added in v0.8.0
func SaveCursorPosition()
SaveCursorPosition saves current cursor position and attributes. Call RestoreCursorPosition() to return
Types ¶
type PrintSuite ¶ added in v1.0.0
type PrintSuite struct { Printer // contains filtered or unexported fields }
PrintSuite zero ore more configurations of Printer which allows to switch added printer configurations on the fly to use differrent output styles. Printer configurations can be added with AddPrinter() and Configure() methods.
An instance of printer is embedded into PrintSuite so you can call printer methods on it directly. By default the embedded printer acts exactly the same way as functions from fmt module of standard library (does not alter output in any way). Embedded printer configuration can be changed either by calling termtools.Printer methods or by adding a new prtinter configuration and switching to it with SwitchTo().
func (*PrintSuite) AddPrinter ¶ added in v1.0.0
func (suite *PrintSuite) AddPrinter(printername string, p *Printer) error
AddPrinter accepts name of printer and pointer to printer. If nil pointer or empty string is passed or if printername is already added the method will fail with an error.
func (*PrintSuite) Configure ¶ added in v1.0.0
func (suite *PrintSuite) Configure(configs ...PrinterConfig) error
Configure accepts one or more PrinterConfig and adds printers to PrintSuite. If one or more configs fail to process the method will return an error listing names that failed to add. Important: if method encounters empty Name field in PrinterConfig(s), the method will fail with an error and subsequent configurations will not be processed.
func (*PrintSuite) SwitchTo ¶ added in v1.0.0
func (suite *PrintSuite) SwitchTo(printername string) error
SwitchTo sets the embedded PrintSuite printer to printer with requested name. If printername is not known the method will return an error without changes to current configuration.
func (*PrintSuite) SwitchToDefault ¶ added in v1.0.0
func (suite *PrintSuite) SwitchToDefault()
SwitchToDefault switches active printer of PrintSuite to default Printer{} with no settings.
func (*PrintSuite) Use ¶ added in v1.0.0
func (suite *PrintSuite) Use(printername string) *Printer
Use returns instance of printer with requested printername. If printername is invalid (no printer with such name has been added or name is empty string) a default Printer instance is returned.
func (*PrintSuite) UseDefault ¶ added in v1.0.0
func (suite *PrintSuite) UseDefault() *Printer
UseDefault acts in the same manner as Use but always returns printer with no style options set which will output the same as fmt module functions.
type Printer ¶
type Printer struct {
// contains filtered or unexported fields
}
Printer holds color and style settings and implements most methods as in fmt module like Print, Println, Sprint etc. adding color and styles to the input values.
func NewPrinter ¶
func NewPrinter(conf PrinterConfig) (p *Printer, err error)
NewPrinter takes PrinterConfig and returns pointer to Printer. If invalid color(s) identifier is encountered the function returns instance of printer with color(s) not set. Otherwise the printer is functional. Check for errors to make sure that returned Printer is fully configured.
func (*Printer) Errorf ¶
Errorf formats according to a format specifier and returns the string as a value that satisfies error.
func (*Printer) Fprint ¶
Fprint formats using the default formats for its operands and writes to w. Spaces are added between operands when neither is a string. It returns the number of bytes written and any write error encountered.
func (*Printer) Fprintf ¶
Fprintf formats according to a format specifier and writes to w. It returns the number of bytes written and any write error encountered.
func (*Printer) Fprintln ¶
Fprintln formats using the default formats for its operands and writes to w. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered.
func (*Printer) MoveHome ¶ added in v1.0.0
func (p *Printer) MoveHome()
MoveHome places cursor at the top left corner of the screen.
func (*Printer) MoveRight ¶ added in v0.8.0
MoveRight moves cursor right specified number of columns.
func (*Printer) MoveToNextRow ¶ added in v0.8.0
func (p *Printer) MoveToNextRow()
MoveToNextRow moves cursor to the next row..
func (*Printer) Print ¶
Print formats using the default formats for its operands and writes to standard output. Spaces are added between operands when neither is a string. It returns the number of bytes written and any write error encountered.
func (*Printer) PrintAtPosition ¶ added in v0.8.0
PrintAtPosition moves cursor to specified column and row and issues Print It does not return to the initial position. It returns the number of bytes written and any write error encountered. See also PrintAtPositionAndReturn method.
func (*Printer) PrintAtPositionAndReturn ¶ added in v0.8.0
PrintAtPositionAndReturn moves cursor to specified column and row and issues Print then moves cursor to initial position when method was called. It returns the number of bytes written and any write error encountered.
func (*Printer) Printf ¶
Printf formats according to a format specifier and writes to standard output. It returns the number of bytes written and any write error encountered.
func (*Printer) Println ¶
Println formats using the default formats for its operands and writes to standard output. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered.
func (*Printer) Reset ¶
func (p *Printer) Reset()
Reset resets printer state to initial state (no color, no background, bold, underline and reversed modes turned off).
func (*Printer) SetBackground ¶
SetBackground sets background color of printer. Argument color is the same as in SetColor method. will return an error and currently set Printer color will not be changed.
func (*Printer) SetColor ¶
SetColor sets color of printer. Argument "color" must be either string or int. Valid color names are: "black", "blue", "cyan", "green", "magenta", "red", "white", "yellow", "brightblack", "brightblue", "brightcyan", "brightgreen", "brightmagenta", "brightred", "brightwhite", "brightyellow". Valid numbers are from 0 to 255 inclusive. If color is not known, is empty, or int is out of range the method will return an error and currently set Printer color will not be changed.
func (*Printer) SetPrefixSuffix ¶ added in v1.0.0
SetPrefixSuffix configures Printer to always preceed output with prefix and end output with suffix. Printer color and style settings apply to prefix and suffix.
func (*Printer) Sprint ¶
Sprint formats using the default formats for its operands and returns the resulting string. Spaces are added between operands when neither is a string.
func (*Printer) Sprintf ¶
Sprintf formats according to a format specifier and returns the resulting string.
func (*Printer) Sprintln ¶
Sprintln formats using the default formats for its operands and returns the resulting string. Spaces are always added between operands and a newline is appended.
func (*Printer) ToggleBlinking ¶ added in v1.0.0
func (p *Printer) ToggleBlinking()
ToggleBlinking toggles blinking mode of Printer
func (*Printer) ToggleReversed ¶
func (p *Printer) ToggleReversed()
ToggleReversed toggles reverse mode of Printer
func (*Printer) ToggleUnderline ¶
func (p *Printer) ToggleUnderline()
ToggleUnderline toggles underline mode of Printer
type PrinterConfig ¶ added in v1.0.0
type PrinterConfig struct { // Name identifies configuration when used // with PrintSuite type. Name field is mandatory when passing PrinterConfig // to PrintSuite Configure method! // // When setting up a Printer instance with NewPrinter() Name field may be omitted. Name string // Color and Backgrount fields may hold color name (as string) or color ID in range // [0;255]. See package docs for list of available color names. Color interface{} Background interface{} // Bold, Underline, Reversed, Blinking switch relevant printer modes on if set to true. Bold bool Underline bool Reversed bool Blinking bool // Prefix and suffix are added to output if they are not empty strings. Prefix string Suffix string }
PrinterConfig describes configuration of Printer.