Compare commits

...

2 Commits

Author SHA1 Message Date
vilmibm e7ff5606f1 WIP on notating signups 2023-02-17 06:06:37 +00:00
vilmibm 41e5463756 WIP 2023-02-17 05:53:05 +00:00
1 changed files with 110 additions and 25 deletions

View File

@ -2,13 +2,16 @@ package main
import (
"encoding/json"
"errors"
"fmt"
"math/rand"
"os"
"os/user"
"path"
"strings"
"time"
tuser "git.tilde.town/tildetown/town/user"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
@ -103,6 +106,18 @@ func getSignups() ([]townSignup, error) {
}
func _main() error {
u, err := user.Current()
if err != nil {
return fmt.Errorf("that's my purse. I don't know you! %w", err)
}
isAdmin, err := tuser.IsAdmin(u)
if err != nil {
return fmt.Errorf("that's my purse. I don't know you! %w", err)
}
if !isAdmin {
return errors.New("this command can only be run by a town admin")
}
rand.Seed(time.Now().Unix())
signups, err := getSignups()
@ -112,9 +127,6 @@ func _main() error {
signupIx := 0
pages := tview.NewPages()
mainFlex := tview.NewFlex()
title := tview.NewTextView()
title.SetText(getTitle())
title.SetTextAlign(tview.AlignCenter)
@ -135,46 +147,119 @@ func _main() error {
legend.SetTextAlign(tview.AlignCenter)
legend.SetBackgroundColor(tcell.ColorBlack)
count := tview.NewTextView()
count.SetDynamicColors(true)
updateCount := func() {
count.SetText(fmt.Sprintf("[-:-:b]%d pending signups[-:-:-]", len(signups)))
}
updateCount()
bottomFlex := tview.NewFlex()
bottomFlex.SetDirection(tview.FlexColumn)
bottomFlex.AddItem(count, 0, 1, false)
bottomFlex.AddItem(legend, 0, 10, false)
mainFlex := tview.NewFlex()
mainFlex.SetDirection(tview.FlexRow)
mainFlex.AddItem(title, 1, -1, false)
mainFlex.AddItem(appView, 0, 1, true)
mainFlex.AddItem(legend, 1, -1, false)
mainFlex.AddItem(bottomFlex, 1, -1, false)
pages := tview.NewPages()
errorModal := tview.NewModal()
errorModal.AddButtons([]string{"damn"})
errorModal.SetDoneFunc(func(ix int, _ string) {
pages.SwitchToPage("main")
})
notate := tview.NewForm()
notate.AddTextArea("note", "", 80, 10, 1000, func(string) {})
notate.AddButton("submit", func() {
// TODO add note; this will require re-serializing and saving the file
pages.SwitchToPage("main")
})
notate.AddButton("cancel", func() {
pages.SwitchToPage("main")
})
pages.AddPage("main", mainFlex, true, true)
// TODO page for textarea to notate
pages.AddPage("error", errorModal, false, false)
pages.AddPage("notate", notate, true, false)
app := tview.NewApplication()
app.SetRoot(pages, true)
// TODO count of pending signups somewhere
// TODO replace imperative shit with a signupManager
advanceSignup := func() {
if len(signups) == 0 {
appView.SetText("no signups found.")
return
}
signupIx++
if signupIx == len(signups) {
signupIx = 0
}
appView.SetText(signups[signupIx].Render())
}
removeSignup := func(signup townSignup) {
newSignups := []townSignup{}
for ix, s := range signups {
if ix != signupIx {
newSignups = append(newSignups, s)
}
}
signups = newSignups
if len(signups) > 0 {
if signupIx >= len(signups) {
signupIx = 0
}
}
appView.SetText(signups[signupIx].Render())
}
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Rune() {
case 's':
signupIx++
if signupIx == len(signups) {
signupIx = 0
}
appView.SetText(signups[signupIx].Render())
advanceSignup()
case 'r':
signupIx = rand.Intn(len(signups))
appView.SetText(signups[signupIx].Render())
if len(signups) > 0 {
signupIx = rand.Intn(len(signups))
appView.SetText(signups[signupIx].Render())
}
case 'A':
err := signups[signupIx].Accept()
if err != nil {
// TODO report in app
panic(fmt.Errorf("failed to approve '%s': %w", signups[signupIx].Filename, err))
if len(signups) == 0 {
return nil
}
// TODO remove from signups list
signup := signups[signupIx]
err := signup.Accept()
if err != nil {
errorModal.SetText(fmt.Sprintf("error! failed to approve '%s': %s", signup.Filename, err.Error()))
pages.SwitchToPage("error")
return nil
}
removeSignup(signup)
updateCount()
case 'R':
err = signups[signupIx].Reject()
if err != nil {
// TODO report in app
panic(fmt.Errorf("failed to rejec t'%s': %w", signups[signupIx].Filename, err))
if len(signups) == 0 {
return nil
}
// TODO remove from signups list
case 'n':
// TODO notate
signup := signups[signupIx]
err = signup.Reject()
if err != nil {
errorModal.SetText(fmt.Sprintf("error! failed to reject '%s': %s", signup.Filename, err.Error()))
pages.SwitchToPage("error")
return nil
}
removeSignup(signup)
updateCount()
case 'N':
if len(signups) == 0 {
return nil
}
pages.SwitchToPage("notate")
return nil
case 'Q':
app.Stop()
}