2022-07-13 15:55:36 +02:00
|
|
|
// Package filter provides a fuzzy searching text input to allow filtering a
|
|
|
|
// list of options to select one option.
|
|
|
|
//
|
|
|
|
// By default it will list all the files (recursively) in the current directory
|
|
|
|
// for the user to choose one, but the script (or user) can provide different
|
|
|
|
// new-line separated options to choose from.
|
|
|
|
//
|
|
|
|
// I.e. let's pick from a list of gum flavors:
|
|
|
|
//
|
2022-08-05 00:45:19 +02:00
|
|
|
// $ cat flavors.text | gum filter
|
2022-07-08 19:58:14 +02:00
|
|
|
package filter
|
2022-07-06 18:07:18 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/charmbracelet/bubbles/textinput"
|
2022-08-02 21:18:22 +02:00
|
|
|
"github.com/charmbracelet/bubbles/viewport"
|
2022-07-06 18:07:18 +02:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
"github.com/sahilm/fuzzy"
|
|
|
|
)
|
|
|
|
|
|
|
|
type model struct {
|
2022-09-03 03:21:31 +02:00
|
|
|
textinput textinput.Model
|
|
|
|
viewport *viewport.Model
|
|
|
|
choices []string
|
|
|
|
matches []fuzzy.Match
|
|
|
|
cursor int
|
2023-03-14 20:58:48 +01:00
|
|
|
header string
|
2022-09-03 03:21:31 +02:00
|
|
|
selected map[string]struct{}
|
|
|
|
limit int
|
|
|
|
numSelected int
|
|
|
|
indicator string
|
|
|
|
selectedPrefix string
|
|
|
|
unselectedPrefix string
|
|
|
|
height int
|
|
|
|
aborted bool
|
|
|
|
quitting bool
|
2023-03-14 20:58:48 +01:00
|
|
|
headerStyle lipgloss.Style
|
2022-09-03 03:21:31 +02:00
|
|
|
matchStyle lipgloss.Style
|
|
|
|
textStyle lipgloss.Style
|
|
|
|
indicatorStyle lipgloss.Style
|
|
|
|
selectedPrefixStyle lipgloss.Style
|
|
|
|
unselectedPrefixStyle lipgloss.Style
|
2022-10-07 21:23:28 +02:00
|
|
|
reverse bool
|
2022-10-13 16:47:27 +02:00
|
|
|
fuzzy bool
|
2022-07-06 18:07:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) Init() tea.Cmd { return nil }
|
|
|
|
func (m model) View() string {
|
2022-07-06 18:51:12 +02:00
|
|
|
if m.quitting {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-07-06 18:07:18 +02:00
|
|
|
var s strings.Builder
|
|
|
|
|
2022-10-07 21:23:28 +02:00
|
|
|
// For reverse layout, if the number of matches is less than the viewport
|
|
|
|
// height, we need to offset the matches so that the first match is at the
|
|
|
|
// bottom edge of the viewport instead of in the middle.
|
|
|
|
if m.reverse && len(m.matches) < m.viewport.Height {
|
|
|
|
s.WriteString(strings.Repeat("\n", m.viewport.Height-len(m.matches)))
|
|
|
|
}
|
|
|
|
|
2022-07-06 18:07:18 +02:00
|
|
|
// Since there are matches, display them so that the user can see, in real
|
|
|
|
// time, what they are searching for.
|
2022-10-07 21:23:28 +02:00
|
|
|
last := len(m.matches) - 1
|
|
|
|
for i := range m.matches {
|
|
|
|
// For reverse layout, the matches are displayed in reverse order.
|
|
|
|
if m.reverse {
|
|
|
|
i = last - i
|
|
|
|
}
|
|
|
|
match := m.matches[i]
|
|
|
|
|
2022-07-06 18:07:18 +02:00
|
|
|
// If this is the current selected index, we add a small indicator to
|
|
|
|
// represent it. Otherwise, simply pad the string.
|
2022-09-03 03:21:31 +02:00
|
|
|
if i == m.cursor {
|
|
|
|
s.WriteString(m.indicatorStyle.Render(m.indicator))
|
2022-07-06 18:07:18 +02:00
|
|
|
} else {
|
2023-05-12 04:21:09 +02:00
|
|
|
s.WriteString(strings.Repeat(" ", lipgloss.Width(m.indicator)))
|
2022-09-03 03:21:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there are multiple selections mark them, otherwise leave an empty space
|
|
|
|
if _, ok := m.selected[match.Str]; ok {
|
|
|
|
s.WriteString(m.selectedPrefixStyle.Render(m.selectedPrefix))
|
|
|
|
} else if m.limit > 1 {
|
|
|
|
s.WriteString(m.unselectedPrefixStyle.Render(m.unselectedPrefix))
|
|
|
|
} else {
|
|
|
|
s.WriteString(" ")
|
2022-07-06 18:07:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// For this match, there are a certain number of characters that have
|
|
|
|
// caused the match. i.e. fuzzy matching.
|
|
|
|
// We should indicate to the users which characters are being matched.
|
2022-10-07 21:23:28 +02:00
|
|
|
mi := 0
|
2022-10-08 04:36:37 +02:00
|
|
|
var buf strings.Builder
|
2022-07-06 18:07:18 +02:00
|
|
|
for ci, c := range match.Str {
|
|
|
|
// Check if the current character index matches the current matched
|
|
|
|
// index. If so, color the character to indicate a match.
|
|
|
|
if mi < len(match.MatchedIndexes) && ci == match.MatchedIndexes[mi] {
|
2022-10-08 04:36:37 +02:00
|
|
|
// Flush text buffer.
|
|
|
|
s.WriteString(m.textStyle.Render(buf.String()))
|
|
|
|
buf.Reset()
|
|
|
|
|
2022-07-12 22:05:40 +02:00
|
|
|
s.WriteString(m.matchStyle.Render(string(c)))
|
2022-07-06 18:07:18 +02:00
|
|
|
// We have matched this character, so we never have to check it
|
|
|
|
// again. Move on to the next match.
|
|
|
|
mi++
|
|
|
|
} else {
|
2022-10-08 04:36:37 +02:00
|
|
|
// Not a match, buffer a regular character.
|
|
|
|
buf.WriteRune(c)
|
2022-07-06 18:07:18 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-08 04:36:37 +02:00
|
|
|
// Flush text buffer.
|
|
|
|
s.WriteString(m.textStyle.Render(buf.String()))
|
2022-07-06 18:07:18 +02:00
|
|
|
|
|
|
|
// We have finished displaying the match with all of it's matched
|
|
|
|
// characters highlighted and the rest filled in.
|
|
|
|
// Move on to the next match.
|
|
|
|
s.WriteRune('\n')
|
|
|
|
}
|
|
|
|
|
2022-08-02 21:18:22 +02:00
|
|
|
m.viewport.SetContent(s.String())
|
|
|
|
|
2022-07-06 18:07:18 +02:00
|
|
|
// View the input and the filtered choices
|
2023-03-14 20:58:48 +01:00
|
|
|
header := m.headerStyle.Render(m.header)
|
2022-10-07 21:23:28 +02:00
|
|
|
if m.reverse {
|
2023-03-14 20:58:48 +01:00
|
|
|
view := m.viewport.View() + "\n" + m.textinput.View()
|
|
|
|
if m.header != "" {
|
|
|
|
return lipgloss.JoinVertical(lipgloss.Left, view, header)
|
|
|
|
}
|
|
|
|
|
|
|
|
return view
|
2022-10-07 21:23:28 +02:00
|
|
|
}
|
2023-03-14 20:58:48 +01:00
|
|
|
|
|
|
|
view := m.textinput.View() + "\n" + m.viewport.View()
|
2023-03-22 16:17:06 +01:00
|
|
|
if m.header != "" {
|
|
|
|
return lipgloss.JoinVertical(lipgloss.Left, header, view)
|
|
|
|
}
|
|
|
|
return view
|
2022-07-06 18:07:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
var cmd tea.Cmd
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case tea.WindowSizeMsg:
|
2022-08-02 21:18:22 +02:00
|
|
|
if m.height == 0 || m.height > msg.Height {
|
|
|
|
m.viewport.Height = msg.Height - lipgloss.Height(m.textinput.View())
|
|
|
|
}
|
2023-03-14 20:58:48 +01:00
|
|
|
|
|
|
|
// Make place in the view port if header is set
|
|
|
|
if m.header != "" {
|
|
|
|
m.viewport.Height = m.viewport.Height - lipgloss.Height(m.headerStyle.Render(m.header))
|
|
|
|
}
|
2022-09-08 18:36:37 +02:00
|
|
|
m.viewport.Width = msg.Width
|
2022-10-07 21:23:28 +02:00
|
|
|
if m.reverse {
|
|
|
|
m.viewport.YOffset = clamp(0, len(m.matches), len(m.matches)-m.viewport.Height)
|
|
|
|
}
|
2022-07-06 18:07:18 +02:00
|
|
|
case tea.KeyMsg:
|
|
|
|
switch msg.String() {
|
2022-07-31 03:10:36 +02:00
|
|
|
case "ctrl+c", "esc":
|
|
|
|
m.aborted = true
|
2022-08-02 21:18:22 +02:00
|
|
|
m.quitting = true
|
|
|
|
return m, tea.Quit
|
2022-07-31 03:10:36 +02:00
|
|
|
case "enter":
|
2022-07-06 18:51:12 +02:00
|
|
|
m.quitting = true
|
2022-07-06 18:07:18 +02:00
|
|
|
return m, tea.Quit
|
2022-07-28 22:15:32 +02:00
|
|
|
case "ctrl+n", "ctrl+j", "down":
|
2022-10-02 18:47:41 +02:00
|
|
|
m.CursorDown()
|
2022-07-28 22:15:32 +02:00
|
|
|
case "ctrl+p", "ctrl+k", "up":
|
2022-10-02 18:47:41 +02:00
|
|
|
m.CursorUp()
|
2022-09-03 03:21:31 +02:00
|
|
|
case "tab":
|
|
|
|
if m.limit == 1 {
|
|
|
|
break // no op
|
|
|
|
}
|
2022-10-02 18:47:41 +02:00
|
|
|
m.ToggleSelection()
|
|
|
|
m.CursorDown()
|
|
|
|
case "shift+tab":
|
|
|
|
if m.limit == 1 {
|
|
|
|
break // no op
|
2022-08-02 21:18:22 +02:00
|
|
|
}
|
2022-10-02 18:47:41 +02:00
|
|
|
m.ToggleSelection()
|
|
|
|
m.CursorUp()
|
2023-02-28 00:59:40 +01:00
|
|
|
case "ctrl+@":
|
|
|
|
if m.limit == 1 {
|
|
|
|
break // no op
|
|
|
|
}
|
|
|
|
m.ToggleSelection()
|
2022-07-06 18:07:18 +02:00
|
|
|
default:
|
|
|
|
m.textinput, cmd = m.textinput.Update(msg)
|
|
|
|
|
2022-10-07 21:23:28 +02:00
|
|
|
// yOffsetFromBottom is the number of lines from the bottom of the
|
|
|
|
// list to the top of the viewport. This is used to keep the viewport
|
|
|
|
// at a constant position when the number of matches are reduced
|
|
|
|
// in the reverse layout.
|
|
|
|
var yOffsetFromBottom int
|
|
|
|
if m.reverse {
|
|
|
|
yOffsetFromBottom = max(0, len(m.matches)-m.viewport.YOffset)
|
|
|
|
}
|
|
|
|
|
2022-10-13 16:47:27 +02:00
|
|
|
// A character was entered, this likely means that the text input has
|
|
|
|
// changed. This suggests that the matches are outdated, so update them.
|
|
|
|
if m.fuzzy {
|
2022-10-09 09:38:48 +02:00
|
|
|
m.matches = fuzzy.Find(m.textinput.Value(), m.choices)
|
2022-10-13 16:47:27 +02:00
|
|
|
} else {
|
|
|
|
m.matches = exactMatches(m.textinput.Value(), m.choices)
|
2022-10-09 00:25:53 +02:00
|
|
|
}
|
|
|
|
|
2022-07-06 18:07:18 +02:00
|
|
|
// If the search field is empty, let's not display the matches
|
|
|
|
// (none), but rather display all possible choices.
|
|
|
|
if m.textinput.Value() == "" {
|
|
|
|
m.matches = matchAll(m.choices)
|
|
|
|
}
|
2022-10-07 21:23:28 +02:00
|
|
|
|
|
|
|
// For reverse layout, we need to offset the viewport so that the
|
|
|
|
// it remains at a constant position relative to the cursor.
|
|
|
|
if m.reverse {
|
|
|
|
maxYOffset := max(0, len(m.matches)-m.viewport.Height)
|
|
|
|
m.viewport.YOffset = clamp(0, maxYOffset, len(m.matches)-yOffsetFromBottom)
|
|
|
|
}
|
2022-07-06 18:07:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's possible that filtering items have caused fewer matches. So, ensure
|
|
|
|
// that the selected index is within the bounds of the number of matches.
|
2022-09-03 03:21:31 +02:00
|
|
|
m.cursor = clamp(0, len(m.matches)-1, m.cursor)
|
2022-07-06 18:07:18 +02:00
|
|
|
return m, cmd
|
|
|
|
}
|
|
|
|
|
2022-10-02 18:47:41 +02:00
|
|
|
func (m *model) CursorUp() {
|
2022-10-07 21:23:28 +02:00
|
|
|
if m.reverse {
|
|
|
|
m.cursor = clamp(0, len(m.matches)-1, m.cursor+1)
|
|
|
|
if len(m.matches)-m.cursor <= m.viewport.YOffset {
|
|
|
|
m.viewport.SetYOffset(len(m.matches) - m.cursor - 1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m.cursor = clamp(0, len(m.matches)-1, m.cursor-1)
|
|
|
|
if m.cursor < m.viewport.YOffset {
|
|
|
|
m.viewport.SetYOffset(m.cursor)
|
|
|
|
}
|
2022-10-02 18:47:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *model) CursorDown() {
|
2022-10-07 21:23:28 +02:00
|
|
|
if m.reverse {
|
|
|
|
m.cursor = clamp(0, len(m.matches)-1, m.cursor-1)
|
|
|
|
if len(m.matches)-m.cursor > m.viewport.Height+m.viewport.YOffset {
|
|
|
|
m.viewport.LineDown(1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m.cursor = clamp(0, len(m.matches)-1, m.cursor+1)
|
|
|
|
if m.cursor >= m.viewport.YOffset+m.viewport.Height {
|
|
|
|
m.viewport.LineDown(1)
|
|
|
|
}
|
2022-10-02 18:47:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *model) ToggleSelection() {
|
|
|
|
if _, ok := m.selected[m.matches[m.cursor].Str]; ok {
|
|
|
|
delete(m.selected, m.matches[m.cursor].Str)
|
|
|
|
m.numSelected--
|
|
|
|
} else if m.numSelected < m.limit {
|
|
|
|
m.selected[m.matches[m.cursor].Str] = struct{}{}
|
|
|
|
m.numSelected++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 18:07:18 +02:00
|
|
|
func matchAll(options []string) []fuzzy.Match {
|
2022-10-07 21:23:28 +02:00
|
|
|
matches := make([]fuzzy.Match, len(options))
|
2022-08-05 18:22:09 +02:00
|
|
|
for i, option := range options {
|
|
|
|
matches[i] = fuzzy.Match{Str: option}
|
2022-07-06 18:07:18 +02:00
|
|
|
}
|
|
|
|
return matches
|
|
|
|
}
|
|
|
|
|
2022-10-09 09:38:48 +02:00
|
|
|
func exactMatches(search string, choices []string) []fuzzy.Match {
|
2022-10-13 16:47:27 +02:00
|
|
|
matches := fuzzy.Matches{}
|
2022-10-09 09:38:48 +02:00
|
|
|
for i, choice := range choices {
|
2022-10-09 00:25:53 +02:00
|
|
|
search = strings.ToLower(search)
|
2022-10-09 09:38:48 +02:00
|
|
|
matchedString := strings.ToLower(choice)
|
2022-10-09 00:25:53 +02:00
|
|
|
|
|
|
|
index := strings.Index(matchedString, search)
|
|
|
|
if index >= 0 {
|
2022-10-09 09:38:48 +02:00
|
|
|
matchedIndexes := []int{}
|
|
|
|
for s := range search {
|
|
|
|
matchedIndexes = append(matchedIndexes, index+s)
|
2022-10-09 00:25:53 +02:00
|
|
|
}
|
2022-10-13 16:47:27 +02:00
|
|
|
matches = append(matches, fuzzy.Match{
|
2022-10-09 09:38:48 +02:00
|
|
|
Str: choice,
|
|
|
|
Index: i,
|
|
|
|
MatchedIndexes: matchedIndexes,
|
|
|
|
})
|
2022-10-09 00:25:53 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-09 00:33:15 +02:00
|
|
|
|
2022-10-13 16:47:27 +02:00
|
|
|
return matches
|
2022-10-09 00:25:53 +02:00
|
|
|
}
|
|
|
|
|
2022-09-03 03:39:01 +02:00
|
|
|
//nolint:unparam
|
2022-07-06 18:07:18 +02:00
|
|
|
func clamp(min, max, val int) int {
|
|
|
|
if val < min {
|
|
|
|
return min
|
|
|
|
}
|
|
|
|
if val > max {
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
2022-10-07 21:23:28 +02:00
|
|
|
|
|
|
|
func max(a, b int) int {
|
|
|
|
if a > b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|