Add option to search for directories through fzgo itself

This commit is contained in:
Thomas Avé 2025-01-27 16:22:38 +01:00
parent 430ff540f5
commit a005938761
3 changed files with 218 additions and 139 deletions

View File

@ -4,9 +4,11 @@ import (
"bufio"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"github.com/charmbracelet/bubbles/list"
)
@ -43,14 +45,14 @@ func computeScore(term string, s string) float64 {
}
if strings.Contains(c, term) {
used[i] = true
score += 1.0/float64(j)
score += 1.0 / float64(j)
splits = splits[j+1:]
break
}
}
}
if score > 0 {
score += 1.0/float64(strings.Count(a, "/")+1)
score += 1.0 / float64(strings.Count(a, "/")+1)
}
return score
}
@ -152,8 +154,60 @@ func getPathsFromStdin() []string {
return paths
}
func findSubdirectories(root string) ([]string, []string, error) {
var dirs []string
var files []string
func getListItems() []list.Item {
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Skip hidden files and directories (those starting with a dot)
baseName := filepath.Base(path)
is_dotfile := strings.HasPrefix(baseName, ".")
if info.IsDir() {
if is_dotfile && filepath.Clean(path) != filepath.Clean(root) {
return filepath.SkipDir
}
dirs = append(dirs, path)
} else {
if is_dotfile {
return nil
}
files = append(files, path)
}
return nil
})
if err != nil {
return nil, nil, fmt.Errorf("error walking directory tree: %v", err)
}
return dirs, files, nil
}
func getListItemsFromDir(root string, searchDirs bool, searchFiles bool) []list.Item {
items := []list.Item{}
dirs, files, err := findSubdirectories(root)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return items
}
if searchFiles {
for _, file := range files {
items = append(items, item{file})
}
}
if searchDirs {
for _, dir := range dirs {
items = append(items, item{dir})
}
}
return items
}
func getListItemsFromStdin() []list.Item {
items := []list.Item{}
// paths := getPathsFromFile("/home/user/.cache/fzy_paths_d")
paths := getPathsFromStdin()

View File

@ -1,10 +1,12 @@
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
@ -12,8 +14,32 @@ import (
func main() {
lipgloss.DefaultRenderer().SetColorProfile(termenv.TrueColor)
p := tea.NewProgram(initialModel(getListItems()), tea.WithOutput(os.Stderr))
m, err := p.Run();
// Command-line flags
searchFiles := flag.Bool("f", false, "Search for files")
searchDirs := flag.Bool("d", false, "Search for directories")
flag.Parse()
if !*searchFiles && !*searchDirs {
*searchFiles = true
*searchDirs = true
}
// Gather list items for each path
var items []list.Item
// Read paths from command line arguments
if len(flag.Args()) < 1 {
items = getListItemsFromStdin()
} else {
paths := flag.Args()
for _, path := range paths {
items = append(items, getListItemsFromDir(path, *searchDirs, *searchFiles)...)
}
}
p := tea.NewProgram(initialModel(items), tea.WithOutput(os.Stderr))
m, err := p.Run()
if err != nil {
log.Fatal(err)
} else {

View File

@ -5,17 +5,18 @@ import (
"io"
"strings"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/bubbles/list"
)
type item struct {
value string
}
func (i item) FilterValue() string { return i.value }
type itemDelegate struct{}
func (d itemDelegate) Height() int { return 1 }
@ -27,7 +28,7 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list
return
}
str := fmt.Sprintf("%s", i.value)
str := i.value
fn := lipgloss.NewStyle().PaddingLeft(2).Render
if index == m.Index() {
@ -47,7 +48,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyCtrlC, tea.KeyEsc:
m.quitting = true;
m.quitting = true
return m, tea.Quit
case tea.KeyEnter:
i, ok := m.list.SelectedItem().(item)
@ -82,7 +83,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.textInput, cmd = m.textInput.Update(msg)
cmds = append(cmds, cmd)
if !m.list.SettingFilter() {
m.list, cmd = m.list.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("/")})
cmds = append(cmds, cmd)
@ -105,7 +105,6 @@ func (m model) View() string {
)
}
type (
errMsg error
)