Fix bug where it was impossible to search for an empty string in the TUI

This commit is contained in:
David Dworken 2022-10-21 23:29:49 -07:00
parent b8039e6d8a
commit bfac5198ec

View File

@ -50,8 +50,8 @@ type model struct {
// The search box for the query // The search box for the query
queryInput textinput.Model queryInput textinput.Model
// The query to run. Reset to an empty string after it was run. // The query to run. Reset to nil after it was run.
runQuery string runQuery *string
// The previous query that was run. // The previous query that was run.
lastQuery string lastQuery string
@ -84,7 +84,7 @@ func initialModel(ctx *context.Context, t table.Model, initialQuery string, numE
if initialQuery != "" { if initialQuery != "" {
queryInput.SetValue(initialQuery) queryInput.SetValue(initialQuery)
} }
return model{ctx: ctx, spinner: s, isLoading: true, table: t, runQuery: initialQuery, queryInput: queryInput, numEntries: numEntries} return model{ctx: ctx, spinner: s, isLoading: true, table: t, runQuery: &initialQuery, queryInput: queryInput, numEntries: numEntries}
} }
func (m model) Init() tea.Cmd { func (m model) Init() tea.Cmd {
@ -92,9 +92,8 @@ func (m model) Init() tea.Cmd {
} }
func runQueryAndUpdateTable(m model) model { func runQueryAndUpdateTable(m model) model {
// TODO: this has the bug where if you search for x, and then hit backspace to an empty search query it won't rerun if m.runQuery != nil && *m.runQuery != m.lastQuery {
if m.runQuery != "" && m.runQuery != m.lastQuery { rows, numEntries, err := getRows(m.ctx, *m.runQuery)
rows, numEntries, err := getRows(m.ctx, m.runQuery)
if err != nil { if err != nil {
m.searchErr = err m.searchErr = err
return m return m
@ -104,8 +103,8 @@ func runQueryAndUpdateTable(m model) model {
m.numEntries = numEntries m.numEntries = numEntries
m.table.SetRows(rows) m.table.SetRows(rows)
m.table.SetCursor(0) m.table.SetCursor(0)
m.lastQuery = m.runQuery m.lastQuery = *m.runQuery
m.runQuery = "" m.runQuery = nil
} }
if m.table.Cursor() >= m.numEntries { if m.table.Cursor() >= m.numEntries {
// Ensure that we can't scroll past the end of the table // Ensure that we can't scroll past the end of the table
@ -131,7 +130,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.table = t m.table = t
i, cmd2 := m.queryInput.Update(msg) i, cmd2 := m.queryInput.Update(msg)
m.queryInput = i m.queryInput = i
m.runQuery = m.queryInput.Value() searchQuery := m.queryInput.Value()
m.runQuery = &searchQuery
m = runQueryAndUpdateTable(m) m = runQueryAndUpdateTable(m)
return m, tea.Batch(cmd1, cmd2) return m, tea.Batch(cmd1, cmd2)
} }