diff options
author | Julien Dessaux | 2021-01-18 12:33:22 +0100 |
---|---|---|
committer | Julien Dessaux | 2021-01-18 12:33:59 +0100 |
commit | 833563a66f71d7505fd0306933cbd715274ac48a (patch) | |
tree | 77bbe571981675b5a15be7cd59feeb2eaa3ba513 /client/input.go | |
parent | Improved menu display tests (diff) | |
download | shell-game-launcher-833563a66f71d7505fd0306933cbd715274ac48a.tar.gz shell-game-launcher-833563a66f71d7505fd0306933cbd715274ac48a.tar.bz2 shell-game-launcher-833563a66f71d7505fd0306933cbd715274ac48a.zip |
Added menu input gathering
Diffstat (limited to 'client/input.go')
-rw-r--r-- | client/input.go | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/client/input.go b/client/input.go new file mode 100644 index 0000000..8c814a6 --- /dev/null +++ b/client/input.go @@ -0,0 +1,26 @@ +package client + +import ( + "bufio" + "os" + + "github.com/pkg/errors" +) + +// getValidInput returns the selected menu command as a string or an error +func (s *State) getValidInput() (string, error) { + menu := s.config.Menus[s.currentMenu] + + reader := bufio.NewReader(os.Stdin) + for { + input, err := reader.ReadByte() + if err != nil { + return "", errors.Wrapf(err, "Could not read byte from stdin") + } + for _, menuEntry := range menu.MenuEntries { + if []byte(menuEntry.Key)[0] == input { + return menuEntry.Action, nil + } + } + } +} |