Added OS-agnostic screen clearing

This commit is contained in:
raul 2024-02-19 10:56:42 +00:00
parent 4a6b4d28c7
commit 75d3b08974
3 changed files with 54 additions and 10 deletions

7
go.mod
View File

@ -1,3 +1,10 @@
module hangman module hangman
go 1.22 go 1.22
require (
github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/term v0.17.0 // indirect
)

8
go.sum Normal file
View File

@ -0,0 +1,8 @@
github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3 h1:fO9A67/izFYFYky7l1pDP5Dr0BTCRkaQJUG6Jm5ehsk=
github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3/go.mod h1:Ey4uAp+LvIl+s5jRbOHLcZpUDnkjLBROl15fZLwPlTM=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=

41
main.go
View File

@ -3,18 +3,16 @@ package main
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"github.com/inancgumus/screen"
"math/rand" "math/rand"
"os" "os"
"reflect" "reflect"
"runtime"
"strconv" "strconv"
"strings" "strings"
"time" "time"
) )
// This is a branch testing comment
// This is yet another comment for the sake of testing branches
var ( var (
player PlayerStats player PlayerStats
names []string names []string
@ -63,6 +61,8 @@ func main() {
for { for {
clear() clear()
// TODO: Use type assertions to do error handling
fmt.Printf("Welcome to the hanged man game!\nMay I know your name?\nName: ") fmt.Printf("Welcome to the hanged man game!\nMay I know your name?\nName: ")
player.name = scanLine() player.name = scanLine()
fmt.Printf("How many lives would you like to have?\nLives: ") fmt.Printf("How many lives would you like to have?\nLives: ")
@ -243,15 +243,44 @@ func getWord(path string) (word string) {
return randName return randName
} }
// TODO: Add OS agnostic clearing // func setClear() {
// switch runtime.GOOS {
// case "linux":
// fmt.Println("You're using linux")
// time.Sleep(1 * time.Second)
// clearCommand = exec.Command("clear")
// clearCommand.Stdout = os.Stdout
// case "windows":
// fmt.Println("You're using windows")
// time.Sleep(1 * time.Second)
// clearCommand = exec.Command("cmd", "/c", "cls")
// clearCommand.Stdout = os.Stdout
// default:
// fmt.Println("Huh?")
// os.Exit(1)
// }
// }
func clear() { func clear() {
fmt.Print("\033[H\033[2J") screen.Clear()
screen.MoveTopLeft()
//fmt.Print("\033[H\033[2J")
} }
func scanLine() (line string) { func scanLine() (line string) {
switch runtime.GOOS {
case "linux":
in := bufio.NewReader(os.Stdin) in := bufio.NewReader(os.Stdin)
lineNew, err := in.ReadString('\n') lineNew, err := in.ReadString('\n')
catchErr(err) catchErr(err)
line = strings.Trim(lineNew, "\n") line = strings.Trim(lineNew, "\n")
// I hate Windows
case "windows":
in := bufio.NewReader(os.Stdin)
lineNew, err := in.ReadString('\r')
catchErr(err)
line = strings.Trim(lineNew, "\r")
}
return line return line
} }