Skip to content

Commit

Permalink
Merge branch 'adv-search' into develop
Browse files Browse the repository at this point in the history
Closes #30
Closes #32
  • Loading branch information
IdlePhysicist committed Aug 18, 2020
2 parents da11a88 + eeabbfd commit 4720162
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 19 deletions.
26 changes: 22 additions & 4 deletions internal/gui/cavers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type cavers struct {
*tview.Table
cavers chan *model.Caver
filterWord string
filterCol, filterTerm string
}

func newCavers(g *Gui) *cavers {
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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(strings.ToLower(caver.Name), c.filterTerm) == -1 {
return true
}
return false
case "club":
if strings.Index(strings.ToLower(caver.Club), c.filterTerm) == -1 {
return true
}
return false
default:
return false
}
}
31 changes: 27 additions & 4 deletions internal/gui/caves.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type caves struct {
*tview.Table
caves chan *model.Cave
filterWord string
filterCol, filterTerm string
}

func newCaves(g *Gui) *caves {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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(strings.ToLower(cave.Name), c.filterTerm) == -1 {
return true
}
return false
case "region":
if strings.Index(strings.ToLower(cave.Region), c.filterTerm) == -1 {
return true
}
return false
case "country":
if strings.Index(strings.ToLower(cave.Country), c.filterTerm) == -1 {
return true
}
return false
default:
return false
}
}
18 changes: 13 additions & 5 deletions internal/gui/keybindings.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gui

import (
"strings"

"github.com/gdamore/tcell"
"github.com/rivo/tview"
)
Expand All @@ -27,12 +29,12 @@ 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"
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)
Expand All @@ -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(strings.ToLower(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")
Expand Down
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()
setFilterWord(string)
setFilter(string, string)
}
23 changes: 18 additions & 5 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
filterWord string
trips chan *model.Log
filterCol, filterTerm string
}

func newTrips(g *Gui) *trips {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -146,3 +147,15 @@ LOOP:
}
}
}

func (t *trips) search(trip *model.Log) bool {
switch t.filterCol {
case "cave", "":
if strings.Index(strings.ToLower(trip.Cave), t.filterTerm) == -1 {
return true
}
return false
default:
return false
}
}

0 comments on commit 4720162

Please sign in to comment.