aboutsummaryrefslogtreecommitdiffstats
path: root/app/listbox.go
diff options
context:
space:
mode:
authorKoni Marti <koni.marti@gmail.com>2024-01-28 02:08:25 +0100
committerRobin Jarry <robin@jarry.cc>2024-01-29 22:46:08 +0100
commit37b05d7e73b11610f2979abdbe821366e55ee651 (patch)
treed4a417bdc2070deaa095d12f1399656f3ca24495 /app/listbox.go
parentea2000633ab6254cf8015d441bd3e79a3009ced2 (diff)
downloadaerc-37b05d7e73b11610f2979abdbe821366e55ee651.tar.gz
listbox: add external text filter function
Set an external filter function to use in the filtering operation of the listbox widget. This allows us to use commands.FilterList without an import cycle conflict. commands.FilterList comes with fuzzy completion, too. Signed-off-by: Koni Marti <koni.marti@gmail.com> Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'app/listbox.go')
-rw-r--r--app/listbox.go37
1 files changed, 26 insertions, 11 deletions
diff --git a/app/listbox.go b/app/listbox.go
index 02118d2a..54ced1e8 100644
--- a/app/listbox.go
+++ b/app/listbox.go
@@ -1,6 +1,7 @@
package app
import (
+ "fmt"
"math"
"strings"
"sync"
@@ -25,18 +26,20 @@ type ListBox struct {
filterMutex sync.Mutex
filter *ui.TextInput
uiConfig *config.UIConfig
+ textFilter func([]string, string) []string
cb func(string)
}
func NewListBox(title string, lines []string, uiConfig *config.UIConfig, cb func(string)) *ListBox {
lb := &ListBox{
- title: title,
- lines: lines,
- cursorPos: -1,
- jump: -1,
- uiConfig: uiConfig,
- cb: cb,
- filter: ui.NewTextInput("", uiConfig),
+ title: title,
+ lines: lines,
+ cursorPos: -1,
+ jump: -1,
+ uiConfig: uiConfig,
+ textFilter: nil,
+ cb: cb,
+ filter: ui.NewTextInput("", uiConfig),
}
lb.filter.OnChange(func(ti *ui.TextInput) {
var show bool
@@ -53,6 +56,11 @@ func NewListBox(title string, lines []string, uiConfig *config.UIConfig, cb func
return lb
}
+func (lb *ListBox) SetTextFilter(fn func([]string, string) []string) *ListBox {
+ lb.textFilter = fn
+ return lb
+}
+
func (lb *ListBox) dedup() {
dedupped := make([]string, 0, len(lb.lines))
dedup := make(map[string]struct{})
@@ -90,7 +98,9 @@ func (lb *ListBox) Draw(ctx *ui.Context) {
y := 0
if lb.showFilterField() {
y = 1
- x := ctx.Printf(0, y, defaultStyle, "Filter: ")
+ x := ctx.Printf(0, y, defaultStyle,
+ fmt.Sprintf("Filter (%d/%d): ",
+ len(lb.filtered()), len(lb.lines)))
lb.filter.Draw(ctx.Subcontext(x, y, w-x, 1))
}
@@ -125,10 +135,15 @@ func (lb *ListBox) moveHorizontal(delta int) {
}
func (lb *ListBox) filtered() []string {
- list := []string{}
- filterTerm := lb.filter.String()
+ term := lb.filter.String()
+
+ if lb.textFilter != nil {
+ return lb.textFilter(lb.lines, term)
+ }
+
+ list := make([]string, 0, len(lb.lines))
for _, line := range lb.lines {
- if strings.Contains(line, filterTerm) {
+ if strings.Contains(line, term) {
list = append(list, line)
}
}