terminal

package module
v0.0.0-...-d921ccd Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2021 License: MIT Imports: 10 Imported by: 0

README

Collection of utilities to output to colored terminal in a unified way.

Documentation

Overview

Package terminal

Package terminal provides console terminal codes for windows

Index

Constants

View Source
const (
	ESC = "\x1b"    // ESC consists of one 0x1b symbol.
	CSI = ESC + "[" // CSI stands for Control Sequence Introducer, used in the majority of sequences.

	FgBlack   = CSI + "30m"
	FgRed     = CSI + "31m"
	FgGreen   = CSI + "32m"
	FgYellow  = CSI + "33m"
	FgBlue    = CSI + "34m"
	FgMagenta = CSI + "35m"
	FgCyan    = CSI + "36m"
	FgWhite   = CSI + "37m"

	BgBlack   = CSI + "40m"
	BgRed     = CSI + "41m"
	BgGreen   = CSI + "42m"
	BgYellow  = CSI + "43m"
	BgBlue    = CSI + "44m"
	BgMagenta = CSI + "45m"
	BgCyan    = CSI + "46m"
	BgWhite   = CSI + "47m"

	FgHiBlack   = CSI + "90m"
	FgHiRed     = CSI + "91m"
	FgHiGreen   = CSI + "92m"
	FgHiYellow  = CSI + "93m"
	FgHiBlue    = CSI + "94m"
	FgHiMagenta = CSI + "95m"
	FgHiCyan    = CSI + "96m"
	FgHiWhite   = CSI + "97m"

	BgHiBlack   = CSI + "100m"
	BgHiRed     = CSI + "101m"
	BgHiGreen   = CSI + "102m"
	BgHiYellow  = CSI + "103m"
	BgHiBlue    = CSI + "104m"
	BgHiMagenta = CSI + "105m"
	BgHiCyan    = CSI + "106m"
	BgHiWhite   = CSI + "107m"

	// Reset returns text formatting attributes to the default state
	// prior to modification, which includes Bold, Underline, Negative
	// and color choice.
	//
	// Reset is equal to Terminal.Reset, just shorter to write in string concatenations.
	Reset = CSI + "0m"
)

Windows Terminal Sequences https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences

Variables

This section is empty.

Functions

func BgColors

func BgColors() []string

BgColors returns all supported named colors

func BgRGB

func BgRGB(r, g, b int) string

func CancelSwap

func CancelSwap() string

CancelSwap returns foreground/background to normal after any proceeding Swap.

func DeleteLines

func DeleteLines(amount int) string

DeleteLines deletes amount of lines from the buffer, starting with the row the cursor is on. Scrolling margins set with SetScrollRegion will be respected.

func EnableVirtualTerminalProcessing

func EnableVirtualTerminalProcessing(f *os.File, enable bool) error

func EndAlternativeBuffer

func EndAlternativeBuffer() string

EndAlternativeBuffer returns back to the screen before StartAlternativeBuffer as it was left off. If no modifications were made before StartAlternativeBuffer to the cursor visibility and colors, sending Reset is unnecessary. If the program interrupts in the middle, it seems necessary to implicitly call EndAlternativeBuffer, otherwise the console will print out the prompt with the alternative buffer settings, and at least in case of cmd.exe continues typing with these settings.

func Erase

func Erase(amount int) string

Erase erases amount characters from the current cursor position without moving the cursor by overwriting characters with a space character and not wrapping after reaching the right screen border.

func EraseFrontOfLine

func EraseFrontOfLine() string

EraseFrontOfLine erases from the beginning of the current line to and including current cursor position without moving the cursor. Clearing happens with the current background color.

func EraseFrontOfScreen

func EraseFrontOfScreen() string

EraseFrontOfScreen erases from the top left of the screen to and including current cursor position without moving the cursor. Clearing happens with the current background color.

func EraseLine

func EraseLine() string

EraseLine erases the whole current line without moving the cursor. Clearing happens with the current background color.

func EraseRestOfLine

func EraseRestOfLine() string

EraseRestOfLine clears from current cursor position (including) to the end of line without moving the cursor. Clearing happens with the current background color.

func EraseRestOfScreen

func EraseRestOfScreen() string

EraseRestOfScreen clears from current cursor position (including) to the right and down until the bottom right of the screen without moving the cursor. Clearing happens with the current background color.

func EraseScreen

func EraseScreen() string

EraseScreen clears the while screen without moving the cursor. Clearing happens with the current background color.

func EraseShiftLeft

func EraseShiftLeft(amount int) string

EraseShiftLeft deletes amount of characters at the current cursor position, shifting in space character from the right edge of the viewport.

func FgColors

func FgColors() []string

FgColors returns all supported named colors

func FgRGB

func FgRGB(r, g, b int) string

func HideConsole

func HideConsole()

HideConsole is only supported on windows

func IsTerminal

func IsTerminal(fd int) bool

IsTerminal returns whether the fd passed in is a terminal or not

func MoveByX

func MoveByX(xDiff int) string

MoveByX moves cursor position by x difference. Negative means left, positive - right. Never passes the edges.

func MoveByY

func MoveByY(yDiff int) string

MoveByY moves cursor position by yDiff difference. Negative means up, positive down. Doesn't cause scrolling.

func MoveNextLineBy

func MoveNextLineBy(amount int) string

MoveNextLineBy moves the cursor down by amount, to the first column, without scrolling.

func MovePreviousLineBy

func MovePreviousLineBy(amount int) string

MovePreviousLineBy moves the cursor up by amount, to the first column, without scrolling.

func MoveToX

func MoveToX(x int) string

MoveToX moves cursor to absolute x column, starting from 0 as left-most column.

func MoveToXY

func MoveToXY(x, y int) string

MoveToXY moves cursor to absolute x.y. Accepts numbers from (0,0) as top left corner.

func MoveToY

func MoveToY(y int) string

MoveToY moves cursor to absolute y row, starting from 0 as left-most column.

func MoveTopLeft

func MoveTopLeft() string

MoveTopLeft moves the cursor to absolute (0,0) corner of the screen, equals to MoveToXY(0,0).

func MoveUpScroll

func MoveUpScroll() string

MoveUpScroll ("Reverse Index") moves up maintaining x cursor position. Upon reaching the top of the screen it begins appending empty lines with the current background color.

func ReadPassword

func ReadPassword(fd int) ([]byte, error)

ReadPassword reads a line of input from a terminal without local echo. This is commonly used for inputting passwords and other sensitive data. The slice returned does not include the \n.

func RestorePos

func RestorePos() string

RestorePos issues terminal command to restore cursor position saved previously using SavePos.

func SavePos

func SavePos() string

SavePos issues terminal command to save cursor position for upcoming RestorePos.

func ScrollBy

func ScrollBy(yDiff int) string

ScrollBy will scroll from the current vertical cursor position. The text will go up for diff < 0 and down for diff > 0. Empty lines will be added to fill the gap with the current background color. The area affected can be controlled using SetScrollRegion.

func SetBlinking

func SetBlinking(on bool) string

SetBlinking sets cursor blinking on / off.

func SetBright

func SetBright(on bool) string

SetBright sets bright / bold flag to foreground color.

func SetCursorVisible

func SetCursorVisible(visible bool) string

SetCursorVisible sets cursor visibility.

func SetScrollRegion

func SetScrollRegion(h, b int) string

SetScrollRegion sets the region for scrolling using ScrollBy, by specifying top and bottom fixed areas. Scrolling also happens if \n is printed at the last line of the scroll region or MoveUpScroll at the top of it, filling the gap of the opposite side of the scrolling region with an empty line with the current background color.

h==0 means no region on top is set aside as fixed.

h==1 means first row will be fixed.

b==h-1 means now bottom region is going to be fixed during scrolling.

b==h-2 means one last row will be fixed during scrolling.

func SetUnderline

func SetUnderline(on bool) string

SetUnderline sets font with underline.

func ShiftDown

func ShiftDown(amount int) string

ShiftDown shifts the current line down by amount, adding empty line(s) to fill the gap. Scrolling margins set with SetScrollRegion will be respected.

func ShiftRight

func ShiftRight(amount int) string

ShiftRight moves current line by amount from the current column position. Spaces will be added to fill the gap, and anything going beyond the borders of viewport will be trimmed.

func StartAlternativeBuffer

func StartAlternativeBuffer() string

StartAlternativeBuffer clears the screen, moves the cursor to (0,0) and allows to return to the original buffer using EndAlternativeBuffer. This allows for isolated modifications.

func Swap

func Swap() string

Swap swaps foreground and background colors. This actually seems to swap the meaning of fg and bg and can be stacked. Output CancelSwap to return to normal.

Types

type Terminal

type Terminal struct {
	// contains filtered or unexported fields
}

Terminal is ready to use with os.Stdout if not initialized using NewTerminal with the output file description.

Special sequences are available through terminal package constants and functions. terminal package functions may be used as well if desired.

It's safe to output special sequences even if destination appears to be a non-terminal, as special sequences will be automatically discarded. Since no special meaning will be applied, like cursor moving, checking for !Terminal.IsTerminal could be utilized to provide a different formatting.

func NewTerminal

func NewTerminal(f *os.File) *Terminal

NewTerminal returns a new Terminal instance attached to specified file, typically os.Stdout.

NewTerminal(os.Stdout) is equal to simply using a Terminal instance.

func (*Terminal) BgRGB

func (t *Terminal) BgRGB(r, g, b int) *Terminal

func (*Terminal) CancelSwap

func (t *Terminal) CancelSwap() *Terminal

CancelSwap returns foreground/background to normal after any proceeding Swap.

func (*Terminal) DeleteLines

func (t *Terminal) DeleteLines(amount int) *Terminal

DeleteLines deletes amount of lines from the buffer, starting with the row the cursor is on. Scrolling margins set with SetScrollRegion will be respected.

func (*Terminal) EndAlternativeBuffer

func (t *Terminal) EndAlternativeBuffer() *Terminal

EndAlternativeBuffer returns back to the screen before StartAlternativeBuffer as it was left off. If no modifications were made before StartAlternativeBuffer to the cursor visibility and colors, sending Reset is unnecessary. If the program interrupts in the middle, it seems necessary to implicitly call EndAlternativeBuffer, otherwise the console will print out the prompt with the alternative buffer settings, and at least in case of cmd.exe continues typing with these settings.

func (*Terminal) Erase

func (t *Terminal) Erase(amount int) *Terminal

Erase erases amount characters from the current cursor position without moving the cursor by overwriting characters with a space character and not wrapping after reaching the right screen border.

func (*Terminal) EraseFrontOfLine

func (t *Terminal) EraseFrontOfLine() *Terminal

EraseFrontOfLine erases from the beginning of the current line to and including current cursor position without moving the cursor. Clearing happens with the current background color.

func (*Terminal) EraseFrontOfScreen

func (t *Terminal) EraseFrontOfScreen() *Terminal

EraseFrontOfScreen erases from the top left of the screen to and including current cursor position without moving the cursor. Clearing happens with the current background color.

func (*Terminal) EraseLine

func (t *Terminal) EraseLine() *Terminal

EraseLine erases the whole current line without moving the cursor. Clearing happens with the current background color.

func (*Terminal) EraseRestOfLine

func (t *Terminal) EraseRestOfLine() *Terminal

EraseRestOfLine clears from current cursor position (including) to the end of line without moving the cursor. Clearing happens with the current background color.

func (*Terminal) EraseRestOfScreen

func (t *Terminal) EraseRestOfScreen() *Terminal

EraseRestOfScreen clears from current cursor position (including) to the right and down until the bottom right of the screen without moving the cursor. Clearing happens with the current background color.

func (*Terminal) EraseScreen

func (t *Terminal) EraseScreen() *Terminal

EraseScreen clears the while screen without moving the cursor. Clearing happens with the current background color.

func (*Terminal) EraseShiftLeft

func (t *Terminal) EraseShiftLeft(amount int) *Terminal

EraseShiftLeft deletes amount of characters at the current cursor position, shifting in space character from the right edge of the viewport.

func (*Terminal) FgRGB

func (t *Terminal) FgRGB(r, g, b int) *Terminal

func (*Terminal) GetSize

func (t *Terminal) GetSize() (w, h int)

GetSize reports current (width, height) of the viewport\ or 80,24 if it can't retrieve it

func (*Terminal) IsTerminal

func (t *Terminal) IsTerminal() bool

IsTerminal returns true if during initialization the output has been recognized as terminal. This may be used to make quick decisions as to how to output the result.

func (*Terminal) MoveByX

func (t *Terminal) MoveByX(xDiff int) *Terminal

MoveByX moves cursor position by x difference. Negative means left, positive - right. Never passes the edges.

func (*Terminal) MoveByY

func (t *Terminal) MoveByY(yDiff int) *Terminal

MoveByY moves cursor position by yDiff difference. Negative means up, positive down. Doesn't cause scrolling.

func (*Terminal) MoveNextLineBy

func (t *Terminal) MoveNextLineBy(amount int) *Terminal

MoveNextLineBy moves the cursor down by amount, to the first column, without scrolling.

func (*Terminal) MovePreviousLineBy

func (t *Terminal) MovePreviousLineBy(amount int) *Terminal

MovePreviousLineBy moves the cursor up by amount, to the first column, without scrolling.

func (*Terminal) MoveToX

func (t *Terminal) MoveToX(x int) *Terminal

MoveToX moves cursor to absolute x column, starting from 0 as left-most column.

func (*Terminal) MoveToXY

func (t *Terminal) MoveToXY(x, y int) *Terminal

MoveToXY moves cursor to absolute x.y. Accepts numbers from (0,0) as top left corner.

func (*Terminal) MoveToY

func (t *Terminal) MoveToY(y int) *Terminal

MoveToY moves cursor to absolute y row, starting from 0 as left-most column.

func (*Terminal) MoveTopLeft

func (t *Terminal) MoveTopLeft() *Terminal

MoveTopLeft moves the cursor to absolute (0,0) corner of the screen, equals to MoveToXY(0,0).

func (*Terminal) MoveUpScroll

func (t *Terminal) MoveUpScroll() *Terminal

MoveUpScroll ("Reverse Index") moves up maintaining x cursor position. Upon reaching the top of the screen it begins appending empty lines with the current background color.

func (*Terminal) OverrideOut

func (t *Terminal) OverrideOut(out io.Writer)

func (*Terminal) Print

func (t *Terminal) Print(a ...interface{}) (n int, err error)

func (*Terminal) Printf

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

func (*Terminal) Println

func (t *Terminal) Println(a ...interface{}) (n int, err error)

func (*Terminal) Reset

func (t *Terminal) Reset()

Reset returns text formatting attributes to the default state prior to modification, which includes Bold, Underline, Negative and color choice.

For quick concatenations use terminal.Reset.

func (*Terminal) RestorePos

func (t *Terminal) RestorePos() *Terminal

RestorePos issues terminal command to restore cursor position saved previously using SavePos.

func (*Terminal) SameLinePrintf

func (t *Terminal) SameLinePrintf(format string, a ...interface{})

SameLinePrintf erases last line and outputs format with provided variables. For a non-terminal case it uses "\r", otherwise MoveToX(0).

Appending "\n" will retain this line in place during the next SameLineTerminalReport call.

func (*Terminal) SavePos

func (t *Terminal) SavePos() *Terminal

SavePos issues terminal command to save cursor position for upcoming RestorePos.

func (*Terminal) ScrollBy

func (t *Terminal) ScrollBy(yDiff int) *Terminal

ScrollBy will scroll from the current vertical cursor position. The text will go up for diff < 0 and down for diff > 0. Empty lines will be added to fill the gap with the current background color. The area affected can be controlled using SetScrollRegion.

func (*Terminal) SetBlinking

func (t *Terminal) SetBlinking(on bool) *Terminal

SetBlinking sets cursor blinking on / off.

func (*Terminal) SetBright

func (t *Terminal) SetBright(on bool) *Terminal

SetBright sets bright / bold flag to foreground color.

func (*Terminal) SetCursorVisible

func (t *Terminal) SetCursorVisible(visible bool) *Terminal

SetCursorVisible sets cursor visibility.

func (*Terminal) SetRaw

func (t *Terminal) SetRaw(raw bool)

SetRaw puts the terminal connection into raw mode or back.

func (*Terminal) SetScrollRegion

func (t *Terminal) SetScrollRegion(h, b int) *Terminal

SetScrollRegion sets the region for scrolling using ScrollBy, by specifying top and bottom fixed areas. Scrolling also happens if \n is printed at the last line of the scroll region or MoveUpScroll at the top of it, filling the gap of the opposite side of the scrolling region with an empty line with the current background color.

h==0 means no region on top is set aside as fixed.

h==1 means first row will be fixed.

b==h-1 means now bottom region is going to be fixed during scrolling.

b==h-2 means one last row will be fixed during scrolling.

func (*Terminal) SetTitle

func (t *Terminal) SetTitle(title string)

SetTitle sets console title which will be automatically restored by Windows when program exists.

func (*Terminal) SetUnderline

func (t *Terminal) SetUnderline(on bool) *Terminal

SetUnderline sets font with underline.

func (*Terminal) ShiftDown

func (t *Terminal) ShiftDown(amount int) *Terminal

ShiftDown shifts the current line down by amount, adding empty line(s) to fill the gap. Scrolling margins set with SetScrollRegion will be respected.

func (*Terminal) ShiftRight

func (t *Terminal) ShiftRight(amount int) *Terminal

ShiftRight moves current line by amount from the current column position. Spaces will be added to fill the gap, and anything going beyond the borders of viewport will be trimmed.

func (*Terminal) StartAlternativeBuffer

func (t *Terminal) StartAlternativeBuffer() *Terminal

StartAlternativeBuffer clears the screen, moves the cursor to (0,0) and allows to return to the original buffer using EndAlternativeBuffer. This allows for isolated modifications.

func (*Terminal) Swap

func (t *Terminal) Swap() *Terminal

Swap swaps foreground and background colors. This actually seems to swap the meaning of fg and bg and can be stacked. Output CancelSwap to return to normal.

func (*Terminal) Write

func (t *Terminal) Write(p []byte) (n int, err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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