59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
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
|
|
}
|