Thu 1 Aug 14:15:35 CEST 2024

This commit is contained in:
Thomas Avé 2024-08-01 14:15:35 +02:00
parent 97bce27750
commit 3cf5fca630
2 changed files with 30 additions and 13 deletions

26
data.go
View File

@ -4,21 +4,41 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
"sort"
"github.com/charmbracelet/bubbles/list" "github.com/charmbracelet/bubbles/list"
) )
type Ranking struct { type Ranking struct {
items []list.Item items []string
indexes []int
filter string filter string
} }
func (a Ranking) Len() int { return len(a.items) } func (a Ranking) Len() int { return len(a.items) }
func (a Ranking) Swap(i, j int) { a.items[i], a.items[j] = a.items[j], a.items[i] } func (a Ranking) Swap(i, j int) {
a.indexes[i], a.indexes[j] = a.indexes[j], a.indexes[i]
}
func (a Ranking) Less(i, j int) bool { func (a Ranking) Less(i, j int) bool {
return len(a.items[i].FilterValue()) < len(a.items[j].FilterValue()) return len(a.items[a.indexes[i]]) < len(a.items[a.indexes[j]])
} }
func Filter(term string, targets []string) []list.Rank {
indexes := make([]int, len(targets))
for i := range targets {
indexes[i] = i
}
ranking := Ranking{targets, indexes, term}
sort.Stable(ranking)
result := make([]list.Rank, len(targets))
for i := range targets {
result[i] = list.Rank{
Index: indexes[i],
MatchedIndexes: []int{i},
}
}
return result
}
func getPaths() []string { func getPaths() []string {
paths := []string{} paths := []string{}

17
tui.go
View File

@ -77,19 +77,16 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if ignoreInput { if ignoreInput {
return m, cmd return m, cmd
} }
cmds := []tea.Cmd{ cmds := []tea.Cmd{}
textinput.Blink,
}
// m.textInput, cmd = m.textInput.Update(msg) m.textInput, cmd = m.textInput.Update(msg)
// cmds = append(cmds, cmd) cmds = append(cmds, cmd)
if !m.list.SettingFilter() { if !m.list.SettingFilter() {
m.list, cmd = m.list.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("/")}) m.list, cmd = m.list.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("/")})
cmds = append(cmds, cmd) cmds = append(cmds, cmd)
} }
m.list, cmd = m.list.Update(msg) m.list, cmd = m.list.Update(msg)
cmds = append(cmds, cmd) cmds = append(cmds, cmd)
@ -103,8 +100,7 @@ func (m model) View() string {
if m.quitting { if m.quitting {
return "" return ""
} }
return fmt.Sprintf("%s\n%s\n", return fmt.Sprintf("%s\n",
m.textInput.View(),
m.list.View(), m.list.View(),
) )
} }
@ -132,14 +128,15 @@ func initialModel(items []list.Item) model {
l.SetShowHelp(false) l.SetShowHelp(false)
l.SetShowTitle(false) l.SetShowTitle(false)
l.SetShowPagination(false) l.SetShowPagination(false)
l.Styles.TitleBar = lipgloss.NewStyle()
ti := textinput.New() ti := textinput.New()
ti.Focus() ti.Focus()
ti.CharLimit = 156 ti.CharLimit = 4096
ti.Width = 20 ti.Width = 20
l.FilterInput = ti l.FilterInput = ti
l.Filter = Filter
return model{ return model{
textInput: ti,
list: l, list: l,
err: nil, err: nil,
} }