2021-05-20 11:45:15 -04:00
|
|
|
// Package terminal ANSI Terminal Escape Codes and helpers
|
2021-03-24 16:23:17 -04:00
|
|
|
package terminal
|
2021-03-24 15:09:28 -04:00
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
2021-03-26 12:01:17 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// !Terminal Escape Code Sequences
|
|
|
|
// ----------------------------------------------------------------------------
|
2021-03-24 15:20:57 -04:00
|
|
|
|
2021-03-26 12:01:17 -04:00
|
|
|
const EscPrefix = "\x1b["
|
2021-03-24 15:09:28 -04:00
|
|
|
|
2021-03-26 12:01:17 -04:00
|
|
|
const (
|
2021-05-20 11:45:15 -04:00
|
|
|
// ClearLine Clears the line after the escape sequence
|
2021-03-30 21:22:28 -04:00
|
|
|
ClearLine = EscPrefix + "K"
|
2021-03-26 12:01:17 -04:00
|
|
|
|
2021-05-20 11:45:15 -04:00
|
|
|
// ClearScreen Clears the entire screen
|
2021-03-30 21:22:28 -04:00
|
|
|
ClearScreen = EscPrefix + "2J"
|
2021-03-26 12:01:17 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Cursor Codes
|
|
|
|
const (
|
2021-03-30 21:22:28 -04:00
|
|
|
HideCursor = EscPrefix + "?25l"
|
|
|
|
ShowCursor = EscPrefix + "?25h"
|
2021-03-26 12:01:17 -04:00
|
|
|
|
2021-05-20 11:45:15 -04:00
|
|
|
// LocateCursor Reports cursor location to stdout
|
2021-03-30 21:22:28 -04:00
|
|
|
LocateCursor = EscPrefix + "6n"
|
2021-03-26 12:01:17 -04:00
|
|
|
|
2021-05-20 11:45:15 -04:00
|
|
|
// ResetCursor Moves cursor to default position (1,1)
|
2021-03-30 21:22:28 -04:00
|
|
|
ResetCursor = EscPrefix + "H"
|
2021-03-26 12:01:17 -04:00
|
|
|
)
|
|
|
|
|
2021-04-01 09:41:16 -04:00
|
|
|
// Color sequences
|
|
|
|
const (
|
2021-04-07 13:08:40 -04:00
|
|
|
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"
|
2021-04-07 12:02:08 -04:00
|
|
|
DefaultFGColor = EscPrefix + "39m"
|
2021-04-07 13:08:40 -04:00
|
|
|
|
|
|
|
InvertColor = EscPrefix + "7m"
|
|
|
|
ResetColor = EscPrefix + "m"
|
2021-04-01 09:41:16 -04:00
|
|
|
)
|
|
|
|
|
2021-03-26 12:01:17 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// !Helpers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2021-05-20 11:45:15 -04:00
|
|
|
// RGBColor Generate RGB color ansi escape sequences
|
2021-04-07 13:08:40 -04:00
|
|
|
func RGBColor(r, g, b int) string {
|
|
|
|
return Code("48;2;%d;%d;%dm", r, g, b)
|
|
|
|
}
|
|
|
|
|
2021-05-20 11:45:15 -04:00
|
|
|
// Code Add the ANSI escape code prefix to the relevant escape code
|
2021-04-01 16:17:13 -04:00
|
|
|
func Code(s string, a ...interface{}) string {
|
2021-03-26 13:35:58 -04:00
|
|
|
str := fmt.Sprintf(s, a...)
|
|
|
|
|
|
|
|
return EscPrefix + str
|
2021-03-25 12:46:53 -04:00
|
|
|
}
|
|
|
|
|
2021-05-20 11:45:15 -04:00
|
|
|
// MoveCursor Generate the escape sequence to move the terminal cursor to the 0-based coordinate
|
2021-03-25 12:46:53 -04:00
|
|
|
func MoveCursor(x int, y int) string {
|
|
|
|
// Allow 0-based indexing, the terminal code is 1-based
|
|
|
|
x += 1
|
|
|
|
y += 1
|
|
|
|
|
2021-03-26 13:35:58 -04:00
|
|
|
return Code("%d;%dH", y, x)
|
2021-04-01 16:17:13 -04:00
|
|
|
}
|