gilo/terminal/ansi.go

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 = "\x1b[K"
// Clears the entire screen
ClearScreen = "\x1b[2J"
)
// Cursor Codes
const (
HideCursor = "\x1b[?25l"
ShowCursor = "\x1b[?25h"
// Reports cursor location to stdout
LocateCursor = "\x1b[6n"
// Moves cursor to default position (1,1)
ResetCursor = "\x1b[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)
}