mirror of
https://github.com/charmbracelet/crush.git
synced 2025-08-02 05:20:46 +03:00
* feat: support .crushignore as well as .gitignore * docs: update * refactor: simplify * chore: fmt * feat: grep should support gitignore/crushignore * fix: small fixes * fix: small fixes * fix: ripgrep * fix: rg * fix: tst * test: fixes * refactor: organized code a bit * fix: try * fix: temp * chore: lint --------- Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package tools
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/charmbracelet/crush/internal/log"
|
|
)
|
|
|
|
var getRg = sync.OnceValue(func() string {
|
|
path, err := exec.LookPath("rg")
|
|
if err != nil {
|
|
if log.Initialized() {
|
|
slog.Warn("Ripgrep (rg) not found in $PATH. Some grep features might be limited or slower.")
|
|
}
|
|
return ""
|
|
}
|
|
return path
|
|
})
|
|
|
|
func getRgCmd(ctx context.Context, globPattern string) *exec.Cmd {
|
|
name := getRg()
|
|
if name == "" {
|
|
return nil
|
|
}
|
|
args := []string{"--files", "-L", "--null"}
|
|
if globPattern != "" {
|
|
if !filepath.IsAbs(globPattern) && !strings.HasPrefix(globPattern, "/") {
|
|
globPattern = "/" + globPattern
|
|
}
|
|
args = append(args, "--glob", globPattern)
|
|
}
|
|
return exec.CommandContext(ctx, name, args...)
|
|
}
|
|
|
|
func getRgSearchCmd(ctx context.Context, pattern, path, include string) *exec.Cmd {
|
|
name := getRg()
|
|
if name == "" {
|
|
return nil
|
|
}
|
|
// Use -n to show line numbers and include the matched line
|
|
args := []string{"-H", "-n", pattern}
|
|
if include != "" {
|
|
args = append(args, "--glob", include)
|
|
}
|
|
args = append(args, path)
|
|
|
|
return exec.CommandContext(ctx, name, args...)
|
|
}
|