Compare commits

..

No commits in common. "e7ff5606f18f91a8dcfbdcfb0d40cab30fb26ffd" and "7e8f8cb0fc58d8efc0d6cb6f22ee2aa6d5fe9b05" have entirely different histories.

1 changed files with 23 additions and 108 deletions

View File

@ -2,16 +2,13 @@ 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"
)
@ -106,18 +103,6 @@ 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()
@ -127,6 +112,9 @@ func _main() error {
signupIx := 0
pages := tview.NewPages()
mainFlex := tview.NewFlex()
title := tview.NewTextView()
title.SetText(getTitle())
title.SetTextAlign(tview.AlignCenter)
@ -147,119 +135,46 @@ 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(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")
})
mainFlex.AddItem(legend, 1, -1, false)
pages.AddPage("main", mainFlex, true, true)
pages.AddPage("error", errorModal, false, false)
pages.AddPage("notate", notate, true, false)
// TODO page for textarea to notate
app := tview.NewApplication()
app.SetRoot(pages, true)
// TODO replace imperative shit with a signupManager
advanceSignup := func() {
if len(signups) == 0 {
appView.SetText("no signups found.")
return
}
// TODO count of pending signups somewhere
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())
}
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':
if len(signups) > 0 {
signupIx = rand.Intn(len(signups))
appView.SetText(signups[signupIx].Render())
}
case 'A':
if len(signups) == 0 {
return nil
}
signup := signups[signupIx]
err := signup.Accept()
err := signups[signupIx].Accept()
if err != nil {
errorModal.SetText(fmt.Sprintf("error! failed to approve '%s': %s", signup.Filename, err.Error()))
pages.SwitchToPage("error")
return nil
// TODO report in app
panic(fmt.Errorf("failed to approve '%s': %w", signups[signupIx].Filename, err))
}
removeSignup(signup)
updateCount()
// TODO remove from signups list
case 'R':
if len(signups) == 0 {
return nil
}
signup := signups[signupIx]
err = signup.Reject()
err = signups[signupIx].Reject()
if err != nil {
errorModal.SetText(fmt.Sprintf("error! failed to reject '%s': %s", signup.Filename, err.Error()))
pages.SwitchToPage("error")
return nil
// TODO report in app
panic(fmt.Errorf("failed to rejec t'%s': %w", signups[signupIx].Filename, err))
}
removeSignup(signup)
updateCount()
case 'N':
if len(signups) == 0 {
return nil
}
pages.SwitchToPage("notate")
return nil
// TODO remove from signups list
case 'n':
// TODO notate
case 'Q':
app.Stop()
}