add search function

trunk
equa 2023-07-12 10:33:27 -04:00
parent 0472c24199
commit 1e7a016dca
1 changed files with 62 additions and 1 deletions

View File

@ -1,12 +1,15 @@
package main package main
import ( import (
"bytes"
"database/sql" "database/sql"
"errors" "errors"
"fmt" "fmt"
"math/rand" "math/rand"
"os" "os"
"os/exec"
"os/user" "os/user"
"strconv"
"strings" "strings"
"time" "time"
@ -88,6 +91,42 @@ func renderNotes(s models.TownSignup) string {
return out return out
} }
func searchSignups(signups []*models.TownSignup) (int, error) {
escapeNuls := func(str string) string {
return strings.ReplaceAll(str, "\000", " ")
}
buf := new(bytes.Buffer)
for ix, signup := range signups {
fmt.Fprintf(buf, "%d\t%s\000", ix, escapeNuls(signup.Email))
fmt.Fprintf(buf, "%d\t%s\000", ix, escapeNuls(signup.How))
fmt.Fprintf(buf, "%d\t%s\000", ix, escapeNuls(signup.Links))
fmt.Fprintf(buf, "%d\t%s\000", ix, escapeNuls(signup.Why))
}
cmd := exec.Command("fzf", "--read0", "--delimiter=\t", "--tac", "--with-nth=2..")
cmd.Stdin = buf
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
return -1, err
}
if len(out) == 0 {
return -1, nil
}
s := strings.Split(string(out[:]), "\t")[0]
n, err := strconv.Atoi(s)
if err != nil {
return -1, err
}
return n, nil
}
func _main() error { func _main() error {
inviteDB, err := invites.ConnectDB() inviteDB, err := invites.ConnectDB()
if err != nil { if err != nil {
@ -136,7 +175,7 @@ func _main() error {
appView.SetDynamicColors(true) appView.SetDynamicColors(true)
legend := tview.NewTextView() legend := tview.NewTextView()
legend.SetText("s/S: next/prev r: random A: approve R: reject N: notate Q: quit") legend.SetText("s/S: next/prev r: random F: find A: approve R: reject N: notate Q: quit")
legend.SetTextColor(tcell.ColorPurple) legend.SetTextColor(tcell.ColorPurple)
legend.SetTextAlign(tview.AlignCenter) legend.SetTextAlign(tview.AlignCenter)
legend.SetBackgroundColor(tcell.ColorBlack) legend.SetBackgroundColor(tcell.ColorBlack)
@ -391,6 +430,28 @@ func _main() error {
} }
pages.SwitchToPage("notate") pages.SwitchToPage("notate")
return nil return nil
case 'F':
app.Suspend(func() {
ix, err := searchSignups(signups)
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
// no match or interrupt. who cares
switch exiterr.ExitCode() {
case 1: case 130:
return
}
}
errorModal.SetText(fmt.Sprintf("error! failed to search: %s", err.Error()))
pages.SwitchToPage("error")
} else if ix >= 0 {
signupIx = ix
}
})
updateCount()
render()
return nil
case 'Q': case 'Q':
app.Stop() app.Stop()
} }