~edwargix/tallyard

7463078ea54d07c86b9b2d92dd020fc0bf5ec26f — David Florness 5 years ago fe6d78f
Basic voting UI
1 files changed, 30 insertions(+), 0 deletions(-)

M ui.go
M ui.go => ui.go +30 -0
@@ 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
}