gol/term/term.go

72 lines
1.6 KiB
Go
Raw Normal View History

2025-02-11 22:30:13 +01:00
package term
import "fmt"
// CursorFlag is used as a parameter to function TurnCursor and
// describes if cursor should be displayed or not.
2025-02-11 22:30:13 +01:00
type CursorFlag bool
const (
CursorOn CursorFlag = true // Cursor should be visible
CursorOff CursorFlag = false // Cursor should be hidden
2025-02-11 22:30:13 +01:00
)
// TurnCursor turns the cursor on and off according to the parameter.
2025-02-11 22:30:13 +01:00
func TurnCursor(onOff CursorFlag) {
2025-02-11 22:25:57 +01:00
switch onOff {
case CursorOn:
fmt.Print("\033[?25h")
case CursorOff:
fmt.Print("\033[?25l")
2025-02-11 22:30:13 +01:00
}
}
// ClearScreen clears current screen.
2025-02-11 22:30:13 +01:00
func ClearScreen() {
2025-02-11 22:25:57 +01:00
fmt.Print("\033[2J")
2025-02-11 22:30:13 +01:00
}
// ResetTerminal resets terminal (ESC c)
2025-02-11 22:30:13 +01:00
func ResetTerminal() {
2025-02-11 22:25:57 +01:00
fmt.Print("\033c")
2025-02-11 22:30:13 +01:00
}
// GoHome moves cursor to the home position (upper left corner)
2025-02-11 22:30:13 +01:00
func GoHome() {
2025-02-11 22:25:57 +01:00
fmt.Print("\033[H")
2025-02-11 22:30:13 +01:00
}
// SaveCursor saves curent position of the SaveCursor
2025-02-11 22:30:13 +01:00
func SaveCursor() {
2025-02-11 22:25:57 +01:00
fmt.Print("\033[s")
2025-02-11 22:30:13 +01:00
}
// RestoreCursor restore cursor to the position saved previously with SaveCursor().
2025-02-11 22:30:13 +01:00
func RestoreCursor() {
2025-02-11 22:25:57 +01:00
fmt.Print("\033[u")
2025-02-11 22:30:13 +01:00
}
// EnableAlternateScreen switches the terminal to the alternative screen.
// The alternative screen doesn't have the scroll buffer.
2025-02-11 22:30:13 +01:00
func EnableAlternateScreen() {
2025-02-11 22:25:57 +01:00
fmt.Print("\033[?1049h")
2025-02-11 22:30:13 +01:00
}
// DiableAlternateScreen switches the terminal to the primary screen.
2025-02-11 22:30:13 +01:00
func DisableAlternateScreen() {
2025-02-11 22:25:57 +01:00
fmt.Print("\033[?1049l")
2025-02-11 22:30:13 +01:00
}
// StartFullscreen saves the cursor, enables alternate screen and clears it.
2025-02-11 22:30:13 +01:00
func StartFullscreen() {
SaveCursor()
EnableAlternateScreen()
ClearScreen()
}
// FinishFullscreen returns to the primary screen and restores the cursor.
2025-02-11 22:30:13 +01:00
func FinishFullscreen() {
DisableAlternateScreen()
RestoreCursor()
}