Documentation ¶
Overview ¶
Package terminal
Package terminal provides console terminal codes for windows
Index ¶
- Constants
- func BgColors() []string
- func BgRGB(r, g, b int) string
- func CancelSwap() string
- func DeleteLines(amount int) string
- func EnableVirtualTerminalProcessing(f *os.File, enable bool) error
- func EndAlternativeBuffer() string
- func Erase(amount int) string
- func EraseFrontOfLine() string
- func EraseFrontOfScreen() string
- func EraseLine() string
- func EraseRestOfLine() string
- func EraseRestOfScreen() string
- func EraseScreen() string
- func EraseShiftLeft(amount int) string
- func FgColors() []string
- func FgRGB(r, g, b int) string
- func HideConsole()
- func IsTerminal(fd int) bool
- func MoveByX(xDiff int) string
- func MoveByY(yDiff int) string
- func MoveNextLineBy(amount int) string
- func MovePreviousLineBy(amount int) string
- func MoveToX(x int) string
- func MoveToXY(x, y int) string
- func MoveToY(y int) string
- func MoveTopLeft() string
- func MoveUpScroll() string
- func ReadPassword(fd int) ([]byte, error)
- func RestorePos() string
- func SavePos() string
- func ScrollBy(yDiff int) string
- func SetBlinking(on bool) string
- func SetBright(on bool) string
- func SetCursorVisible(visible bool) string
- func SetScrollRegion(h, b int) string
- func SetUnderline(on bool) string
- func ShiftDown(amount int) string
- func ShiftRight(amount int) string
- func StartAlternativeBuffer() string
- func Swap() string
- type Terminal
- func (t *Terminal) BgRGB(r, g, b int) *Terminal
- func (t *Terminal) CancelSwap() *Terminal
- func (t *Terminal) DeleteLines(amount int) *Terminal
- func (t *Terminal) EndAlternativeBuffer() *Terminal
- func (t *Terminal) Erase(amount int) *Terminal
- func (t *Terminal) EraseFrontOfLine() *Terminal
- func (t *Terminal) EraseFrontOfScreen() *Terminal
- func (t *Terminal) EraseLine() *Terminal
- func (t *Terminal) EraseRestOfLine() *Terminal
- func (t *Terminal) EraseRestOfScreen() *Terminal
- func (t *Terminal) EraseScreen() *Terminal
- func (t *Terminal) EraseShiftLeft(amount int) *Terminal
- func (t *Terminal) FgRGB(r, g, b int) *Terminal
- func (t *Terminal) GetSize() (w, h int)
- func (t *Terminal) IsTerminal() bool
- func (t *Terminal) MoveByX(xDiff int) *Terminal
- func (t *Terminal) MoveByY(yDiff int) *Terminal
- func (t *Terminal) MoveNextLineBy(amount int) *Terminal
- func (t *Terminal) MovePreviousLineBy(amount int) *Terminal
- func (t *Terminal) MoveToX(x int) *Terminal
- func (t *Terminal) MoveToXY(x, y int) *Terminal
- func (t *Terminal) MoveToY(y int) *Terminal
- func (t *Terminal) MoveTopLeft() *Terminal
- func (t *Terminal) MoveUpScroll() *Terminal
- func (t *Terminal) OverrideOut(out io.Writer)
- func (t *Terminal) Print(a ...interface{}) (n int, err error)
- func (t *Terminal) Printf(format string, a ...interface{}) (n int, err error)
- func (t *Terminal) Println(a ...interface{}) (n int, err error)
- func (t *Terminal) Reset()
- func (t *Terminal) RestorePos() *Terminal
- func (t *Terminal) SameLinePrintf(format string, a ...interface{})
- func (t *Terminal) SavePos() *Terminal
- func (t *Terminal) ScrollBy(yDiff int) *Terminal
- func (t *Terminal) SetBlinking(on bool) *Terminal
- func (t *Terminal) SetBright(on bool) *Terminal
- func (t *Terminal) SetCursorVisible(visible bool) *Terminal
- func (t *Terminal) SetRaw(raw bool)
- func (t *Terminal) SetScrollRegion(h, b int) *Terminal
- func (t *Terminal) SetTitle(title string)
- func (t *Terminal) SetUnderline(on bool) *Terminal
- func (t *Terminal) ShiftDown(amount int) *Terminal
- func (t *Terminal) ShiftRight(amount int) *Terminal
- func (t *Terminal) StartAlternativeBuffer() *Terminal
- func (t *Terminal) Swap() *Terminal
- func (t *Terminal) Write(p []byte) (n int, err error)
Constants ¶
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 CancelSwap ¶
func CancelSwap() string
CancelSwap returns foreground/background to normal after any proceeding Swap.
func DeleteLines ¶
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 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 ¶
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 ¶
EraseShiftLeft deletes amount of characters at the current cursor position, shifting in space character from the right edge of the viewport.
func IsTerminal ¶
IsTerminal returns whether the fd passed in is a terminal or not
func MoveByX ¶
MoveByX moves cursor position by x difference. Negative means left, positive - right. Never passes the edges.
func MoveByY ¶
MoveByY moves cursor position by yDiff difference. Negative means up, positive down. Doesn't cause scrolling.
func MoveNextLineBy ¶
MoveNextLineBy moves the cursor down by amount, to the first column, without scrolling.
func MovePreviousLineBy ¶
MovePreviousLineBy moves the cursor up by amount, to the first column, without scrolling.
func MoveToXY ¶
MoveToXY moves cursor to absolute x.y. Accepts numbers from (0,0) as top left corner.
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 ¶
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 ¶
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 SetCursorVisible ¶
SetCursorVisible sets cursor visibility.
func SetScrollRegion ¶
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 ShiftDown ¶
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 ¶
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.
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 ¶
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) CancelSwap ¶
CancelSwap returns foreground/background to normal after any proceeding Swap.
func (*Terminal) DeleteLines ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
EraseLine erases the whole current line without moving the cursor. Clearing happens with the current background color.
func (*Terminal) EraseRestOfLine ¶
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 ¶
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 ¶
EraseScreen clears the while screen without moving the cursor. Clearing happens with the current background color.
func (*Terminal) EraseShiftLeft ¶
EraseShiftLeft deletes amount of characters at the current cursor position, shifting in space character from the right edge of the viewport.
func (*Terminal) GetSize ¶
GetSize reports current (width, height) of the viewport\ or 80,24 if it can't retrieve it
func (*Terminal) IsTerminal ¶
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 ¶
MoveByX moves cursor position by x difference. Negative means left, positive - right. Never passes the edges.
func (*Terminal) MoveByY ¶
MoveByY moves cursor position by yDiff difference. Negative means up, positive down. Doesn't cause scrolling.
func (*Terminal) MoveNextLineBy ¶
MoveNextLineBy moves the cursor down by amount, to the first column, without scrolling.
func (*Terminal) MovePreviousLineBy ¶
MovePreviousLineBy moves the cursor up by amount, to the first column, without scrolling.
func (*Terminal) MoveToX ¶
MoveToX moves cursor to absolute x column, starting from 0 as left-most column.
func (*Terminal) MoveToXY ¶
MoveToXY moves cursor to absolute x.y. Accepts numbers from (0,0) as top left corner.
func (*Terminal) MoveToY ¶
MoveToY moves cursor to absolute y row, starting from 0 as left-most column.
func (*Terminal) MoveTopLeft ¶
MoveTopLeft moves the cursor to absolute (0,0) corner of the screen, equals to MoveToXY(0,0).
func (*Terminal) MoveUpScroll ¶
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 (*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 ¶
RestorePos issues terminal command to restore cursor position saved previously using SavePos.
func (*Terminal) SameLinePrintf ¶
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 ¶
SavePos issues terminal command to save cursor position for upcoming RestorePos.
func (*Terminal) ScrollBy ¶
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 ¶
SetBlinking sets cursor blinking on / off.
func (*Terminal) SetCursorVisible ¶
SetCursorVisible sets cursor visibility.
func (*Terminal) SetScrollRegion ¶
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 ¶
SetTitle sets console title which will be automatically restored by Windows when program exists.
func (*Terminal) SetUnderline ¶
SetUnderline sets font with underline.
func (*Terminal) ShiftDown ¶
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 ¶
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 ¶
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.