Skip to content

Commit

Permalink
Refactor search, moving Bubbletea dependencies
Browse files Browse the repository at this point in the history
Move out all of Bubbletea dependencies, belonging to TUI.
  • Loading branch information
carhartl committed Dec 8, 2024
1 parent 57142bb commit 26ac243
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 36 deletions.
65 changes: 31 additions & 34 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"os/exec"
"path/filepath"
"regexp"

tea "github.com/charmbracelet/bubbletea"
)

type repo struct {
Expand All @@ -17,44 +15,43 @@ type repo struct {

var excludeDirs = regexp.MustCompile(`.+/(\..+|node_modules)`) // Skip hidden directories (incl. .git) and node_modules

func getRepos(path string, sub chan repo) tea.Cmd {
return func() tea.Msg {
path, err := filepath.Abs(path)
func collectDirtyRepos(path string, sub chan repo) error {
path, err := filepath.Abs(path)
if err != nil {
return err
}

err = filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() && excludeDirs.MatchString(path) {
if filepath.Base(path) == ".git" {
repopath := filepath.Dir(path)

var buf = new(bytes.Buffer)
cmd := exec.Command("git", "status", "--porcelain=v2", "--show-stash", "--branch")
cmd.Stdout = buf
cmd.Dir = repopath
err = cmd.Run()
if err != nil {
return err
}

err = filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() && excludeDirs.MatchString(path) {
if filepath.Base(path) == ".git" {
repopath := filepath.Dir(path)

var buf = new(bytes.Buffer)
cmd := exec.Command("git", "status", "--porcelain=v2", "--show-stash", "--branch")
cmd.Stdout = buf
cmd.Dir = repopath
err = cmd.Run()
if err != nil {
return err
}

gi := GitInfo{}
gi.Parse(buf)

if !gi.IsClean() {
sub <- repo{path: repopath, status: gi.Summary()}
}
gi := GitInfo{}
gi.Parse(buf)

if !gi.IsClean() {
sub <- repo{path: repopath, status: gi.Summary()}
}
return fs.SkipDir
}
return nil
})
if err != nil {
return err
return fs.SkipDir
}
return doneMsg{}
return nil
})
if err != nil {
return err
}

return nil
}
2 changes: 1 addition & 1 deletion search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestDetectGitRepo(t *testing.T) {

output := make(chan repo)
go func() {
getRepos(d, output)()
_ = collectDirtyRepos(d, output)
defer close(output)
}()

Expand Down
12 changes: 11 additions & 1 deletion tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type model struct {
func (m model) Init() tea.Cmd {
return tea.Batch(
m.list.StartSpinner(),
getRepos(m.path, m.sub),
startRepoSearch(m.path, m.sub),
waitForRepoStatus(m.sub),
)
}
Expand Down Expand Up @@ -110,6 +110,16 @@ func additionalShortHelpKeys() []key.Binding {
}
}

func startRepoSearch(path string, sub chan repo) tea.Cmd {
return func() tea.Msg {
err := collectDirtyRepos(path, sub)
if err != nil {
return err
}
return doneMsg{}
}
}

func waitForRepoStatus(sub chan repo) tea.Cmd {
return func() tea.Msg {
repo := <-sub
Expand Down

0 comments on commit 26ac243

Please sign in to comment.