99 lines
2.8 KiB
Go
99 lines
2.8 KiB
Go
// Package terminal ANSI Terminal Escape Codes and helpers
|
|
package terminal
|
|
|
|
import "fmt"
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// !Terminal Escape Code Sequences
|
|
// ----------------------------------------------------------------------------
|
|
|
|
const EscPrefix = "\x1b["
|
|
|
|
const (
|
|
// ClearLine Clears the line after the escape sequence
|
|
ClearLine = EscPrefix + "K"
|
|
|
|
// ClearScreen Clears the entire screen
|
|
ClearScreen = EscPrefix + "2J"
|
|
)
|
|
|
|
// Cursor Codes
|
|
const (
|
|
HideCursor = EscPrefix + "?25l"
|
|
ShowCursor = EscPrefix + "?25h"
|
|
|
|
// LocateCursor Reports cursor location to stdout
|
|
LocateCursor = EscPrefix + "6n"
|
|
|
|
// ResetCursor Moves cursor to default position (1,1)
|
|
ResetCursor = EscPrefix + "H"
|
|
)
|
|
|
|
// Color sequences
|
|
const (
|
|
BGBlack = EscPrefix + "40m"
|
|
BGRed = EscPrefix + "41m"
|
|
BGGreen = EscPrefix + "42m"
|
|
BGYellow = EscPrefix + "43m"
|
|
BGBlue = EscPrefix + "44m"
|
|
BGMagenta = EscPrefix + "45m"
|
|
BGCyan = EscPrefix + "46m"
|
|
BGWhite = EscPrefix + "47m"
|
|
BGBrightBlack = EscPrefix + "100m"
|
|
BGBrightRed = EscPrefix + "101m"
|
|
BGBrightGreen = EscPrefix + "102m"
|
|
BGBrightYellow = EscPrefix + "103m"
|
|
BGBrightBlue = EscPrefix + "104m"
|
|
BGBrightMagenta = EscPrefix + "105m"
|
|
BGBrightCyan = EscPrefix + "106m"
|
|
BGBrightWhite = EscPrefix + "107m"
|
|
|
|
FGBlack = EscPrefix + "30m"
|
|
FGRed = EscPrefix + "31m"
|
|
FGGreen = EscPrefix + "32m"
|
|
FGYellow = EscPrefix + "33m"
|
|
FGBlue = EscPrefix + "34m"
|
|
FGMagenta = EscPrefix + "35m"
|
|
FGCyan = EscPrefix + "36m"
|
|
FGWhite = EscPrefix + "37m"
|
|
FGBrightBlack = EscPrefix + "90m"
|
|
FGBrightRed = EscPrefix + "91m"
|
|
FGBrightGreen = EscPrefix + "92m"
|
|
FGBrightYellow = EscPrefix + "93m"
|
|
FGBrightBlue = EscPrefix + "94m"
|
|
FGBrightMagenta = EscPrefix + "95m"
|
|
FGBrightCyan = EscPrefix + "96m"
|
|
FGBrightWhite = EscPrefix + "97m"
|
|
|
|
DefaultBGColor = EscPrefix + "49m"
|
|
DefaultFGColor = EscPrefix + "39m"
|
|
|
|
InvertColor = EscPrefix + "7m"
|
|
ResetColor = EscPrefix + "m"
|
|
)
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// !Helpers
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// RGBColor Generate RGB color ansi escape sequences
|
|
func RGBColor(r, g, b int) string {
|
|
return Code("48;2;%d;%d;%dm", r, g, b)
|
|
}
|
|
|
|
// Code Add the ANSI escape code prefix to the relevant escape code
|
|
func Code(s string, a ...interface{}) string {
|
|
str := fmt.Sprintf(s, a...)
|
|
|
|
return EscPrefix + str
|
|
}
|
|
|
|
// MoveCursor Generate the escape sequence to move the terminal cursor to the 0-based coordinate
|
|
func MoveCursor(x int, y int) string {
|
|
// Allow 0-based indexing, the terminal code is 1-based
|
|
x += 1
|
|
y += 1
|
|
|
|
return Code("%d;%dH", y, x)
|
|
}
|