Add option to search for directories through fzgo itself
This commit is contained in:
parent
430ff540f5
commit
a005938761
56
src/data.go
56
src/data.go
|
@ -4,9 +4,11 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/charmbracelet/bubbles/list"
|
"github.com/charmbracelet/bubbles/list"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -152,8 +154,60 @@ func getPathsFromStdin() []string {
|
||||||
return paths
|
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{}
|
items := []list.Item{}
|
||||||
// paths := getPathsFromFile("/home/user/.cache/fzy_paths_d")
|
// paths := getPathsFromFile("/home/user/.cache/fzy_paths_d")
|
||||||
paths := getPathsFromStdin()
|
paths := getPathsFromStdin()
|
||||||
|
|
30
src/main.go
30
src/main.go
|
@ -1,10 +1,12 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/charmbracelet/bubbles/list"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
"github.com/muesli/termenv"
|
"github.com/muesli/termenv"
|
||||||
|
@ -12,8 +14,32 @@ import (
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
lipgloss.DefaultRenderer().SetColorProfile(termenv.TrueColor)
|
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 {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
} else {
|
} else {
|
||||||
|
|
11
src/tui.go
11
src/tui.go
|
@ -5,17 +5,18 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/charmbracelet/bubbles/list"
|
||||||
"github.com/charmbracelet/bubbles/textinput"
|
"github.com/charmbracelet/bubbles/textinput"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
"github.com/charmbracelet/bubbles/list"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type item struct {
|
type item struct {
|
||||||
value string
|
value string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i item) FilterValue() string { return i.value }
|
func (i item) FilterValue() string { return i.value }
|
||||||
|
|
||||||
type itemDelegate struct{}
|
type itemDelegate struct{}
|
||||||
|
|
||||||
func (d itemDelegate) Height() int { return 1 }
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
str := fmt.Sprintf("%s", i.value)
|
str := i.value
|
||||||
|
|
||||||
fn := lipgloss.NewStyle().PaddingLeft(2).Render
|
fn := lipgloss.NewStyle().PaddingLeft(2).Render
|
||||||
if index == m.Index() {
|
if index == m.Index() {
|
||||||
|
@ -47,7 +48,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
switch msg.Type {
|
switch msg.Type {
|
||||||
case tea.KeyCtrlC, tea.KeyEsc:
|
case tea.KeyCtrlC, tea.KeyEsc:
|
||||||
m.quitting = true;
|
m.quitting = true
|
||||||
return m, tea.Quit
|
return m, tea.Quit
|
||||||
case tea.KeyEnter:
|
case tea.KeyEnter:
|
||||||
i, ok := m.list.SelectedItem().(item)
|
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)
|
m.textInput, cmd = m.textInput.Update(msg)
|
||||||
cmds = append(cmds, cmd)
|
cmds = append(cmds, cmd)
|
||||||
|
|
||||||
|
|
||||||
if !m.list.SettingFilter() {
|
if !m.list.SettingFilter() {
|
||||||
m.list, cmd = m.list.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("/")})
|
m.list, cmd = m.list.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("/")})
|
||||||
cmds = append(cmds, cmd)
|
cmds = append(cmds, cmd)
|
||||||
|
@ -105,7 +105,6 @@ func (m model) View() string {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
type (
|
type (
|
||||||
errMsg error
|
errMsg error
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue