Skip to content

Commit

Permalink
Merge pull request #8 from koki-develop/search-files
Browse files Browse the repository at this point in the history
  • Loading branch information
koki-develop committed Mar 26, 2023
2 parents e44823a + a2ca83f commit c631c58
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
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

0 comments on commit c631c58

Please sign in to comment.