Documentation
¶
Overview ¶
Package termtools provides basic functionality to style console output in Linux.
Copyright 2021 Dmitry Fedotov
Index ¶
- Constants
- Variables
- func ClearLine()
- func ClearLineLeft()
- func ClearLineRight()
- func ClearScreen()
- func ClearScreenDown()
- func ClearScreenUp()
- func ColorIDSprint(colorcode int, a ...interface{}) string
- func ColorSprint(colorname string, a ...interface{}) string
- func GetBackgroundCodeByID(id int) (string, error)
- func GetBackgroundCodeByName(colorname string) (string, error)
- func GetColorCodeByID(id int) (string, error)
- func GetColorCodeByName(colorname string) (string, error)
- func GetTermSize() (int, int)
- func MoveCursor(x, y int)
- func PrintAtPositionAndReturn(x, y int, a ...interface{})
- 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) Print(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(colorname string) error
- func (p *Printer) SetBackgroundID(id int) error
- func (p *Printer) SetColor(colorname string) error
- func (p *Printer) SetColorID(id int) error
- 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) ToggleBold()
- func (p *Printer) ToggleReversed()
- func (p *Printer) ToggleUnderline()
Constants ¶
const ( //Basic 8 colors Black string = "\u001b[30m" Red = "\u001b[31m" Green = "\u001b[32m" Yellow = "\u001b[33m" Blue = "\u001b[34m" Magenta = "\u001b[35m" Cyan = "\u001b[36m" White = "\u001b[37m" //Additional 8 bright colors BrightBlack string = "\u001b[30;1m" BrightRed = "\u001b[31;1m" BrightGreen = "\u001b[32;1m" BrightYellow = "\u001b[33;1m" BrightBlue = "\u001b[34;1m" BrightMagenta = "\u001b[35;1m" BrightCyan = "\u001b[36;1m" BrightWhite = "\u001b[37;1m" //Basic 8 background colors BBlack string = "\u001b[40m" BRed = "\u001b[41m" BGreen = "\u001b[42m" BYellow = "\u001b[43m" BBlue = "\u001b[44m" BMagenta = "\u001b[45m" BCyan = "\u001b[46m" BWhite = "\u001b[47m" //Additional 8 bright background colors BBrightBlack string = "\u001b[40;1m" BBrightRed = "\u001b[41;1m" BBrightGreen = "\u001b[42;1m" BBrightYellow = "\u001b[43;1m" BBrightBlue = "\u001b[44;1m" BBrightMagenta = "\u001b[45;1m" BBrightCyan = "\u001b[46;1m" BBrightWhite = "\u001b[47;1m" //Styles (can be used separately or together with color and background codes) Bold string = "\u001b[1m" Underline = "\u001b[4m" Reversed = "\u001b[7m" //Reset escape sequence Reset string = "\u001b[0m" // SaveCursor - code to save cursor position SaveCursor string = "\033[s" //RestoreCursor code to restore cursor position RestoreCursor string = "\033[u" // Clear screen Codes Clear string = "\u001b[2J" ClearUp = "\u001b[1J" ClearDown = "\u001b[0J" // Clear line codes ClearL string = "\u001b[2K" ClearLLeft = "\u001b[1K" ClearLRight = "\u001b[0K" // Format string to move cursor. Needs two values: y and x (in this order). MoveTemplate = "\033[%v;%vH" )
Variables ¶
var ( // ErrUnknownColor is returned whenever wrong color name or numeric id is requested ErrUnknownColor = errors.New("error: unknown color name or id out of range [0;255]") )
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 ColorIDSprint ¶
ColorIDSprint formats using the default formats for its operands and returns the resulting string. If accepts color id in range [0;255].
func ColorSprint ¶
ColorSprint formats using the default formats for its operands and returns the resulting string. If accepts colorname (basic 16 colors).
func GetBackgroundCodeByID ¶
GetBackgroundCodeByID accepts int (from 0 to 255 inclusive) and returns ANSI escape sequence for requested background color. If id is out of range, returns an empty string.
func GetBackgroundCodeByName ¶
GetBackgroundCodeByName accepts color name (string) and returns ANSI escape sequence for requested backgrount color. If name is invalid, returns an empty string and error.
func GetColorCodeByID ¶
GetColorCodeByID accepts int (from 0 to 255 inclusive) and returns ANSI escape sequence for requested color. If id is out of range, returns an empty string.
func GetColorCodeByName ¶
GetColorCodeByName accepts color name (string) and returns ANSI escape sequence for requested color. If name is invalid, returns an empty string and error.
func GetTermSize ¶
GetTermSize rturns Current terminal size (in characters)
func MoveCursor ¶
func MoveCursor(x, y int)
MoveCursor moves cursor to the specified position on terminal screen. Will do nothing if x or y are out of bounds or we could not get size of terminal
func PrintAtPositionAndReturn ¶
func PrintAtPositionAndReturn(x, y int, a ...interface{})
PrintAtPositionAndReturn moves cursor in current terminal to the specified position, prints the string then returns to the original position (when the function was called). Will print at current cursor position if terminal size is unavailable
Types ¶
type Printer ¶
type Printer struct {
// contains filtered or unexported fields
}
Printer holds color and style settings and implements most methods as in fmt modure like Print, Println, Sprint etc. adding color and styles to the input values.
func NewColorPrinter ¶
NewColorPrinter takes color name (string) and returns Printer with font color set to the requirement. If supplied color name is invalid, the function will return an error.
func NewPrinter ¶
NewPrinter returns instance of Printer with parameters set as requested
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) 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) 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 backgtound color of printer defined by colorname input string. If colorname is not known or is empty the method will return an error.
func (*Printer) SetBackgroundID ¶
SetBackgroundID sets color of printer defined by id in range [0;255]. If id is out of range the method will return an error.
func (*Printer) SetColor ¶
SetColor sets color of printer defined by colorname input string. If colorname is not known to the library or is empty the method will return an error.
func (*Printer) SetColorID ¶
SetColorID sets color of printer defined by id in range [0;255]. If id is out of range the method will return an error.
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) ToggleReversed ¶
func (p *Printer) ToggleReversed()
ToggleReversed toggles reverse mode of Printer
func (*Printer) ToggleUnderline ¶
func (p *Printer) ToggleUnderline()
ToggleUnderline toggles underline mode of Printer