From 7e8f8cb0fc58d8efc0d6cb6f22ee2aa6d5fe9b05 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Thu, 16 Feb 2023 05:48:26 +0000 Subject: [PATCH] WIP on accept/reject --- cmd/review/main.go | 68 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/cmd/review/main.go b/cmd/review/main.go index 740c769..76d4779 100644 --- a/cmd/review/main.go +++ b/cmd/review/main.go @@ -13,6 +13,12 @@ import ( "github.com/rivo/tview" ) +const ( + signupDir = "/town/signups" + acceptedDir = "/town/signups/accepted" + rejectedDir = "/town/signups/rejected" +) + func getTitle() string { titles := []string{ "yo bum rush the show", @@ -25,21 +31,37 @@ func getTitle() string { return titles[rand.Intn(len(titles))] } -const ( - signupDir = "/town/signups" - acceptedDir = "/town/signups/accepted" - rejectedDir = "/town/signups/rejected" -) - type townSignup struct { - When time.Time - Answers map[string]string + When time.Time + DecisionTime time.Time + Decision string + Filename string + Answers map[string]string } -func (a townSignup) Render() string { - out := fmt.Sprintf("[-:-:b]submitted:[-:-:-] %s\n", a.When.Format("2006-01-02 15:04")) +func (s townSignup) Accept() error { + return s.review("accept") +} - for k, v := range a.Answers { +func (s townSignup) Reject() error { + return s.review("reject") +} + +func (s townSignup) review(decision string) error { + s.DecisionTime = time.Now() + s.Decision = "decision" + oldpath := path.Join(signupDir, s.Filename) + newpath := path.Join(acceptedDir, s.Filename) + if decision == "reject" { + newpath = path.Join(rejectedDir, s.Filename) + } + return os.Rename(oldpath, newpath) +} + +func (s townSignup) Render() string { + out := fmt.Sprintf("[-:-:b]submitted:[-:-:-] %s\n", s.When.Format("2006-01-02 15:04")) + + for k, v := range s.Answers { out += fmt.Sprintf("[-:-:b]%s[-:-:-]\n", k) out += strings.TrimSpace(v) out += "\n\n" @@ -72,6 +94,7 @@ func getSignups() ([]townSignup, error) { if err != nil { return nil, fmt.Errorf("could not unmarshal signup file '%s': %w", abs, err) } + signup.Filename = entry.Name() out = append(out, signup) } @@ -95,7 +118,7 @@ func _main() error { title := tview.NewTextView() title.SetText(getTitle()) title.SetTextAlign(tview.AlignCenter) - title.SetTextColor(tcell.ColorPink) + title.SetTextColor(tcell.ColorPurple) title.SetBackgroundColor(tcell.ColorBlack) appView := tview.NewTextView() @@ -107,7 +130,10 @@ func _main() error { } legend := tview.NewTextView() - legend.SetText("TODO actions legend") + legend.SetText("s: skip r: random A: approve R: reject N: notate Q: quit") + legend.SetTextColor(tcell.ColorPurple) + legend.SetTextAlign(tview.AlignCenter) + legend.SetBackgroundColor(tcell.ColorBlack) mainFlex.SetDirection(tview.FlexRow) mainFlex.AddItem(title, 1, -1, false) @@ -120,6 +146,8 @@ func _main() error { app := tview.NewApplication() app.SetRoot(pages, true) + // TODO count of pending signups somewhere + app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { switch event.Rune() { case 's': @@ -132,9 +160,19 @@ func _main() error { signupIx = rand.Intn(len(signups)) appView.SetText(signups[signupIx].Render()) case 'A': - // TODO approve + err := signups[signupIx].Accept() + if err != nil { + // TODO report in app + panic(fmt.Errorf("failed to approve '%s': %w", signups[signupIx].Filename, err)) + } + // TODO remove from signups list case 'R': - // TODO reject + err = signups[signupIx].Reject() + if err != nil { + // TODO report in app + panic(fmt.Errorf("failed to rejec t'%s': %w", signups[signupIx].Filename, err)) + } + // TODO remove from signups list case 'n': // TODO notate case 'Q':