From 7463078ea54d07c86b9b2d92dd020fc0bf5ec26f Mon Sep 17 00:00:00 2001 From: David Florness Date: Sat, 6 Jun 2020 22:17:57 -0600 Subject: [PATCH] Basic voting UI --- ui.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/ui.go b/ui.go index 7e31430..38b4233 100644 --- a/ui.go +++ b/ui.go @@ -2,7 +2,9 @@ package main import ( "fmt" + "strconv" "strings" + "unicode" "github.com/cbergoon/merkletree" "github.com/libp2p/go-libp2p-core/peer" @@ -77,3 +79,31 @@ func joinElection() { panic(err) } } + +func vote(candidates []ElectionOption) []int { + result := make([]int, len(candidates)) + app := tview.NewApplication() + form := tview.NewForm() + for _, eo := range candidates { + // TODO: support more than 99 candidates + form.AddInputField(string(eo), "", 2, + func(textToCheck string, lastChar rune) bool { + return len(textToCheck) < 3 && unicode.IsDigit(lastChar) + }, nil) + } + form.AddButton("Submit", func() { + app.Stop() + for i := 0; i < len(candidates); i++ { + rank, err := strconv.Atoi(form.GetFormItem(i). + (*tview.InputField).GetText()) + if err != nil { + panic(err) + } + result[i] = rank + } + }) + if err := app.SetRoot(form, true).EnableMouse(true).Run(); err != nil { + panic(err) + } + return result +} -- 2.38.4