-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiltration.go
130 lines (110 loc) · 2.51 KB
/
filtration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"os"
"sort"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/sahilm/fuzzy"
)
type FilterState int
const (
Unfiltered FilterState = iota
Filtering
FilterApplied
)
type filteredFile struct {
file os.DirEntry
matches []int
}
type filteredFiles []filteredFile
type FilterMatchesMsg filteredFiles
type FilterFunc func(string, []string) []Rank
type Rank struct {
Index int
MatchedIndexes []int
}
func (ff filteredFiles) filteredFilesAsDirEntries() []os.DirEntry {
de := make([]os.DirEntry, len(ff))
for i, f := range ff {
de[i] = f.file
}
return de
}
func DefaultFilter(term string, targets []string) []Rank {
ranks := fuzzy.Find(term, targets)
sort.Stable(ranks)
result := make([]Rank, len(ranks))
for i, r := range ranks {
result[i] = Rank{
Index: r.Index,
MatchedIndexes: r.MatchedIndexes,
}
}
return result
}
func filterFiles(m Model) tea.Cmd {
return func() tea.Msg {
if m.filterInput.Value() == "" {
return FilterMatchesMsg(m.filesAsFilterFiles())
}
fs := m.files
targets := make([]string, len(fs))
for i, f := range fs {
targets[i] = f.Name()
}
filterMatches := filteredFiles{}
for _, r := range m.filter(m.filterInput.Value(), targets) {
filterMatches = append(filterMatches, filteredFile{
file: fs[r.Index],
matches: r.MatchedIndexes,
})
}
return FilterMatchesMsg(filterMatches)
}
}
func (m Model) filesAsFilterFiles() filteredFiles {
ff := make(filteredFiles, len(m.files))
for i, f := range m.files {
ff[i] = filteredFile{
file: f,
}
}
return ff
}
func (m *Model) filterAccept() {
m.filterState = FilterApplied
m.idx = 0
}
func (m *Model) filterOn() {
m.filterState = Filtering
if m.filterInput.Value() == "" {
m.filteredFiles = m.filesAsFilterFiles()
}
m.filterInput.Focus()
}
func (m *Model) filterOff() {
m.filterState = Unfiltered
m.filterInput.Reset()
m.filteredFiles = nil
m.news = ""
}
func (m Model) filterMode(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keys.FilterOff):
m.filterOff()
case key.Matches(msg, m.keys.FilterAccept):
m.filterAccept()
}
}
newFilterInputModel, inputCmd := m.filterInput.Update(msg)
filterChanged := m.filterInput.Value() != newFilterInputModel.Value()
m.filterInput = newFilterInputModel
cmds = append(cmds, inputCmd)
if filterChanged {
cmds = append(cmds, filterFiles(m))
}
return m, tea.Batch(cmds...)
}