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 ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"math/rand" "math/rand"
"os" "os"
"os/user"
"path" "path"
"strings" "strings"
"time" "time"
tuser "git.tilde.town/tildetown/town/user"
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/rivo/tview" "github.com/rivo/tview"
) )
@ -103,6 +106,18 @@ func getSignups() ([]townSignup, error) {
} }
func _main() 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()) rand.Seed(time.Now().Unix())
signups, err := getSignups() signups, err := getSignups()
@ -112,9 +127,6 @@ func _main() error {
signupIx := 0 signupIx := 0
pages := tview.NewPages()
mainFlex := tview.NewFlex()
title := tview.NewTextView() title := tview.NewTextView()
title.SetText(getTitle()) title.SetText(getTitle())
title.SetTextAlign(tview.AlignCenter) title.SetTextAlign(tview.AlignCenter)
@ -135,46 +147,119 @@ func _main() error {
legend.SetTextAlign(tview.AlignCenter) legend.SetTextAlign(tview.AlignCenter)
legend.SetBackgroundColor(tcell.ColorBlack) 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.SetDirection(tview.FlexRow)
mainFlex.AddItem(title, 1, -1, false) mainFlex.AddItem(title, 1, -1, false)
mainFlex.AddItem(appView, 0, 1, true) 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) 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 := tview.NewApplication()
app.SetRoot(pages, true) app.SetRoot(pages, true)
// TODO count of pending signups somewhere // TODO replace imperative shit with a signupManager
advanceSignup := func() {
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { if len(signups) == 0 {
switch event.Rune() { appView.SetText("no signups found.")
case 's': return
}
signupIx++ signupIx++
if signupIx == len(signups) { if signupIx == len(signups) {
signupIx = 0 signupIx = 0
} }
appView.SetText(signups[signupIx].Render()) 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':
advanceSignup()
case 'r': case 'r':
if len(signups) > 0 {
signupIx = rand.Intn(len(signups)) signupIx = rand.Intn(len(signups))
appView.SetText(signups[signupIx].Render()) appView.SetText(signups[signupIx].Render())
}
case 'A': case 'A':
err := signups[signupIx].Accept() if len(signups) == 0 {
if err != nil { return nil
// TODO report in app
panic(fmt.Errorf("failed to approve '%s': %w", signups[signupIx].Filename, err))
} }
// 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': case 'R':
err = signups[signupIx].Reject() if len(signups) == 0 {
if err != nil { return nil
// TODO report in app
panic(fmt.Errorf("failed to rejec t'%s': %w", signups[signupIx].Filename, err))
} }
// TODO remove from signups list signup := signups[signupIx]
case 'n': err = signup.Reject()
// TODO notate 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': case 'Q':
app.Stop() app.Stop()
} }