gilo/internal/terminal/ansi.go

59 lines
1.4 KiB
Go
Raw Normal View History

2021-03-25 12:46:53 -04:00
// ANSI Terminal Escape Codes and helpers
2021-03-24 16:23:17 -04:00
package terminal
2021-03-24 15:09:28 -04:00
import "fmt"
2021-03-26 12:01:17 -04:00
// ----------------------------------------------------------------------------
// !Terminal Escape Code Sequences
// ----------------------------------------------------------------------------
2021-03-24 15:20:57 -04:00
2021-03-26 12:01:17 -04:00
const EscPrefix = "\x1b["
2021-03-24 15:09:28 -04:00
2021-03-26 12:01:17 -04:00
const (
// Clears the line after the escape sequence
ClearLine = EscPrefix + "K"
2021-03-26 12:01:17 -04:00
// Clears the entire screen
ClearScreen = EscPrefix + "2J"
2021-03-26 12:01:17 -04:00
)
// Cursor Codes
const (
HideCursor = EscPrefix + "?25l"
ShowCursor = EscPrefix + "?25h"
2021-03-26 12:01:17 -04:00
// Reports cursor location to stdout
LocateCursor = EscPrefix + "6n"
2021-03-26 12:01:17 -04:00
// Moves cursor to default position (1,1)
ResetCursor = EscPrefix + "H"
2021-03-26 12:01:17 -04:00
)
2021-04-01 09:41:16 -04:00
// Color sequences
const (
2021-04-07 12:02:08 -04:00
DefaultFGColor = EscPrefix + "39m"
FGRed = EscPrefix + "31m"
2021-04-01 09:41:16 -04:00
InvertColor = EscPrefix + "7m"
ResetColor = EscPrefix + "m"
2021-04-01 09:41:16 -04:00
)
2021-03-26 12:01:17 -04:00
// ----------------------------------------------------------------------------
// !Helpers
// ----------------------------------------------------------------------------
// Add the ANSI escape code prefix to the relevant escape code
2021-04-01 16:17:13 -04:00
func Code(s string, a ...interface{}) string {
2021-03-26 13:35:58 -04:00
str := fmt.Sprintf(s, a...)
return EscPrefix + str
2021-03-25 12:46:53 -04:00
}
2021-03-26 12:01:17 -04:00
// Generate the escape sequence to move the terminal cursor to the 0-based coordinate
2021-03-25 12:46:53 -04:00
func MoveCursor(x int, y int) string {
// Allow 0-based indexing, the terminal code is 1-based
x += 1
y += 1
2021-03-26 13:35:58 -04:00
return Code("%d;%dH", y, x)
2021-04-01 16:17:13 -04:00
}