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

(cli) search files when no stdin #8

Merged
merged 4 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
47 changes: 41 additions & 6 deletions cmd/gofzf/main.go
Expand Up @@ -3,7 +3,9 @@ package main
import (
"bufio"
"fmt"
"io/fs"
"os"
"path/filepath"
"runtime/debug"

"github.com/koki-develop/go-fzf"
Expand Down Expand Up @@ -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

var is []string
for sc.Scan() {
is = append(is, 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(
Expand Down Expand Up @@ -147,13 +182,13 @@ var rootCmd = &cobra.Command{
}),
),
)
choices, err := f.Find(is, func(i int) string { return is[i] })
choices, err := f.Find(items, func(i int) string { return items[i] })
if err != nil {
return err
}

for _, choice := range choices {
fmt.Println(is[choice])
fmt.Println(items[choice])
}
return nil
},
Expand Down
15 changes: 12 additions & 3 deletions model.go
@@ -1,7 +1,6 @@
package fzf

import (
"fmt"
"strings"

"github.com/charmbracelet/bubbles/key"
Expand All @@ -26,6 +25,8 @@ type model struct {
nocursor string
cursorPosition int

promptWidth int

selectedPrefix string
unselectedPrefix string

Expand Down Expand Up @@ -65,6 +66,8 @@ func newModel(fzf *FZF, items *items) *model {
nocursor: strings.Repeat(" ", lipgloss.Width(fzf.option.cursor)),
cursorPosition: 0,

promptWidth: lipgloss.Width(fzf.option.prompt),

selectedPrefix: fzf.option.styles.option.selectedPrefix.Render(fzf.option.selectedPrefix),
unselectedPrefix: fzf.option.styles.option.unselectedPrefix.Render(fzf.option.unselectedPrefix),

Expand Down Expand Up @@ -95,7 +98,13 @@ func (m *model) Init() tea.Cmd {
*/

func (m *model) View() string {
return fmt.Sprintf("%s\n%s", m.headerView(), m.itemsView())
var v strings.Builder

_, _ = v.WriteString(m.headerView())
_, _ = v.WriteRune('\n')
_, _ = v.WriteString(m.itemsView())

return v.String()
}

func (m *model) headerView() string {
Expand Down Expand Up @@ -201,7 +210,7 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// window
m.windowWidth = msg.Width
m.windowHeight = msg.Height
m.input.Width = m.windowWidth - 3
m.input.Width = m.windowWidth - m.promptWidth
}

var cmds []tea.Cmd
Expand Down