From 3e8313b1182c4e49b6cfd94af9f8b44896f1be28 Mon Sep 17 00:00:00 2001 From: raul Date: Mon, 11 Mar 2024 21:46:09 +0100 Subject: [PATCH] Adding utilities file Gonna keep reusing these text scanning and screen clearing functions as much as I can --- tools.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tools.go b/tools.go index b99c7c4..a90783d 100644 --- a/tools.go +++ b/tools.go @@ -1,8 +1,11 @@ package main import ( + "bufio" "fmt" "os" + "runtime" + "strings" ) func catchErr(err error) (errHappened bool) { @@ -14,3 +17,24 @@ func catchErr(err error) (errHappened bool) { } return errHappened } + +func clear() { + fmt.Print("\033[H\033[2J") +} + +func scanLine() (line string) { + switch runtime.GOOS { + case "linux": + in := bufio.NewReader(os.Stdin) + lineNew, err := in.ReadString('\n') + catchErr(err) + line = strings.Trim(lineNew, "\n") + + case "windows": + in := bufio.NewReader(os.Stdin) + lineNew, err := in.ReadString('\r') + catchErr(err) + line = strings.Trim(lineNew, "\r") + } + return line +}