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