@@ 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
+}