From 3d57868e9d9c69a9904b5b3541cf1c74a37793a1 Mon Sep 17 00:00:00 2001 From: Arek Date: Tue, 11 Feb 2025 22:25:39 +0100 Subject: [PATCH] chore: bump go version, comments, small fixes --- cmd/gol/gol.go | 1 + go.mod | 2 +- term/term.go | 17 +++++++++++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/cmd/gol/gol.go b/cmd/gol/gol.go index 091a6c3..769d4b3 100644 --- a/cmd/gol/gol.go +++ b/cmd/gol/gol.go @@ -34,6 +34,7 @@ func main() { loop := func() { ticker := time.NewTicker(250 * time.Millisecond) + defer ticker.Stop() for { term.GoHome() front.PrintMe() diff --git a/go.mod b/go.mod index 127e86e..3af8700 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module gol -go 1.19 +go 1.21 diff --git a/term/term.go b/term/term.go index 969dd7d..186e2db 100644 --- a/term/term.go +++ b/term/term.go @@ -2,13 +2,16 @@ 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 - CursorOff CursorFlag = false + 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) { if onOff == CursorOn { fmt.Printf("\033[?25h") @@ -17,40 +20,50 @@ func TurnCursor(onOff CursorFlag) { } } +// ClearScreen clears current screen. func ClearScreen() { fmt.Printf("\033[2J") } +// ResetTerminal resets terminal (ESC c) func ResetTerminal() { fmt.Printf("\033c") } +// GoHome moves cursor to the home position (upper left corner) func GoHome() { fmt.Printf("\033[H") } +// SaveCursor saves curent position of the SaveCursor func SaveCursor() { fmt.Printf("\033[s") } +// RestoreCursor restore cursor to the position saved previously with SaveCursor(). func RestoreCursor() { fmt.Printf("\033[u") } +// EnableAlternateScreen switches the terminal to the alternative screen. +// The alternative screen doesn't have the scroll buffer. func EnableAlternateScreen() { fmt.Printf("\033[?1049h") } +// DiableAlternateScreen switches the terminal to the primary screen. func DisableAlternateScreen() { fmt.Printf("\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()