Basic filter
This commit is contained in:
parent
4ed91206ca
commit
35d898845c
|
@ -0,0 +1,58 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/charmbracelet/bubbles/list"
|
||||
)
|
||||
|
||||
type Ranking struct {
|
||||
items []list.Item
|
||||
filter string
|
||||
}
|
||||
|
||||
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) Less(i, j int) bool {
|
||||
return len(a.items[i].FilterValue()) < len(a.items[j].FilterValue())
|
||||
}
|
||||
|
||||
|
||||
func getPaths() []string {
|
||||
paths := []string{}
|
||||
|
||||
file, err := os.Open("/home/user/.cache/fzy_paths_d")
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Error opening file:", err)
|
||||
return paths
|
||||
}
|
||||
defer file.Close() // Ensure the file is closed when we're done
|
||||
|
||||
// Create a scanner to read the file line by line
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if len(line) == 0 {
|
||||
continue
|
||||
}
|
||||
paths = append(paths, line)
|
||||
}
|
||||
|
||||
// Check for errors during scanning
|
||||
if err := scanner.Err(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Error reading file:", err)
|
||||
}
|
||||
return paths
|
||||
}
|
||||
|
||||
func getListItems() []list.Item {
|
||||
items := []list.Item{}
|
||||
paths := getPaths()
|
||||
// TODO: sort initial list based on most often used
|
||||
for _, path := range paths {
|
||||
items = append(items, item{path})
|
||||
}
|
||||
return items
|
||||
}
|
8
main.go
8
main.go
|
@ -5,20 +5,14 @@ import (
|
|||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/charmbracelet/bubbles/list"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/muesli/termenv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
items := []list.Item{
|
||||
item("/home/user/Workspace"),
|
||||
item("/home/user/Downloads"),
|
||||
}
|
||||
|
||||
lipgloss.DefaultRenderer().SetColorProfile(termenv.TrueColor)
|
||||
p := tea.NewProgram(initialModel(items), tea.WithOutput(os.Stderr))
|
||||
p := tea.NewProgram(initialModel(getListItems()), tea.WithOutput(os.Stderr))
|
||||
m, err := p.Run();
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
|
22
tui.go
22
tui.go
|
@ -12,8 +12,10 @@ import (
|
|||
)
|
||||
|
||||
|
||||
type item string
|
||||
func (i item) FilterValue() string { return "" }
|
||||
type item struct {
|
||||
value string
|
||||
}
|
||||
func (i item) FilterValue() string { return i.value }
|
||||
type itemDelegate struct{}
|
||||
|
||||
func (d itemDelegate) Height() int { return 1 }
|
||||
|
@ -25,7 +27,7 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list
|
|||
return
|
||||
}
|
||||
|
||||
str := fmt.Sprintf("%d. %s", index+1, i)
|
||||
str := fmt.Sprintf("%s", i.value)
|
||||
|
||||
fn := lipgloss.NewStyle().PaddingLeft(2).Render
|
||||
if index == m.Index() {
|
||||
|
@ -39,6 +41,7 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list
|
|||
|
||||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
var cmd tea.Cmd
|
||||
ignoreInput := false
|
||||
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
|
@ -49,13 +52,15 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
case tea.KeyEnter:
|
||||
i, ok := m.list.SelectedItem().(item)
|
||||
if ok {
|
||||
m.choice = string(i)
|
||||
m.choice = i.value
|
||||
}
|
||||
return m, tea.Quit
|
||||
case tea.KeyUp, tea.KeyCtrlK:
|
||||
m.list.CursorUp()
|
||||
ignoreInput = true
|
||||
case tea.KeyDown, tea.KeyCtrlJ:
|
||||
m.list.CursorDown()
|
||||
ignoreInput = true
|
||||
}
|
||||
|
||||
// We handle errors just like any other message
|
||||
|
@ -68,7 +73,12 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
m.list.SetWidth(msg.Width)
|
||||
return m, nil
|
||||
}
|
||||
|
||||
if ignoreInput {
|
||||
return m, cmd
|
||||
}
|
||||
m.textInput, cmd = m.textInput.Update(msg)
|
||||
m.list, cmd = m.list.Update(msg)
|
||||
return m, cmd
|
||||
}
|
||||
|
||||
|
@ -102,8 +112,9 @@ func initialModel(items []list.Item) model {
|
|||
const defaultWidth = 20
|
||||
|
||||
l := list.New(items, itemDelegate{}, defaultWidth, 14)
|
||||
l.SetShowFilter(false)
|
||||
l.SetShowStatusBar(false)
|
||||
l.SetFilteringEnabled(false)
|
||||
l.SetFilteringEnabled(true)
|
||||
l.SetShowHelp(false)
|
||||
l.SetShowTitle(false)
|
||||
l.SetShowPagination(false)
|
||||
|
@ -111,6 +122,7 @@ func initialModel(items []list.Item) model {
|
|||
ti.Focus()
|
||||
ti.CharLimit = 156
|
||||
ti.Width = 20
|
||||
l.FilterInput = ti
|
||||
|
||||
return model{
|
||||
textInput: ti,
|
||||
|
|
Loading…
Reference in New Issue