79 lines
1.7 KiB
Go
79 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"sort"
|
|
|
|
"github.com/charmbracelet/bubbles/list"
|
|
)
|
|
|
|
type Ranking struct {
|
|
items []string
|
|
indexes []int
|
|
filter string
|
|
}
|
|
|
|
func (a Ranking) Len() int { return len(a.items) }
|
|
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 {
|
|
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 {
|
|
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
|
|
}
|