termtools

package module
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 28, 2021 License: MIT Imports: 3 Imported by: 1

README

module termtools

termtools is basically a collection utilities to style terminal output and also some utility functions to move cursor around, clear screen, delete lines of text etc.

First of all you can use can use ANSI escapes directly: attach prefix of color code and suffix of color reset to your string like this:

s := termtools.Red + "We're gophers" + termtools.ColorReset

More common and clever way would be to use Printer

var pinter Printer
printer.SetColorID(9)
printer.Println("This will output in orange")

Printer implements most print methods in the same way as fmt package from standard library. You can call Println, Print, Sprint, Sprintf, Errorf. Methods implemented by Printer have same signatures as those if fmt module. In fact Printer wraps those methods only adding required ANSI escapes to the original values passed and does nothing more than that.

For a detailed listing of functions and mthods signatures see:

A demo program is included in sample directory. Here's the listing:

package main

import (
	"fmt"

	"github.com/dmfed/termtools"
)

func main() {
	// How to use Printer
	var p termtools.Printer         // initializes Printer
	p.SetColorID(9)                 // Sets color to orange
	termtools.ClearScreen()         //clears screen
	x, y := termtools.GetTermSize() //gets size of terminal window
	s := p.Sprint("We are gophers")
	// Below line outputs string s in the middle of screen and returns cursor to initial position
	termtools.PrintAtPositionAndReturn(x/2-len(s)/2, y/2, s)
	p.ToggleUnderline()
	p.SetColor("green")
	p.Println("We love Go")

	// Simplest use is to call ColorSprint (same signature as in fmt.Sprint)
	mystring := termtools.ColorSprint("red", "This will print in red")
	fmt.Println(mystring)
}

Documentation

Overview

Package termtools provides basic functionality to style console output in Linux.

Copyright 2021 Dmitry Fedotov

Index

Constants

View Source
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

View Source
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 ClearLine

func ClearLine()

ClearLine deletes the whole line of text

func ClearLineLeft

func ClearLineLeft()

ClearLineLeft deletes line left of cursor position

func ClearLineRight

func ClearLineRight()

ClearLineRight deletes line right of cursor position

func ClearScreen

func ClearScreen()

ClearScreen clears screen

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

func ColorIDSprint(colorcode int, a ...interface{}) string

ColorIDSprint formats using the default formats for its operands and returns the resulting string. If accepts color id in range [0;255].

func ColorSprint

func ColorSprint(colorname string, a ...interface{}) string

ColorSprint formats using the default formats for its operands and returns the resulting string. If accepts colorname (basic 16 colors).

func GetBackgroundCodeByID

func GetBackgroundCodeByID(id int) (string, error)

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

func GetBackgroundCodeByName(colorname string) (string, error)

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

func GetColorCodeByID(id int) (string, error)

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

func GetColorCodeByName(colorname string) (string, error)

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

func GetTermSize() (int, int)

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

func NewColorPrinter(color string) (*Printer, error)

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

func NewPrinter(color, background string, bold, underline, reversed bool) (*Printer, error)

NewPrinter returns instance of Printer with parameters set as requested

func (*Printer) Errorf

func (p *Printer) Errorf(format string, a ...interface{}) error

Errorf formats according to a format specifier and returns the string as a value that satisfies error.

func (*Printer) Fprint

func (p *Printer) Fprint(w io.Writer, a ...interface{}) (n int, err error)

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

func (p *Printer) Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)

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

func (p *Printer) Fprintln(w io.Writer, a ...interface{}) (n int, err error)

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

func (p *Printer) Print(a ...interface{}) (n int, err error)

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

func (p *Printer) Printf(format string, a ...interface{}) (n int, err error)

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

func (p *Printer) Println(a ...interface{}) (n int, err error)

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

func (p *Printer) SetBackground(colorname string) error

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

func (p *Printer) SetBackgroundID(id int) error

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

func (p *Printer) SetColor(colorname string) error

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

func (p *Printer) SetColorID(id int) error

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

func (p *Printer) Sprint(a ...interface{}) string

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

func (p *Printer) Sprintf(format string, a ...interface{}) string

Sprintf formats according to a format specifier and returns the resulting string.

func (*Printer) Sprintln

func (p *Printer) Sprintln(a ...interface{}) string

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) ToggleBold

func (p *Printer) ToggleBold()

ToggleBold toggles bold 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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL