1
0
Fork 0
gilo/terminal/ansi.go

50 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) string {
return fmt.Sprintf("%s%s", EscPrefix, s)
}
// 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(fmt.Sprintf("%d;%dH", y, x))
}