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:
|
|
|
|
//
|
2022-08-05 01:45:19 +03:00
|
|
|
// $ 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"
|
2023-06-30 00:29:46 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/charmbracelet/gum/timeout"
|
2022-07-06 19:07:18 +03:00
|
|
|
|
|
|
|
"github.com/charmbracelet/bubbles/textinput"
|
2022-08-02 22:18:22 +03:00
|
|
|
"github.com/charmbracelet/bubbles/viewport"
|
2022-07-06 19:07:18 +03:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
"github.com/sahilm/fuzzy"
|
|
|
|
)
|
|
|
|
|
|
|
|
type model struct {
|
2022-09-03 04:21:31 +03:00
|
|
|
textinput textinput.Model
|
|
|
|
viewport *viewport.Model
|
|
|
|
choices []string
|
|
|
|
matches []fuzzy.Match
|
|
|
|
cursor int
|
2023-03-14 22:58:48 +03:00
|
|
|
header string
|
2022-09-03 04:21:31 +03:00
|
|
|
selected map[string]struct{}
|
|
|
|
limit int
|
|
|
|
numSelected int
|
|
|
|
indicator string
|
|
|
|
selectedPrefix string
|
|
|
|
unselectedPrefix string
|
|
|
|
height int
|
|
|
|
aborted bool
|
|
|
|
quitting bool
|
2023-03-14 22:58:48 +03:00
|
|
|
headerStyle lipgloss.Style
|
2022-09-03 04:21:31 +03:00
|
|
|
matchStyle lipgloss.Style
|
|
|
|
textStyle lipgloss.Style
|
2023-05-24 15:11:20 +03:00
|
|
|
cursorTextStyle lipgloss.Style
|
2022-09-03 04:21:31 +03:00
|
|
|
indicatorStyle lipgloss.Style
|
|
|
|
selectedPrefixStyle lipgloss.Style
|
|
|
|
unselectedPrefixStyle lipgloss.Style
|
2022-10-07 22:23:28 +03:00
|
|
|
reverse bool
|
2022-10-13 17:47:27 +03:00
|
|
|
fuzzy bool
|
2023-05-30 17:19:53 +03:00
|
|
|
sort bool
|
2023-06-30 00:29:46 +03:00
|
|
|
timeout time.Duration
|
|
|
|
hasTimeout bool
|
2022-07-06 19:07:18 +03:00
|
|
|
}
|
|
|
|
|
2023-06-30 00:29:46 +03:00
|
|
|
func (m model) Init() tea.Cmd {
|
|
|
|
return timeout.Init(m.timeout, nil)
|
|
|
|
}
|
2022-07-06 19:07:18 +03:00
|
|
|
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
|
2023-05-24 15:11:20 +03:00
|
|
|
var lineTextStyle lipgloss.Style
|
2022-07-06 19:07:18 +03:00
|
|
|
|
2022-10-07 22:23:28 +03: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 19:07:18 +03:00
|
|
|
// Since there are matches, display them so that the user can see, in real
|
|
|
|
// time, what they are searching for.
|
2022-10-07 22:23:28 +03: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 19:07:18 +03:00
|
|
|
// If this is the current selected index, we add a small indicator to
|
|
|
|
// represent it. Otherwise, simply pad the string.
|
2023-05-24 15:11:20 +03:00
|
|
|
// The line's text style is set depending on whether or not the cursor
|
|
|
|
// points to this line.
|
2022-09-03 04:21:31 +03:00
|
|
|
if i == m.cursor {
|
|
|
|
s.WriteString(m.indicatorStyle.Render(m.indicator))
|
2023-05-24 15:11:20 +03:00
|
|
|
lineTextStyle = m.cursorTextStyle
|
2022-07-06 19:07:18 +03:00
|
|
|
} else {
|
2023-05-12 05:21:09 +03:00
|
|
|
s.WriteString(strings.Repeat(" ", lipgloss.Width(m.indicator)))
|
2023-05-24 15:11:20 +03:00
|
|
|
lineTextStyle = m.textStyle
|
2022-09-03 04:21:31 +03: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 19:07:18 +03: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 22:23:28 +03:00
|
|
|
mi := 0
|
2022-10-08 05:36:37 +03:00
|
|
|
var buf strings.Builder
|
2022-07-06 19:07:18 +03: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 05:36:37 +03:00
|
|
|
// Flush text buffer.
|
2023-05-24 15:11:20 +03:00
|
|
|
s.WriteString(lineTextStyle.Render(buf.String()))
|
2022-10-08 05:36:37 +03:00
|
|
|
buf.Reset()
|
|
|
|
|
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 {
|
2022-10-08 05:36:37 +03:00
|
|
|
// Not a match, buffer a regular character.
|
|
|
|
buf.WriteRune(c)
|
2022-07-06 19:07:18 +03:00
|
|
|
}
|
|
|
|
}
|
2022-10-08 05:36:37 +03:00
|
|
|
// Flush text buffer.
|
2023-05-24 15:11:20 +03:00
|
|
|
s.WriteString(lineTextStyle.Render(buf.String()))
|
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')
|
|
|
|
}
|
|
|
|
|
2022-08-02 22:18:22 +03:00
|
|
|
m.viewport.SetContent(s.String())
|
|
|
|
|
2022-07-06 19:07:18 +03:00
|
|
|
// View the input and the filtered choices
|
2023-03-14 22:58:48 +03:00
|
|
|
header := m.headerStyle.Render(m.header)
|
2022-10-07 22:23:28 +03:00
|
|
|
if m.reverse {
|
2023-03-14 22:58:48 +03:00
|
|
|
view := m.viewport.View() + "\n" + m.textinput.View()
|
|
|
|
if m.header != "" {
|
|
|
|
return lipgloss.JoinVertical(lipgloss.Left, view, header)
|
|
|
|
}
|
|
|
|
|
|
|
|
return view
|
2022-10-07 22:23:28 +03:00
|
|
|
}
|
2023-03-14 22:58:48 +03:00
|
|
|
|
|
|
|
view := m.textinput.View() + "\n" + m.viewport.View()
|
2023-03-22 18:17:06 +03:00
|
|
|
if m.header != "" {
|
|
|
|
return lipgloss.JoinVertical(lipgloss.Left, header, view)
|
|
|
|
}
|
|
|
|
return view
|
2022-07-06 19:07:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
var cmd tea.Cmd
|
|
|
|
switch msg := msg.(type) {
|
2023-06-30 00:29:46 +03:00
|
|
|
case timeout.TickTimeoutMsg:
|
|
|
|
if msg.TimeoutValue <= 0 {
|
|
|
|
m.quitting = true
|
|
|
|
m.aborted = true
|
|
|
|
return m, tea.Quit
|
|
|
|
}
|
|
|
|
m.timeout = msg.TimeoutValue
|
|
|
|
return m, timeout.Tick(msg.TimeoutValue, msg.Data)
|
|
|
|
|
2022-07-06 19:07:18 +03:00
|
|
|
case tea.WindowSizeMsg:
|
2022-08-02 22:18:22 +03:00
|
|
|
if m.height == 0 || m.height > msg.Height {
|
|
|
|
m.viewport.Height = msg.Height - lipgloss.Height(m.textinput.View())
|
|
|
|
}
|
2023-03-14 22:58:48 +03: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 19:36:37 +03:00
|
|
|
m.viewport.Width = msg.Width
|
2022-10-07 22:23:28 +03:00
|
|
|
if m.reverse {
|
|
|
|
m.viewport.YOffset = clamp(0, len(m.matches), len(m.matches)-m.viewport.Height)
|
|
|
|
}
|
2022-07-06 19:07:18 +03:00
|
|
|
case tea.KeyMsg:
|
|
|
|
switch msg.String() {
|
2022-07-31 04:10:36 +03:00
|
|
|
case "ctrl+c", "esc":
|
|
|
|
m.aborted = true
|
2022-08-02 22:18:22 +03:00
|
|
|
m.quitting = true
|
|
|
|
return m, tea.Quit
|
2022-07-31 04:10:36 +03:00
|
|
|
case "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-10-02 19:47:41 +03:00
|
|
|
m.CursorDown()
|
2022-07-28 23:15:32 +03:00
|
|
|
case "ctrl+p", "ctrl+k", "up":
|
2022-10-02 19:47:41 +03:00
|
|
|
m.CursorUp()
|
2022-09-03 04:21:31 +03:00
|
|
|
case "tab":
|
|
|
|
if m.limit == 1 {
|
|
|
|
break // no op
|
|
|
|
}
|
2022-10-02 19:47:41 +03:00
|
|
|
m.ToggleSelection()
|
|
|
|
m.CursorDown()
|
|
|
|
case "shift+tab":
|
|
|
|
if m.limit == 1 {
|
|
|
|
break // no op
|
2022-08-02 22:18:22 +03:00
|
|
|
}
|
2022-10-02 19:47:41 +03:00
|
|
|
m.ToggleSelection()
|
|
|
|
m.CursorUp()
|
2023-02-28 02:59:40 +03:00
|
|
|
case "ctrl+@":
|
|
|
|
if m.limit == 1 {
|
|
|
|
break // no op
|
|
|
|
}
|
|
|
|
m.ToggleSelection()
|
2022-07-06 19:07:18 +03:00
|
|
|
default:
|
|
|
|
m.textinput, cmd = m.textinput.Update(msg)
|
|
|
|
|
2022-10-07 22:23:28 +03: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 17:47:27 +03: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 {
|
2023-05-30 17:19:53 +03:00
|
|
|
if m.sort {
|
|
|
|
m.matches = fuzzy.Find(m.textinput.Value(), m.choices)
|
|
|
|
} else {
|
|
|
|
m.matches = fuzzy.FindNoSort(m.textinput.Value(), m.choices)
|
|
|
|
}
|
2022-10-13 17:47:27 +03:00
|
|
|
} else {
|
|
|
|
m.matches = exactMatches(m.textinput.Value(), m.choices)
|
2022-10-09 01:25:53 +03:00
|
|
|
}
|
|
|
|
|
2022-07-06 19:07:18 +03: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 22:23:28 +03: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 19:07:18 +03: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 04:21:31 +03:00
|
|
|
m.cursor = clamp(0, len(m.matches)-1, m.cursor)
|
2022-07-06 19:07:18 +03:00
|
|
|
return m, cmd
|
|
|
|
}
|
|
|
|
|
2022-10-02 19:47:41 +03:00
|
|
|
func (m *model) CursorUp() {
|
2022-10-07 22:23:28 +03:00
|
|
|
if m.reverse {
|
2024-07-25 17:02:56 +03:00
|
|
|
m.cursor = (m.cursor + 1) % len(m.matches)
|
2022-10-07 22:23:28 +03:00
|
|
|
if len(m.matches)-m.cursor <= m.viewport.YOffset {
|
2024-07-25 17:02:56 +03:00
|
|
|
m.viewport.LineUp(1)
|
|
|
|
}
|
|
|
|
if len(m.matches)-m.cursor > m.viewport.Height+m.viewport.YOffset {
|
|
|
|
m.viewport.SetYOffset(len(m.matches) - m.viewport.Height)
|
2022-10-07 22:23:28 +03:00
|
|
|
}
|
|
|
|
} else {
|
2024-07-25 17:02:56 +03:00
|
|
|
m.cursor = (m.cursor - 1 + len(m.matches)) % len(m.matches)
|
2022-10-07 22:23:28 +03:00
|
|
|
if m.cursor < m.viewport.YOffset {
|
2024-07-25 17:02:56 +03:00
|
|
|
m.viewport.LineUp(1)
|
|
|
|
}
|
|
|
|
if m.cursor >= m.viewport.YOffset+m.viewport.Height {
|
|
|
|
m.viewport.SetYOffset(len(m.matches) - m.viewport.Height)
|
2022-10-07 22:23:28 +03:00
|
|
|
}
|
2022-10-02 19:47:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *model) CursorDown() {
|
2022-10-07 22:23:28 +03:00
|
|
|
if m.reverse {
|
2024-07-25 17:02:56 +03:00
|
|
|
m.cursor = (m.cursor - 1 + len(m.matches)) % len(m.matches)
|
2022-10-07 22:23:28 +03:00
|
|
|
if len(m.matches)-m.cursor > m.viewport.Height+m.viewport.YOffset {
|
|
|
|
m.viewport.LineDown(1)
|
|
|
|
}
|
2024-07-25 17:02:56 +03:00
|
|
|
if len(m.matches)-m.cursor <= m.viewport.YOffset {
|
|
|
|
m.viewport.GotoTop()
|
|
|
|
}
|
2022-10-07 22:23:28 +03:00
|
|
|
} else {
|
2024-07-25 17:02:56 +03:00
|
|
|
m.cursor = (m.cursor + 1) % len(m.matches)
|
2022-10-07 22:23:28 +03:00
|
|
|
if m.cursor >= m.viewport.YOffset+m.viewport.Height {
|
|
|
|
m.viewport.LineDown(1)
|
|
|
|
}
|
2024-07-25 17:02:56 +03:00
|
|
|
if m.cursor < m.viewport.YOffset {
|
|
|
|
m.viewport.GotoTop()
|
|
|
|
}
|
2022-10-02 19:47:41 +03: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 19:07:18 +03:00
|
|
|
func matchAll(options []string) []fuzzy.Match {
|
2022-10-07 22:23:28 +03:00
|
|
|
matches := make([]fuzzy.Match, len(options))
|
2022-08-05 19:22:09 +03:00
|
|
|
for i, option := range options {
|
|
|
|
matches[i] = fuzzy.Match{Str: option}
|
2022-07-06 19:07:18 +03:00
|
|
|
}
|
|
|
|
return matches
|
|
|
|
}
|
|
|
|
|
2022-10-09 10:38:48 +03:00
|
|
|
func exactMatches(search string, choices []string) []fuzzy.Match {
|
2022-10-13 17:47:27 +03:00
|
|
|
matches := fuzzy.Matches{}
|
2022-10-09 10:38:48 +03:00
|
|
|
for i, choice := range choices {
|
2022-10-09 01:25:53 +03:00
|
|
|
search = strings.ToLower(search)
|
2022-10-09 10:38:48 +03:00
|
|
|
matchedString := strings.ToLower(choice)
|
2022-10-09 01:25:53 +03:00
|
|
|
|
|
|
|
index := strings.Index(matchedString, search)
|
|
|
|
if index >= 0 {
|
2022-10-09 10:38:48 +03:00
|
|
|
matchedIndexes := []int{}
|
|
|
|
for s := range search {
|
|
|
|
matchedIndexes = append(matchedIndexes, index+s)
|
2022-10-09 01:25:53 +03:00
|
|
|
}
|
2022-10-13 17:47:27 +03:00
|
|
|
matches = append(matches, fuzzy.Match{
|
2022-10-09 10:38:48 +03:00
|
|
|
Str: choice,
|
|
|
|
Index: i,
|
|
|
|
MatchedIndexes: matchedIndexes,
|
|
|
|
})
|
2022-10-09 01:25:53 +03:00
|
|
|
}
|
|
|
|
}
|
2022-10-09 01:33:15 +03:00
|
|
|
|
2022-10-13 17:47:27 +03:00
|
|
|
return matches
|
2022-10-09 01:25:53 +03:00
|
|
|
}
|
|
|
|
|
2022-09-03 04:39:01 +03:00
|
|
|
//nolint:unparam
|
2022-07-06 19:07:18 +03:00
|
|
|
func clamp(min, max, val int) int {
|
|
|
|
if val < min {
|
|
|
|
return min
|
|
|
|
}
|
|
|
|
if val > max {
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|