Skip to content

Commit c8d0cf0

Browse files
authored
minor refactoring; cmd/go-wordles -> game (#6)
1 parent b6e8726 commit c8d0cf0

File tree

14 files changed

+84
-91
lines changed

14 files changed

+84
-91
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ default: run
1111
all: test
1212

1313
build:
14-
go build ./cmd/go-wordle
14+
go build .
1515

1616
dist:
17-
gox -ldflags="-s -w -X main.version=${VERSION}" \
17+
gox -ldflags="-s -w -X github.com/jedib0t/go-wordle/game.version=${VERSION}" \
1818
-os="linux darwin windows" \
1919
-arch="amd64" \
2020
-output="./dist/{{.Dir}}_{{.OS}}_{{.Arch}}" \
21-
./cmd/go-wordle
21+
.
2222

2323
fmt:
2424
go fmt $(shell go list ./...)
@@ -31,7 +31,7 @@ lint:
3131
golint -set_exit_status $(shell go list ./...)
3232

3333
run:
34-
go run ./cmd/go-wordle
34+
go run .
3535

3636
test: fmt tidy lint vet build
3737
go test -cover -coverprofile=.coverprofile $(shell go list ./...)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ page.
1818

1919
If you want to build your own using GoLang:
2020

21-
* `go get -u github.com/jedib0t/go-wordle/cmd/go-wordle`
21+
* `go install github.com/jedib0t/go-wordle`
2222
* `go-wordle`
2323

2424
If you want to run from source:

cmd/go-wordle/actions.go renamed to game/actions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package game
22

33
import (
44
"os"

cmd/go-wordle/flags.go renamed to game/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package game
22

33
import (
44
"flag"

cmd/go-wordle/game.go renamed to game/game.go

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package game
22

33
import (
44
"strings"
@@ -22,39 +22,10 @@ var (
2222
hints []string
2323
)
2424

25-
func getUserInput() {
26-
char, key, err := keyboard.GetSingleKey()
27-
if err != nil {
28-
logErrorAndExit("failed to get input: %v", err)
29-
}
30-
31-
switch key {
32-
case keyboard.KeyEsc, keyboard.KeyCtrlC:
33-
handleActionQuit()
34-
case keyboard.KeyCtrlD:
35-
handleActionDecrementAttempts()
36-
case keyboard.KeyCtrlI:
37-
handleActionIncrementAttempts()
38-
case keyboard.KeyCtrlR:
39-
handleActionReset()
40-
case keyboard.KeyBackspace, keyboard.KeyBackspace2:
41-
handleActionBackSpace()
42-
case keyboard.KeyEnter:
43-
if inputCharStatus {
44-
handleActionAttemptStatus()
45-
} else {
46-
handleActionAttempt()
47-
}
48-
default:
49-
if inputCharStatus {
50-
handleActionInputStatus(char)
51-
} else {
52-
handleActionInput(char)
53-
}
54-
}
55-
}
56-
57-
func play() {
25+
// Play starts the game.
26+
func Play() {
27+
defer cleanup()
28+
generateWordles(*flagNumWordles)
5829
cliAttempts := strings.Split(*flagAttempts, ",")
5930
currAttempts = make([]wordle.Attempt, len(wordles))
6031
hints = wordle.CombineHints(wordles...)
@@ -106,16 +77,34 @@ func play() {
10677
wg.Wait()
10778
}
10879

109-
func renderAsync(chStop chan bool, wg *sync.WaitGroup) {
110-
defer wg.Done()
111-
timer := time.Tick(time.Second / time.Duration(*flagRefreshRate))
112-
for {
113-
select {
114-
case <-chStop: // render one final time and return
115-
render(wordles, hints, currAttempts)
116-
return
117-
case <-timer: // render as part of regular cycle
118-
render(wordles, hints, currAttempts)
80+
func getUserInput() {
81+
char, key, err := keyboard.GetSingleKey()
82+
if err != nil {
83+
logErrorAndExit("failed to get input: %v", err)
84+
}
85+
86+
switch key {
87+
case keyboard.KeyEsc, keyboard.KeyCtrlC:
88+
handleActionQuit()
89+
case keyboard.KeyCtrlD:
90+
handleActionDecrementAttempts()
91+
case keyboard.KeyCtrlI:
92+
handleActionIncrementAttempts()
93+
case keyboard.KeyCtrlR:
94+
handleActionReset()
95+
case keyboard.KeyBackspace, keyboard.KeyBackspace2:
96+
handleActionBackSpace()
97+
case keyboard.KeyEnter:
98+
if inputCharStatus {
99+
handleActionAttemptStatus()
100+
} else {
101+
handleActionAttempt()
102+
}
103+
default:
104+
if inputCharStatus {
105+
handleActionInputStatus(char)
106+
} else {
107+
handleActionInput(char)
119108
}
120109
}
121110
}

cmd/go-wordle/header_footer.go renamed to game/header_footer.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package game
22

33
import (
44
"fmt"
@@ -20,25 +20,13 @@ const (
2020
// U+259x ▐ ░ ▒ ▓ ▔ ▕ ▖ ▗ ▘ ▙ ▚ ▛ ▜ ▝ ▞ ▟
2121

2222
titleText1 = `
23-
▞ ▛▀▀▀▀▀▀▀▀▀▀▀▀▀▜ ▚
24-
░ ▒ ▓ █ ▌ W O R D L E ▐ █ ▓ ▒ ░
25-
▚ ▙▄▄▄▄▄▄▄▄▄▄▄▄▄▟ ▞
26-
`
27-
titleTextBig = `
28-
░░ ░░ ░░░░░░ ░░░░░░ ░░░░░░ ░░ ░░░░░░░
29-
▒▒ ▒▒ ▒▒ ▒▒ ▒▒ ▒▒ ▒▒ ▒▒ ▒▒ ▒▒
30-
▒▒ ▒ ▒▒ ▒▒ ▒▒ ▒▒▒▒▒▒ ▒▒ ▒▒ ▒▒ ▒▒▒▒▒
31-
▓▓ ▓▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓
32-
███ ███ ██████ ██ ██ ██████ ███████ ███████
33-
`
34-
titleTextScary = `
3523
▄▄▌ ▐ ▄▌ ▄▄▄ ·▄▄▄▄ ▄▄▌ ▄▄▄ .
3624
██· █▌▐█ ▄█▀▄ ▀▄ █·██· ██ ██• ▀▄.▀·
3725
██▪▐█▐▐▌▐█▌.▐▌▐▀▀▄ ▐█▪ ▐█▌██ ▪ ▐▀▀▪▄
3826
▐█▌██▐█▌▐█▌.▐▌▐█•█▌██. ██ ▐█▌ ▄▐█▄▄▌
3927
▀▀▀▀ ▀▪ ▀█▄▀▪.▀ ▀▀▀▀▀▀• .▀▀▀ ▀▀▀
4028
`
41-
titleText = titleTextScary
29+
titleText = titleText1
4230
)
4331

4432
var (

cmd/go-wordle/helpers.go renamed to game/helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package game
22

33
import (
44
"strings"

cmd/go-wordle/keyboard.go renamed to game/keyboard.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package game
22

33
import (
44
"fmt"

cmd/go-wordle/main.go renamed to game/main.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
package main
1+
package game
22

33
import (
4-
"fmt"
54
"math/rand"
65
"os"
76
"os/signal"
8-
"strings"
97
"syscall"
108
"time"
119
)
@@ -46,17 +44,3 @@ func cleanup() {
4644
exitHandler()
4745
}
4846
}
49-
50-
func logErrorAndExit(msg string, a ...interface{}) {
51-
_, _ = fmt.Fprintf(os.Stderr, "ERROR: "+strings.TrimSpace(msg)+"\n", a...)
52-
cleanup()
53-
os.Exit(-1)
54-
}
55-
56-
func main() {
57-
defer cleanup()
58-
59-
generateWordles(*flagNumWordles)
60-
61-
play()
62-
}

cmd/go-wordle/render.go renamed to game/render.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package main
1+
package game
22

33
import (
44
"fmt"
55
"strings"
66
"sync"
7+
"time"
78

89
"github.com/jedib0t/go-pretty/v6/table"
910
"github.com/jedib0t/go-pretty/v6/text"
@@ -37,6 +38,20 @@ var (
3738
renderMutex = sync.Mutex{}
3839
)
3940

41+
func renderAsync(chStop chan bool, wg *sync.WaitGroup) {
42+
defer wg.Done()
43+
timer := time.Tick(time.Second / time.Duration(*flagRefreshRate))
44+
for {
45+
select {
46+
case <-chStop: // render one final time and return
47+
render(wordles, hints, currAttempts)
48+
return
49+
case <-timer: // render as part of regular cycle
50+
render(wordles, hints, currAttempts)
51+
}
52+
}
53+
}
54+
4055
func render(wordles []wordle.Wordle, hints []string, currAttempts []wordle.Attempt) {
4156
renderMutex.Lock()
4257
defer renderMutex.Unlock()
@@ -197,9 +212,9 @@ func renderKeyboardLegend(wordles []wordle.Wordle) string {
197212
}
198213

199214
func renderKeyboardShortcuts() string {
200-
shortcuts := "escape/ctrl+c to quit; ctrl+r to restart"
215+
shortcuts := "quit: <ESC>/<Ctrl+C> | restart: <Ctrl+R>"
201216
if *flagSolve {
202-
shortcuts = "escape/ctrl+c to quit"
217+
shortcuts = "quit: <ESC>/<Ctrl+C>"
203218
}
204219
shortcuts = text.AlignCenter.Apply(shortcuts, 56)
205220
return colorHints.Sprint(shortcuts)

0 commit comments

Comments
 (0)