skip through applications

trunk
vilmibm 2023-02-16 05:08:31 +00:00
parent 327e0551b9
commit b03e4f069e
1 changed files with 48 additions and 18 deletions

View File

@ -1,9 +1,11 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"math/rand" "math/rand"
"os" "os"
"path"
"strings" "strings"
"time" "time"
@ -29,56 +31,79 @@ const (
rejectedDir = "/town/signups/rejected" rejectedDir = "/town/signups/rejected"
) )
// TODO copypasta from signup cmd type townSignup struct {
type townApplication struct {
When time.Time When time.Time
Answers map[string]string Answers map[string]string
} }
func (a townApplication) Render() string { func (a townSignup) Render() string {
// TODO out := fmt.Sprintf("[-:-:b]submitted:[-:-:-] %s\n", a.When.Format("2006-01-02 15:04"))
return "TODO"
for k, v := range a.Answers {
out += fmt.Sprintf("[-:-:b]%s[-:-:-]\n", k)
out += strings.TrimSpace(v)
out += "\n\n"
}
return out
} }
func getApplications() ([]townApplication, error) { func getSignups() ([]townSignup, error) {
entries, err := os.ReadDir(signupDir) entries, err := os.ReadDir(signupDir)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to read '%s': %w", signupDir, err) return nil, fmt.Errorf("failed to read '%s': %w", signupDir, err)
} }
out := []townApplication{} out := []townSignup{}
for _, entry := range entries { for _, entry := range entries {
if !strings.HasSuffix(entry.Name(), "json") { if !strings.HasSuffix(entry.Name(), "json") {
continue continue
} }
// TODO read file
// TODO unmarshal
// TODO append
abs := path.Join(signupDir, entry.Name())
data, err := os.ReadFile(abs)
if err != nil {
return nil, fmt.Errorf("could not read signup file '%s': %w", abs, err)
}
fmt.Println(string(data))
var signup townSignup
err = json.Unmarshal(data, &signup)
if err != nil {
return nil, fmt.Errorf("could not unmarshal signup file '%s': %w", abs, err)
}
out = append(out, signup)
} }
// TODO
return out, nil return out, nil
} }
func _main() error { func _main() error {
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
applications, err := getApplications() signups, err := getSignups()
if err != nil { if err != nil {
return fmt.Errorf("could not get applications: %w", err) return fmt.Errorf("could not get signups: %w", err)
} }
signupIx := 0
pages := tview.NewPages() pages := tview.NewPages()
mainFlex := tview.NewFlex() mainFlex := tview.NewFlex()
title := tview.NewTextView() title := tview.NewTextView()
title.SetText(getTitle()) title.SetText(getTitle())
title.SetTextAlign(tview.AlignCenter)
title.SetTextColor(tcell.ColorPink)
title.SetBackgroundColor(tcell.ColorBlack)
appView := tview.NewTextView() appView := tview.NewTextView()
if len(applications) == 0 { appView.SetDynamicColors(true)
appView.SetText("no applications found.") if len(signups) == 0 {
appView.SetText("no signups found.")
} else { } else {
appView.SetText(applications[0].Render()) appView.SetText(signups[signupIx].Render())
} }
legend := tview.NewTextView() legend := tview.NewTextView()
@ -98,9 +123,14 @@ func _main() error {
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Rune() { switch event.Rune() {
case 's': case 's':
// TODO skip signupIx++
if signupIx == len(signups) {
signupIx = 0
}
appView.SetText(signups[signupIx].Render())
case 'r': case 'r':
// TODO skip to random signupIx = rand.Intn(len(signups))
appView.SetText(signups[signupIx].Render())
case 'A': case 'A':
// TODO approve // TODO approve
case 'R': case 'R':