From a3680aab26a51d5b3cf9f177d711e5360174fc82 Mon Sep 17 00:00:00 2001 From: IdlePhysicist Date: Mon, 17 Aug 2020 23:36:48 +0100 Subject: [PATCH 1/2] First commit of advanced search Most string columns can now be selected and searched for. It could be done for other numerical and boolean fields too. But it will not be implemented for all fields. --- internal/gui/cavers.go | 26 ++++++++++++++++++++++---- internal/gui/caves.go | 31 +++++++++++++++++++++++++++---- internal/gui/keybindings.go | 14 +++++++++++--- internal/gui/panel.go | 2 +- internal/gui/trips.go | 23 ++++++++++++++++++----- 5 files changed, 79 insertions(+), 17 deletions(-) diff --git a/internal/gui/cavers.go b/internal/gui/cavers.go index bf05417..cb3e67d 100644 --- a/internal/gui/cavers.go +++ b/internal/gui/cavers.go @@ -14,7 +14,7 @@ import ( type cavers struct { *tview.Table cavers chan *model.Caver - filterWord string + filterCol, filterTerm string } func newCavers(g *Gui) *cavers { @@ -108,7 +108,7 @@ func (c *cavers) entries(g *Gui) { var filteredCavers []*model.Caver for _, caver := range cavers { - if strings.Index(caver.Name, c.filterWord) == -1 { + if c.search(caver) { continue } filteredCavers = append(filteredCavers, caver) @@ -125,8 +125,9 @@ func (c *cavers) unfocus() { c.SetSelectable(false, false) } -func (c *cavers) setFilterWord(word string) { - c.filterWord = word +func (c *cavers) setFilter(col, term string) { + c.filterCol = col + c.filterTerm = term } func (c *cavers) monitoringCavers(g *Gui) { @@ -157,3 +158,20 @@ func (g *Gui) uniqueClubs(input []*model.Caver) []string { return uniq } + +func (c *cavers) search(caver *model.Caver) bool { + switch c.filterCol { + case "name": + if strings.Index(caver.Name, c.filterTerm) == -1 { + return true + } + return false + case "club": + if strings.Index(caver.Club, c.filterTerm) == -1 { + return true + } + return false + default: + return false + } +} diff --git a/internal/gui/caves.go b/internal/gui/caves.go index 8024312..592706a 100644 --- a/internal/gui/caves.go +++ b/internal/gui/caves.go @@ -14,7 +14,7 @@ import ( type caves struct { *tview.Table caves chan *model.Cave - filterWord string + filterCol, filterTerm string } func newCaves(g *Gui) *caves { @@ -63,7 +63,7 @@ func (c *caves) entries(g *Gui) { var filteredCaves []*model.Cave for _, cave := range caves { - if strings.Index(cave.Name, c.filterWord) == -1 { + if c.search(cave) { continue } filteredCaves = append(filteredCaves, cave) @@ -137,8 +137,9 @@ func (c *caves) unfocus() { c.SetSelectable(false, false) } -func (c *caves) setFilterWord(word string) { - c.filterWord = word +func (c *caves) setFilter(col, term string) { + c.filterCol = col + c.filterTerm = term } func (c *caves) monitoringCaves(g *Gui) { @@ -191,3 +192,25 @@ func yesOrNo(val bool) string { return `N` } } + +func (c *caves) search(cave *model.Cave) bool { + switch c.filterCol { + case "name": + if strings.Index(cave.Name, c.filterTerm) == -1 { + return true + } + return false + case "region": + if strings.Index(cave.Region, c.filterTerm) == -1 { + return true + } + return false + case "country": + if strings.Index(cave.Country, c.filterTerm) == -1 { + return true + } + return false + default: + return false + } +} diff --git a/internal/gui/keybindings.go b/internal/gui/keybindings.go index c06bffc..08bf2ee 100644 --- a/internal/gui/keybindings.go +++ b/internal/gui/keybindings.go @@ -1,6 +1,8 @@ package gui import ( + "strings" + "github.com/gdamore/tcell" "github.com/rivo/tview" ) @@ -27,7 +29,7 @@ func (g *Gui) setGlobalKeybinding(event *tcell.EventKey) { func (g *Gui) filter() { currentPanel := g.state.panels.panel[g.state.panels.currentPanel] - currentPanel.setFilterWord("") + currentPanel.setFilter("", "") currentPanel.updateEntries(g) viewName := "filter" @@ -55,8 +57,14 @@ func (g *Gui) filter() { }) searchInput.SetChangedFunc(func(text string) { - currentPanel.setFilterWord(text) - currentPanel.updateEntries(g) + if strings.Contains(text, "/") { + textSl := strings.Split(text, "/") + + if len(textSl) == 2 { + currentPanel.setFilter(textSl[0], textSl[1]) + currentPanel.updateEntries(g) + } + } }) g.pages.AddAndSwitchToPage(viewName, g.modal(searchInput, 80, 3), true).ShowPage("main") diff --git a/internal/gui/panel.go b/internal/gui/panel.go index 0e1becc..aa0c718 100644 --- a/internal/gui/panel.go +++ b/internal/gui/panel.go @@ -8,5 +8,5 @@ type panel interface { setKeybinding(*Gui) focus(*Gui) unfocus() - setFilterWord(string) + setFilter(string, string) } diff --git a/internal/gui/trips.go b/internal/gui/trips.go index b126bcf..e573465 100644 --- a/internal/gui/trips.go +++ b/internal/gui/trips.go @@ -12,8 +12,8 @@ import ( type trips struct { *tview.Table - trips chan *model.Log - filterWord string + trips chan *model.Log + filterCol, filterTerm string } func newTrips(g *Gui) *trips { @@ -66,7 +66,7 @@ func (t *trips) entries(g *Gui) { var filteredTrips []*model.Log for _, trip := range trips { - if strings.Index(trip.Cave, t.filterWord) == -1 { + if t.search(trip) { continue } filteredTrips = append(filteredTrips, trip) @@ -128,8 +128,9 @@ func (t *trips) unfocus() { t.SetSelectable(false, false) } -func (t *trips) setFilterWord(word string) { - t.filterWord = word +func (t *trips) setFilter(col, term string) { + t.filterCol = col + t.filterTerm = term } func (t *trips) monitoringTrips(g *Gui) { @@ -146,3 +147,15 @@ LOOP: } } } + +func (t *trips) search(trip *model.Log) bool { + switch t.filterCol { + case "cave": + if strings.Index(trip.Cave, t.filterTerm) == -1 { + return true + } + return false + default: + return false + } +} From eeabbfd905769aa0d671aae92c31571aa08639ce Mon Sep 17 00:00:00 2001 From: IdlePhysicist Date: Tue, 18 Aug 2020 10:48:07 +0100 Subject: [PATCH 2/2] Case insensitive search implemented Default columns have also been implemented for each view pane too. --- internal/gui/cavers.go | 6 +++--- internal/gui/caves.go | 8 ++++---- internal/gui/keybindings.go | 6 +++--- internal/gui/trips.go | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/internal/gui/cavers.go b/internal/gui/cavers.go index cb3e67d..61b02f6 100644 --- a/internal/gui/cavers.go +++ b/internal/gui/cavers.go @@ -161,13 +161,13 @@ func (g *Gui) uniqueClubs(input []*model.Caver) []string { func (c *cavers) search(caver *model.Caver) bool { switch c.filterCol { - case "name": - if strings.Index(caver.Name, c.filterTerm) == -1 { + case "name", "": + if strings.Index(strings.ToLower(caver.Name), c.filterTerm) == -1 { return true } return false case "club": - if strings.Index(caver.Club, c.filterTerm) == -1 { + if strings.Index(strings.ToLower(caver.Club), c.filterTerm) == -1 { return true } return false diff --git a/internal/gui/caves.go b/internal/gui/caves.go index 592706a..dfcb398 100644 --- a/internal/gui/caves.go +++ b/internal/gui/caves.go @@ -195,18 +195,18 @@ func yesOrNo(val bool) string { func (c *caves) search(cave *model.Cave) bool { switch c.filterCol { - case "name": - if strings.Index(cave.Name, c.filterTerm) == -1 { + case "name", "": + if strings.Index(strings.ToLower(cave.Name), c.filterTerm) == -1 { return true } return false case "region": - if strings.Index(cave.Region, c.filterTerm) == -1 { + if strings.Index(strings.ToLower(cave.Region), c.filterTerm) == -1 { return true } return false case "country": - if strings.Index(cave.Country, c.filterTerm) == -1 { + if strings.Index(strings.ToLower(cave.Country), c.filterTerm) == -1 { return true } return false diff --git a/internal/gui/keybindings.go b/internal/gui/keybindings.go index 08bf2ee..abfd4ac 100644 --- a/internal/gui/keybindings.go +++ b/internal/gui/keybindings.go @@ -33,8 +33,8 @@ func (g *Gui) filter() { currentPanel.updateEntries(g) viewName := "filter" - searchInput := tview.NewInputField().SetLabel("Parameter") - searchInput.SetLabelWidth(10) + searchInput := tview.NewInputField().SetLabel("Column/Parameter") + searchInput.SetLabelWidth(17) searchInput.SetTitle(" Filter ") searchInput.SetTitleAlign(tview.AlignLeft) searchInput.SetBorder(true) @@ -58,7 +58,7 @@ func (g *Gui) filter() { searchInput.SetChangedFunc(func(text string) { if strings.Contains(text, "/") { - textSl := strings.Split(text, "/") + textSl := strings.Split(strings.ToLower(text), "/") if len(textSl) == 2 { currentPanel.setFilter(textSl[0], textSl[1]) diff --git a/internal/gui/trips.go b/internal/gui/trips.go index e573465..943c5a7 100644 --- a/internal/gui/trips.go +++ b/internal/gui/trips.go @@ -150,8 +150,8 @@ LOOP: func (t *trips) search(trip *model.Log) bool { switch t.filterCol { - case "cave": - if strings.Index(trip.Cave, t.filterTerm) == -1 { + case "cave", "": + if strings.Index(strings.ToLower(trip.Cave), t.filterTerm) == -1 { return true } return false