gilo/internal/terminal/ansi.go
Timothy J. Warren 9c1fa5402d
All checks were successful
timw4mail/gilo/pipeline/head This commit looks good
Add more color constants
2021-04-07 13:08:40 -04:00

102 lines
2.7 KiB
Go

// ANSI Terminal Escape Codes and helpers
package terminal
import "fmt"
// ----------------------------------------------------------------------------
// !Terminal Escape Code Sequences
// ----------------------------------------------------------------------------
const EscPrefix = "\x1b["
const (
// Clears the line after the escape sequence
ClearLine = EscPrefix + "K"
// Clears the entire screen
ClearScreen = EscPrefix + "2J"
)
// Cursor Codes
const (
HideCursor = EscPrefix + "?25l"
ShowCursor = EscPrefix + "?25h"
// Reports cursor location to stdout
LocateCursor = EscPrefix + "6n"
// Moves cursor to default position (1,1)
ResetCursor = EscPrefix + "H"
)
// Color sequences
const (
// Background
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"
// Foreground
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"
// Defaults
DefaultBGColor = EscPrefix + "49m"
DefaultFGColor = EscPrefix + "39m"
InvertColor = EscPrefix + "7m"
ResetColor = EscPrefix + "m"
)
// ----------------------------------------------------------------------------
// !Helpers
// ----------------------------------------------------------------------------
// Generate RGB color ansi escape sequences
func RGBColor(r, g, b int) string {
return Code("48;2;%d;%d;%dm", r, g, b)
}
// 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
}
// 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)
}