battleship-cli/tools.go

41 lines
635 B
Go
Raw Normal View History

2024-03-11 08:43:14 +01:00
package main
import (
"bufio"
2024-03-11 08:43:14 +01:00
"fmt"
"os"
"runtime"
"strings"
2024-03-11 08:43:14 +01:00
)
func catchErr(err error) (errHappened bool) {
errHappened = false
if err != nil {
errHappened = true
fmt.Println(err)
os.Exit(2)
}
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
}