aboutsummaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authorJulien Dessaux2021-01-15 16:08:31 +0100
committerJulien Dessaux2021-01-15 16:08:31 +0100
commit3d1d8ba0a772c3c038f072095d87d0b46bd1efb2 (patch)
tree4b51303ee3a4e0c094cc3b9ab3aefc1846d85559 /client
parentHave the configuration loadFile function return a pointer (diff)
downloadshell-game-launcher-3d1d8ba0a772c3c038f072095d87d0b46bd1efb2.tar.gz
shell-game-launcher-3d1d8ba0a772c3c038f072095d87d0b46bd1efb2.tar.bz2
shell-game-launcher-3d1d8ba0a772c3c038f072095d87d0b46bd1efb2.zip
Added generation of the menu display
Diffstat (limited to '')
-rw-r--r--client/menu.go21
-rw-r--r--client/menu_test.go49
-rw-r--r--client/state.go23
-rw-r--r--client/state_test.go14
4 files changed, 107 insertions, 0 deletions
diff --git a/client/menu.go b/client/menu.go
new file mode 100644
index 0000000..773060c
--- /dev/null
+++ b/client/menu.go
@@ -0,0 +1,21 @@
+package client
+
+import "fmt"
+
+func (s *State) displayMenu() {
+ menu := s.config.Menus[s.currentMenu]
+ fmt.Println("\033[2J") // clear the screen
+ fmt.Println(menu.Banner)
+ output := ""
+ prefix := ""
+ for i := 0; i < menu.XOffset; i++ {
+ prefix += " "
+ }
+ for i := 0; i < menu.YOffset; i++ {
+ output += "\n"
+ }
+ fmt.Printf("%s", output)
+ for i := 0; i < len(menu.MenuEntries); i++ {
+ fmt.Printf("%s%s) %s\n", prefix, menu.MenuEntries[i].Key, menu.MenuEntries[i].Label)
+ }
+}
diff --git a/client/menu_test.go b/client/menu_test.go
new file mode 100644
index 0000000..8570b8d
--- /dev/null
+++ b/client/menu_test.go
@@ -0,0 +1,49 @@
+package client
+
+import (
+ "io/ioutil"
+ "os"
+ "reflect"
+ "shell-game-launcher/config"
+ "testing"
+)
+
+func TestDisplayMenu(t *testing.T) {
+ realStdout := os.Stdout
+ t.Cleanup(func() { os.Stdout = realStdout })
+ r, w, _ := os.Pipe()
+ os.Stdout = w
+
+ // Complete menu
+ state := State{
+ config: &config.Config{
+ Menus: map[string]config.Menu{
+ "test": config.Menu{
+ Banner: "TEST TEST TEST",
+ XOffset: 5,
+ YOffset: 3,
+ MenuEntries: []config.MenuEntry{
+ config.MenuEntry{
+ Key: "q",
+ Label: "quit",
+ Action: "quit",
+ },
+ },
+ },
+ },
+ },
+ currentMenu: "test",
+ login: "nil",
+ }
+ want := []byte("\033[2J\n" +
+ "TEST TEST TEST\n" +
+ "\n\n\n" +
+ " q) quit\n")
+ state.displayMenu()
+ // back to normal state
+ w.Close()
+ out, _ := ioutil.ReadAll(r)
+ if !reflect.DeepEqual(out, want) {
+ t.Fatalf("menu displayed incorrectly:\nwant:%+v\ngot: %+v", want, out)
+ }
+}
diff --git a/client/state.go b/client/state.go
new file mode 100644
index 0000000..9565efc
--- /dev/null
+++ b/client/state.go
@@ -0,0 +1,23 @@
+package client
+
+import (
+ "shell-game-launcher/config"
+)
+
+type State struct {
+ config *config.Config
+ currentMenu string
+ login string
+}
+
+func NewState(config *config.Config, login string) *State {
+ cs := State{
+ config: config,
+ currentMenu: "anonymous",
+ login: login,
+ }
+ if login != "" {
+ cs.currentMenu = "logged_in"
+ }
+ return &cs
+}
diff --git a/client/state_test.go b/client/state_test.go
new file mode 100644
index 0000000..917c211
--- /dev/null
+++ b/client/state_test.go
@@ -0,0 +1,14 @@
+package client
+
+import "testing"
+
+func TestNewState(t *testing.T) {
+ // Empty login
+ if s := NewState(nil, ""); s.currentMenu != "anonymous" {
+ t.Fatal("a new state without login should init to anonymous")
+ }
+ // logged_in
+ if s := NewState(nil, "test"); s.currentMenu != "logged_in" {
+ t.Fatal("a new state with login should init to logged_in")
+ }
+}