1
0
Fork 0
gilo/terminal/ansi.go

57 lines
1.3 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 (
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)
}