add search function
parent
0472c24199
commit
1e7a016dca
|
@ -1,12 +1,15 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/user"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
@ -88,6 +91,42 @@ func renderNotes(s models.TownSignup) string {
|
|||
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 {
|
||||
inviteDB, err := invites.ConnectDB()
|
||||
if err != nil {
|
||||
|
@ -136,7 +175,7 @@ func _main() error {
|
|||
appView.SetDynamicColors(true)
|
||||
|
||||
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.SetTextAlign(tview.AlignCenter)
|
||||
legend.SetBackgroundColor(tcell.ColorBlack)
|
||||
|
@ -391,6 +430,28 @@ func _main() error {
|
|||
}
|
||||
pages.SwitchToPage("notate")
|
||||
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':
|
||||
app.Stop()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue