Skip to content

Commit

Permalink
Feature filter rows by inequality. (#36)
Browse files Browse the repository at this point in the history
Allows rudimentary filtering of rows using inequalities, in Caves &
Cavers, on columns Views & Count respectively.

Added comments to `search` methods that attempt point out the goofy
nature of that function, i.e. that it needs to return `false`
generally, and that we only return `true` if there is something we
want to _not_ display there by only displaying what matches our
search criteria. Not sure exactly why I wrote it that way.

Updated .gitignore to exclude tags file.

NB: Change in functionality — Applying a filter with no column specified
now assumes you are looking at `visits` or `count`.
  • Loading branch information
IdlePhysicist authored Feb 8, 2022
1 parent 4df9d39 commit 788e98c
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ _old
# Mac
*.DS_Store

tags

# -- Except ----
!.keep
Expand Down
20 changes: 16 additions & 4 deletions internal/gui/cavers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

type cavers struct {
*tview.Table
cavers chan *model.Caver
filterCol, filterTerm string
cavers chan *model.Caver
filterCol, filterTerm, filterAction string
}

func newCavers(g *Gui) *cavers {
Expand Down Expand Up @@ -138,9 +138,10 @@ func (c *cavers) unfocus() {
c.SetSelectable(false, false)
}

func (c *cavers) setFilter(col, term string) {
func (c *cavers) setFilter(col, term, action string) {
c.filterCol = col
c.filterTerm = term
c.filterAction = action
}

func (c *cavers) monitoringCavers(g *Gui) {
Expand Down Expand Up @@ -173,8 +174,10 @@ func (g *Gui) uniqueClubs(input []*model.Caver) []string {
}

func (c *cavers) search(caver *model.Caver) bool {
// Below *looks* goofy but it all makes sense considering this funciton
// needs to return false normally!!
switch c.filterCol {
case "name", "":
case "name":
if strings.Index(strings.ToLower(caver.Name), c.filterTerm) == -1 {
return true
}
Expand All @@ -184,6 +187,15 @@ func (c *cavers) search(caver *model.Caver) bool {
return true
}
return false
case "count", "":
switch c.filterAction {
case "<":
return caver.Count > atoi(c.filterTerm)
case ">":
return caver.Count < atoi(c.filterTerm)
default:
return false
}
default:
return false
}
Expand Down
20 changes: 16 additions & 4 deletions internal/gui/caves.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

type caves struct {
*tview.Table
caves chan *model.Cave
filterCol, filterTerm string
caves chan *model.Cave
filterCol, filterTerm, filterAction string
}

func newCaves(g *Gui) *caves {
Expand Down Expand Up @@ -150,9 +150,10 @@ func (c *caves) unfocus() {
c.SetSelectable(false, false)
}

func (c *caves) setFilter(col, term string) {
func (c *caves) setFilter(col, term, action string) {
c.filterCol = col
c.filterTerm = term
c.filterAction = action
}

func (c *caves) monitoringCaves(g *Gui) {
Expand Down Expand Up @@ -207,8 +208,10 @@ func yesOrNo(val bool) string {
}

func (c *caves) search(cave *model.Cave) bool {
// Below *looks* goofy but it all makes sense considering this funciton
// needs to return false normally!!
switch c.filterCol {
case "name", "":
case "name":
if strings.Index(strings.ToLower(cave.Name), c.filterTerm) == -1 {
return true
}
Expand All @@ -223,6 +226,15 @@ func (c *caves) search(cave *model.Cave) bool {
return true
}
return false
case "visits", "":
switch c.filterAction {
case "<":
return cave.Visits > atoi(c.filterTerm)
case ">":
return cave.Visits < atoi(c.filterTerm)
default:
return false
}
default:
return false
}
Expand Down
27 changes: 25 additions & 2 deletions internal/gui/keybindings.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gui

import (
"strconv"
"strings"

"github.com/gdamore/tcell/v2"
Expand Down Expand Up @@ -29,7 +30,7 @@ func (g *Gui) setGlobalKeybinding(event *tcell.EventKey) {

func (g *Gui) filter() {
currentPanel := g.state.panels.panel[g.state.panels.currentPanel]
currentPanel.setFilter("", "")
currentPanel.setFilter("", "", "")
currentPanel.updateEntries(g)

viewName := "filter"
Expand Down Expand Up @@ -61,7 +62,21 @@ func (g *Gui) filter() {
textSl := strings.Split(strings.ToLower(text), "/")

if len(textSl) == 2 {
currentPanel.setFilter(textSl[0], textSl[1])
currentPanel.setFilter(textSl[0], textSl[1], "")
currentPanel.updateEntries(g)
}
} else if strings.Contains(text, "<") {
textSl := strings.Split(strings.ToLower(text), "<")

if len(textSl) == 2 {
currentPanel.setFilter(textSl[0], textSl[1], "<")
currentPanel.updateEntries(g)
}
} else if strings.Contains(text, ">") {
textSl := strings.Split(strings.ToLower(text), ">")

if len(textSl) == 2 {
currentPanel.setFilter(textSl[0], textSl[1], ">")
currentPanel.updateEntries(g)
}
}
Expand Down Expand Up @@ -95,3 +110,11 @@ func (g *Gui) nextPage() {
g.goTo(g.selectPage(slide - 1, 0)) // NOTE: If the Highlight func is fixed for the tab bar then this line will not be required
}
*/

func atoi(s string) int64 {
v, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return 0
}
return v
}
2 changes: 1 addition & 1 deletion internal/gui/panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ type panel interface {
setKeybinding(*Gui)
focus(*Gui)
unfocus()
setFilter(string, string)
setFilter(string, string, string)
}
9 changes: 6 additions & 3 deletions internal/gui/trips.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

type trips struct {
*tview.Table
trips chan *model.Log
filterCol, filterTerm string
trips chan *model.Log
filterCol, filterTerm, filterAction string
}

func newTrips(g *Gui) *trips {
Expand Down Expand Up @@ -132,9 +132,10 @@ func (t *trips) unfocus() {
t.SetSelectable(false, false)
}

func (t *trips) setFilter(col, term string) {
func (t *trips) setFilter(col, term, action string) {
t.filterCol = col
t.filterTerm = term
t.filterAction = action // Unused in this window.
}

func (t *trips) monitoringTrips(g *Gui) {
Expand All @@ -153,6 +154,8 @@ LOOP:
}

func (t *trips) search(trip *model.Log) bool {
// Below *looks* goofy but it all makes sense considering this funciton
// needs to return false normally!!
switch t.filterCol {
case "cave", "":
if strings.Index(strings.ToLower(trip.Cave), t.filterTerm) == -1 {
Expand Down

0 comments on commit 788e98c

Please sign in to comment.