2022-07-13 16:55:36 +03: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:
|
|
|
|
//
|
|
|
|
// $ cat flavors.text | gum filter
|
|
|
|
//
|
2022-07-08 20:58:14 +03:00
|
|
|
package filter
|
2022-07-06 19:07:18 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/charmbracelet/bubbles/textinput"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
"github.com/mattn/go-runewidth"
|
|
|
|
"github.com/sahilm/fuzzy"
|
|
|
|
)
|
|
|
|
|
|
|
|
type model struct {
|
2022-07-08 00:46:22 +03:00
|
|
|
textinput textinput.Model
|
|
|
|
choices []string
|
|
|
|
matches []fuzzy.Match
|
|
|
|
selected int
|
|
|
|
indicator string
|
|
|
|
height int
|
|
|
|
quitting bool
|
2022-07-12 23:05:40 +03:00
|
|
|
matchStyle lipgloss.Style
|
|
|
|
textStyle lipgloss.Style
|
2022-07-08 00:46:22 +03:00
|
|
|
indicatorStyle lipgloss.Style
|
2022-07-06 19:07:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) Init() tea.Cmd { return nil }
|
|
|
|
func (m model) View() string {
|
2022-07-06 19:51:12 +03:00
|
|
|
if m.quitting {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-07-06 19:07:18 +03:00
|
|
|
var s strings.Builder
|
|
|
|
|
|
|
|
// Since there are matches, display them so that the user can see, in real
|
|
|
|
// time, what they are searching for.
|
|
|
|
for i, match := range m.matches {
|
|
|
|
// If this is the current selected index, we add a small indicator to
|
|
|
|
// represent it. Otherwise, simply pad the string.
|
|
|
|
if i == m.selected {
|
2022-07-08 00:46:22 +03:00
|
|
|
s.WriteString(m.indicatorStyle.Render(m.indicator) + " ")
|
2022-07-06 19:07:18 +03:00
|
|
|
} else {
|
|
|
|
s.WriteString(strings.Repeat(" ", runewidth.StringWidth(m.indicator)) + " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
var mi = 0
|
|
|
|
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-07-12 23:05:40 +03:00
|
|
|
s.WriteString(m.matchStyle.Render(string(c)))
|
2022-07-06 19:07:18 +03:00
|
|
|
// We have matched this character, so we never have to check it
|
|
|
|
// again. Move on to the next match.
|
|
|
|
mi++
|
|
|
|
} else {
|
|
|
|
// Not a match, simply show the character, unstyled.
|
2022-07-12 23:05:40 +03:00
|
|
|
s.WriteString(m.textStyle.Render(string(c)))
|
2022-07-06 19:07:18 +03: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')
|
|
|
|
}
|
|
|
|
|
|
|
|
tv := m.textinput.View()
|
|
|
|
results := lipgloss.NewStyle().MaxHeight(m.height - lipgloss.Height(tv)).Render(s.String())
|
|
|
|
// View the input and the filtered choices
|
|
|
|
return tv + "\n" + results
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
var cmd tea.Cmd
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case tea.WindowSizeMsg:
|
|
|
|
m.height = msg.Height
|
|
|
|
case tea.KeyMsg:
|
|
|
|
switch msg.String() {
|
|
|
|
case "ctrl+c", "esc", "enter":
|
2022-07-06 19:51:12 +03:00
|
|
|
m.quitting = true
|
2022-07-06 19:07:18 +03:00
|
|
|
return m, tea.Quit
|
2022-07-28 23:15:32 +03:00
|
|
|
case "ctrl+n", "ctrl+j", "down":
|
2022-07-06 19:07:18 +03:00
|
|
|
m.selected = clamp(0, len(m.matches)-1, m.selected+1)
|
2022-07-28 23:15:32 +03:00
|
|
|
case "ctrl+p", "ctrl+k", "up":
|
2022-07-06 19:07:18 +03:00
|
|
|
m.selected = clamp(0, len(m.matches)-1, m.selected-1)
|
|
|
|
default:
|
|
|
|
m.textinput, cmd = m.textinput.Update(msg)
|
|
|
|
|
|
|
|
// A character was entered, this likely means that the text input
|
|
|
|
// has changed. This suggests that the matches are outdated, so
|
|
|
|
// update them, with a fuzzy finding algorithm provided by
|
|
|
|
// https://github.com/sahilm/fuzzy
|
|
|
|
m.matches = fuzzy.Find(m.textinput.Value(), m.choices)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
m.selected = clamp(0, len(m.matches)-1, m.selected)
|
|
|
|
return m, cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func matchAll(options []string) []fuzzy.Match {
|
|
|
|
var matches []fuzzy.Match
|
|
|
|
for _, option := range options {
|
|
|
|
matches = append(matches, fuzzy.Match{Str: option})
|
|
|
|
}
|
|
|
|
return matches
|
|
|
|
}
|
|
|
|
|
|
|
|
func clamp(min, max, val int) int {
|
|
|
|
if val < min {
|
|
|
|
return min
|
|
|
|
}
|
|
|
|
if val > max {
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|