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