summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authoradamdottv <[email protected]>2025-06-13 07:01:26 -0500
committeradamdottv <[email protected]>2025-06-13 07:01:26 -0500
commit4169f0c412a3bcd06ae117c4bb9d223fe743adc7 (patch)
tree97411949c34408fb558f42c6b0f7335fe4717756
parentb7f06bbc1f8359f6a78de697fbb5c2e4b5a6ebb7 (diff)
downloadopencode-4169f0c412a3bcd06ae117c4bb9d223fe743adc7.tar.gz
opencode-4169f0c412a3bcd06ae117c4bb9d223fe743adc7.zip
wip: refactoring tui
-rw-r--r--packages/tui/internal/app/app.go2
-rw-r--r--packages/tui/internal/completions/files-folders.go145
-rw-r--r--packages/tui/internal/fileutil/fileutil.go163
-rw-r--r--packages/tui/internal/tui/tui.go5
4 files changed, 1 insertions, 314 deletions
diff --git a/packages/tui/internal/app/app.go b/packages/tui/internal/app/app.go
index 69900a0bd..5baa15bff 100644
--- a/packages/tui/internal/app/app.go
+++ b/packages/tui/internal/app/app.go
@@ -10,7 +10,6 @@ import (
tea "github.com/charmbracelet/bubbletea/v2"
"github.com/sst/opencode/internal/config"
- "github.com/sst/opencode/internal/fileutil"
"github.com/sst/opencode/internal/state"
"github.com/sst/opencode/internal/status"
"github.com/sst/opencode/internal/theme"
@@ -121,7 +120,6 @@ func New(ctx context.Context, version string, httpClient *client.ClientWithRespo
}
theme.SetTheme(appConfig.Theme)
- fileutil.Init()
return app, nil
}
diff --git a/packages/tui/internal/completions/files-folders.go b/packages/tui/internal/completions/files-folders.go
index 6077b880f..a0c5260de 100644
--- a/packages/tui/internal/completions/files-folders.go
+++ b/packages/tui/internal/completions/files-folders.go
@@ -1,14 +1,6 @@
package completions
import (
- "bytes"
- "fmt"
- "os/exec"
- "path/filepath"
-
- "github.com/lithammer/fuzzysearch/fuzzy"
- "github.com/sst/opencode/internal/fileutil"
- "github.com/sst/opencode/internal/status"
"github.com/sst/opencode/internal/components/dialog"
)
@@ -27,143 +19,8 @@ func (cg *filesAndFoldersContextGroup) GetEntry() dialog.CompletionItemI {
})
}
-func processNullTerminatedOutput(outputBytes []byte) []string {
- if len(outputBytes) > 0 && outputBytes[len(outputBytes)-1] == 0 {
- outputBytes = outputBytes[:len(outputBytes)-1]
- }
-
- if len(outputBytes) == 0 {
- return []string{}
- }
-
- split := bytes.Split(outputBytes, []byte{0})
- matches := make([]string, 0, len(split))
-
- for _, p := range split {
- if len(p) == 0 {
- continue
- }
-
- path := string(p)
- path = filepath.Join(".", path)
-
- if !fileutil.SkipHidden(path) {
- matches = append(matches, path)
- }
- }
-
- return matches
-}
-
func (cg *filesAndFoldersContextGroup) getFiles(query string) ([]string, error) {
- cmdRg := fileutil.GetRgCmd("") // No glob pattern for this use case
- cmdFzf := fileutil.GetFzfCmd(query)
-
- var matches []string
- // Case 1: Both rg and fzf available
- if cmdRg != nil && cmdFzf != nil {
- rgPipe, err := cmdRg.StdoutPipe()
- if err != nil {
- return nil, fmt.Errorf("failed to get rg stdout pipe: %w", err)
- }
- defer rgPipe.Close()
-
- cmdFzf.Stdin = rgPipe
- var fzfOut bytes.Buffer
- var fzfErr bytes.Buffer
- cmdFzf.Stdout = &fzfOut
- cmdFzf.Stderr = &fzfErr
-
- if err := cmdFzf.Start(); err != nil {
- return nil, fmt.Errorf("failed to start fzf: %w", err)
- }
-
- errRg := cmdRg.Run()
- errFzf := cmdFzf.Wait()
-
- if errRg != nil {
- status.Warn(fmt.Sprintf("rg command failed during pipe: %v", errRg))
- }
-
- if errFzf != nil {
- if exitErr, ok := errFzf.(*exec.ExitError); ok && exitErr.ExitCode() == 1 {
- return []string{}, nil // No matches from fzf
- }
- return nil, fmt.Errorf("fzf command failed: %w\nStderr: %s", errFzf, fzfErr.String())
- }
-
- matches = processNullTerminatedOutput(fzfOut.Bytes())
-
- // Case 2: Only rg available
- } else if cmdRg != nil {
- status.Debug("Using Ripgrep with fuzzy match fallback for file completions")
- var rgOut bytes.Buffer
- var rgErr bytes.Buffer
- cmdRg.Stdout = &rgOut
- cmdRg.Stderr = &rgErr
-
- if err := cmdRg.Run(); err != nil {
- return nil, fmt.Errorf("rg command failed: %w\nStderr: %s", err, rgErr.String())
- }
-
- allFiles := processNullTerminatedOutput(rgOut.Bytes())
- matches = fuzzy.Find(query, allFiles)
-
- // Case 3: Only fzf available
- } else if cmdFzf != nil {
- status.Debug("Using FZF with doublestar fallback for file completions")
- files, _, err := fileutil.GlobWithDoublestar("**/*", ".", 0)
- if err != nil {
- return nil, fmt.Errorf("failed to list files for fzf: %w", err)
- }
-
- allFiles := make([]string, 0, len(files))
- for _, file := range files {
- if !fileutil.SkipHidden(file) {
- allFiles = append(allFiles, file)
- }
- }
-
- var fzfIn bytes.Buffer
- for _, file := range allFiles {
- fzfIn.WriteString(file)
- fzfIn.WriteByte(0)
- }
-
- cmdFzf.Stdin = &fzfIn
- var fzfOut bytes.Buffer
- var fzfErr bytes.Buffer
- cmdFzf.Stdout = &fzfOut
- cmdFzf.Stderr = &fzfErr
-
- if err := cmdFzf.Run(); err != nil {
- if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() == 1 {
- return []string{}, nil
- }
- return nil, fmt.Errorf("fzf command failed: %w\nStderr: %s", err, fzfErr.String())
- }
-
- matches = processNullTerminatedOutput(fzfOut.Bytes())
-
- // Case 4: Fallback to doublestar with fuzzy match
- } else {
- status.Debug("Using doublestar with fuzzy match for file completions")
- allFiles, _, err := fileutil.GlobWithDoublestar("**/*", ".", 0)
- if err != nil {
- return nil, fmt.Errorf("failed to glob files: %w", err)
- }
-
- filteredFiles := make([]string, 0, len(allFiles))
- for _, file := range allFiles {
- if !fileutil.SkipHidden(file) {
- filteredFiles = append(filteredFiles, file)
- }
- }
-
- matches = fuzzy.Find(query, filteredFiles)
- }
-
- return matches, nil
+ return []string{}, nil
}
func (cg *filesAndFoldersContextGroup) GetChildEntries(query string) ([]dialog.CompletionItemI, error) {
diff --git a/packages/tui/internal/fileutil/fileutil.go b/packages/tui/internal/fileutil/fileutil.go
deleted file mode 100644
index b48152f7a..000000000
--- a/packages/tui/internal/fileutil/fileutil.go
+++ /dev/null
@@ -1,163 +0,0 @@
-package fileutil
-
-import (
- "fmt"
- "io/fs"
- "os"
- "os/exec"
- "path/filepath"
- "sort"
- "strings"
- "time"
-
- "github.com/bmatcuk/doublestar/v4"
- "github.com/sst/opencode/internal/status"
-)
-
-var (
- rgPath string
- fzfPath string
-)
-
-func Init() {
- var err error
- rgPath, err = exec.LookPath("rg")
- if err != nil {
- status.Warn("Ripgrep (rg) not found in $PATH. Some features might be limited or slower.")
- rgPath = ""
- }
- fzfPath, err = exec.LookPath("fzf")
- if err != nil {
- status.Warn("FZF not found in $PATH. Some features might be limited or slower.")
- fzfPath = ""
- }
-}
-
-func GetRgCmd(globPattern string) *exec.Cmd {
- if rgPath == "" {
- return nil
- }
- rgArgs := []string{
- "--files",
- "-L",
- "--null",
- }
- if globPattern != "" {
- if !filepath.IsAbs(globPattern) && !strings.HasPrefix(globPattern, "/") {
- globPattern = "/" + globPattern
- }
- rgArgs = append(rgArgs, "--glob", globPattern)
- }
- cmd := exec.Command(rgPath, rgArgs...)
- cmd.Dir = "."
- return cmd
-}
-
-func GetFzfCmd(query string) *exec.Cmd {
- if fzfPath == "" {
- return nil
- }
- fzfArgs := []string{
- "--filter",
- query,
- "--read0",
- "--print0",
- }
- cmd := exec.Command(fzfPath, fzfArgs...)
- cmd.Dir = "."
- return cmd
-}
-
-type FileInfo struct {
- Path string
- ModTime time.Time
-}
-
-func SkipHidden(path string) bool {
- // Check for hidden files (starting with a dot)
- base := filepath.Base(path)
- if base != "." && strings.HasPrefix(base, ".") {
- return true
- }
-
- commonIgnoredDirs := map[string]bool{
- ".opencode": true,
- "node_modules": true,
- "vendor": true,
- "dist": true,
- "build": true,
- "target": true,
- ".git": true,
- ".idea": true,
- ".vscode": true,
- "__pycache__": true,
- "bin": true,
- "obj": true,
- "out": true,
- "coverage": true,
- "tmp": true,
- "temp": true,
- "logs": true,
- "generated": true,
- "bower_components": true,
- "jspm_packages": true,
- }
-
- parts := strings.Split(path, string(os.PathSeparator))
- for _, part := range parts {
- if commonIgnoredDirs[part] {
- return true
- }
- }
- return false
-}
-
-func GlobWithDoublestar(pattern, searchPath string, limit int) ([]string, bool, error) {
- fsys := os.DirFS(searchPath)
- relPattern := strings.TrimPrefix(pattern, "/")
- var matches []FileInfo
-
- err := doublestar.GlobWalk(fsys, relPattern, func(path string, d fs.DirEntry) error {
- if d.IsDir() {
- return nil
- }
- if SkipHidden(path) {
- return nil
- }
- info, err := d.Info()
- if err != nil {
- return nil
- }
- absPath := path
- if !strings.HasPrefix(absPath, searchPath) && searchPath != "." {
- absPath = filepath.Join(searchPath, absPath)
- } else if !strings.HasPrefix(absPath, "/") && searchPath == "." {
- absPath = filepath.Join(searchPath, absPath) // Ensure relative paths are joined correctly
- }
-
- matches = append(matches, FileInfo{Path: absPath, ModTime: info.ModTime()})
- if limit > 0 && len(matches) >= limit*2 {
- return fs.SkipAll
- }
- return nil
- })
- if err != nil {
- return nil, false, fmt.Errorf("glob walk error: %w", err)
- }
-
- sort.Slice(matches, func(i, j int) bool {
- return matches[i].ModTime.After(matches[j].ModTime)
- })
-
- truncated := false
- if limit > 0 && len(matches) > limit {
- matches = matches[:limit]
- truncated = true
- }
-
- results := make([]string, len(matches))
- for i, m := range matches {
- results[i] = m.Path
- }
- return results, truncated, nil
-}
diff --git a/packages/tui/internal/tui/tui.go b/packages/tui/internal/tui/tui.go
index 6de884078..dfeb30fc4 100644
--- a/packages/tui/internal/tui/tui.go
+++ b/packages/tui/internal/tui/tui.go
@@ -326,11 +326,6 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
a.modal = quitDialog
return a, nil
}
-
- default:
- // f, filepickerCmd := a.filepicker.Update(msg)
- // a.filepicker = f.(dialog.FilepickerComponent)
- // cmds = append(cmds, filepickerCmd)
}
// update status bar