gilo/internal/terminal/ansi.go
Timothy J. Warren c3b2900f42
All checks were successful
timw4mail/gilo/pipeline/head This commit looks good
Highlight numbers
2021-04-07 12:02:08 -04:00

59 lines
1.4 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 (
DefaultFGColor = EscPrefix + "39m"
FGRed = EscPrefix + "31m"
InvertColor = EscPrefix + "7m"
ResetColor = EscPrefix + "m"
)
// ----------------------------------------------------------------------------
// !Helpers
// ----------------------------------------------------------------------------
// 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)
}