gilo/terminal/ansi.go
Timothy J. Warren d2a6c480a7
All checks were successful
timw4mail/gilo/pipeline/head This commit looks good
Cut down on repetition with ansi constants
2021-03-30 21:22:28 -04:00

52 lines
1.2 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"
)
// ----------------------------------------------------------------------------
// !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)
}