From a2ca83fe79e4ca1e71b51bbfc194a3fee0413b3e Mon Sep 17 00:00:00 2001 From: koki-develop Date: Sun, 26 Mar 2023 10:29:05 +0900 Subject: [PATCH] search files when no stdin --- cmd/gofzf/main.go | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/cmd/gofzf/main.go b/cmd/gofzf/main.go index 782c271..cfee004 100644 --- a/cmd/gofzf/main.go +++ b/cmd/gofzf/main.go @@ -3,7 +3,9 @@ package main import ( "bufio" "fmt" + "io/fs" "os" + "path/filepath" "runtime/debug" "github.com/koki-develop/go-fzf" @@ -78,11 +80,44 @@ var rootCmd = &cobra.Command{ Use: "gofzf", SilenceUsage: true, RunE: func(cmd *cobra.Command, args []string) error { - sc := bufio.NewScanner(os.Stdin) - var items []string - for sc.Scan() { - items = append(items, sc.Text()) + + 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 + } } f := fzf.New(