From 833563a66f71d7505fd0306933cbd715274ac48a Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Mon, 18 Jan 2021 12:33:22 +0100 Subject: Added menu input gathering --- client/input.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 client/input.go (limited to 'client/input.go') 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 + } + } + } +} -- cgit v1.2.3