Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

load files asyncronously #17

Merged
merged 2 commits into from Mar 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
119 changes: 78 additions & 41 deletions cmd/gofzf/main.go
Expand Up @@ -2,14 +2,17 @@ package main

import (
"bufio"
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
"runtime/debug"
"sync"

"github.com/koki-develop/go-fzf"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)

const (
Expand Down Expand Up @@ -83,49 +86,14 @@ var rootCmd = &cobra.Command{
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
var items []string

info, err := os.Stdin.Stat()
if err != nil {
return err
}

if info.Mode()&os.ModeCharDevice == 0 {
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
items = append(items, sc.Text())
}
} else {
wd, err := os.Getwd()
if err != nil {
return err
}
err = filepath.WalkDir(wd, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

if d.Name()[0] == '.' {
if d.IsDir() {
return fs.SkipDir
}
return nil
}

if !d.IsDir() {
items = append(items, path)
}

return nil
})
if err != nil {
return err
}
}
var mu sync.RWMutex

f := fzf.New(
fzf.WithNoLimit(flagNoLimit),
fzf.WithLimit(flagLimit),

fzf.WithHotReload(mu.RLocker()),

fzf.WithPrompt(flagPrompt),
fzf.WithCursor(flagCursor),
fzf.WithSelectedPrefix(flagSelectedPrefix),
Expand Down Expand Up @@ -187,14 +155,83 @@ var rootCmd = &cobra.Command{
}),
),
)
choices, err := f.Find(items, func(i int) string { return items[i] })

ctx := context.Background()
g, ctx := errgroup.WithContext(ctx)

info, err := os.Stdin.Stat()
if err != nil {
return err
}

for _, choice := range choices {
fmt.Println(items[choice])
if info.Mode()&os.ModeCharDevice == 0 {
g.Go(func() error {
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
mu.Lock()
items = append(items, sc.Text())
mu.Unlock()
}
return nil
})
} else {
wd, err := os.Getwd()
if err != nil {
return err
}

g.Go(func() error {
err := filepath.WalkDir(wd, func(path string, d fs.DirEntry, err error) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
}

if err != nil {
return err
}

if d.Name()[0] == '.' {
if d.IsDir() {
return fs.SkipDir
}
return nil
}

if !d.IsDir() {
mu.Lock()
items = append(items, path)
mu.Unlock()
}

return nil
})
if err != nil {
f.Quit()
return err
}
return nil
})
}

g.Go(func() error {
choices, err := f.Find(&items, func(i int) string { return items[i] })
if err != nil {
return err
}

for _, choice := range choices {
fmt.Println(items[choice])
}

return nil
})

if err := g.Wait(); err != nil {
return err
}

return nil
},
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -7,6 +7,7 @@ require (
github.com/charmbracelet/bubbletea v0.23.2
github.com/charmbracelet/lipgloss v0.7.1
github.com/spf13/cobra v1.6.1
golang.org/x/sync v0.1.0
)

require (
Expand All @@ -24,7 +25,6 @@ require (
github.com/muesli/termenv v0.15.1 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.8 // indirect
Expand Down