Skip to content

Commit e907467

Browse files
committed
Add a case-insensitive file search flag
1 parent f447836 commit e907467

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

Diff for: v2/filesearch.go

+32
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,35 @@ func ExtFileSearch(absCppFilename string, headerExtensions []string, maxTime tim
8383
// Return the result
8484
return foundHeaderAbsPath, nil
8585
}
86+
87+
// FindFile searches for files that contain the given substring in their filename
88+
// starting from the current directory. It returns all matches as absolute paths
89+
// or an error if the search fails.
90+
func FindFile(substring string) ([]string, error) {
91+
var matches []string
92+
currentDir, err := os.Getwd()
93+
if err != nil {
94+
return nil, errors.New("failed to get current directory: " + err.Error())
95+
}
96+
if err := filepath.Walk(currentDir, func(path string, info os.FileInfo, err error) error {
97+
if err != nil {
98+
return nil
99+
}
100+
101+
if info.IsDir() {
102+
return nil
103+
}
104+
105+
if strings.Contains(strings.ToLower(info.Name()), strings.ToLower(substring)) {
106+
absPath, err := filepath.Abs(path)
107+
if err != nil {
108+
return nil
109+
}
110+
matches = append(matches, absPath)
111+
}
112+
return nil
113+
}); err != nil {
114+
return nil, errors.New("no matches when searching for " + substring)
115+
}
116+
return matches, nil
117+
}

Diff for: v2/main.go

+11
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func main() {
7272
pasteFlag bool
7373
quickHelpFlag bool
7474
versionFlag bool
75+
searchAndOpenFlag bool
7576
)
7677

7778
pflag.BoolVarP(&batFlag, "bat", "B", false, "Cat the file with colors instead of editing it, using bat")
@@ -95,6 +96,7 @@ func main() {
9596
pflag.BoolVarP(&quickHelpFlag, "quick-help", "q", false, "always display the quick help when starting")
9697
pflag.BoolVarP(&versionFlag, "version", "v", false, "version information")
9798
pflag.StringVarP(&inputFileWhenRunning, "input-file", "i", "input.txt", "input file when building and running programs")
99+
pflag.BoolVarP(&searchAndOpenFlag, "search", "s", false, "search for a file with the given string and open it")
98100

99101
pflag.Parse()
100102

@@ -309,6 +311,15 @@ func main() {
309311
} else {
310312
fnord.filename, lineNumber, colNumber = FilenameLineColNumber(pflag.Arg(0), pflag.Arg(1), pflag.Arg(2))
311313
}
314+
315+
if searchAndOpenFlag {
316+
substring := fnord.filename
317+
if matches, err := FindFile(substring); err == nil && len(matches) > 0 {
318+
sort.Strings(matches)
319+
fnord.filename = matches[0]
320+
}
321+
}
322+
312323
// Check if the given filename contains something
313324
if fnord.Empty() {
314325
if fnord.filename == "" {

0 commit comments

Comments
 (0)