@@ -5,6 +5,7 @@ import (
55 "context"
66 "encoding/json"
77 "fmt"
8+ "io"
89 "os"
910 "os/exec"
1011 "path/filepath"
@@ -377,6 +378,11 @@ func searchFilesWithRegex(pattern, rootPath, include string) ([]grepMatch, error
377378}
378379
379380func fileContainsPattern(filePath string, pattern *regexp.Regexp) (bool, int, string, error) {
381+ // Quick binary file detection
382+ if isBinaryFile(filePath) {
383+ return false, 0, "", nil
384+ }
385+
380386 file, err := os.Open(filePath)
381387 if err != nil {
382388 return false, 0, "", err
@@ -396,6 +402,46 @@ func fileContainsPattern(filePath string, pattern *regexp.Regexp) (bool, int, st
396402 return false, 0, "", scanner.Err()
397403}
398404
405+// isBinaryFile performs a quick check to determine if a file is binary
406+func isBinaryFile(filePath string) bool {
407+ // Check file extension first (fastest)
408+ ext := strings.ToLower(filepath.Ext(filePath))
409+ binaryExts := map[string]bool{
410+ ".exe": true, ".dll": true, ".so": true, ".dylib": true,
411+ ".bin": true, ".obj": true, ".o": true, ".a": true,
412+ ".zip": true, ".tar": true, ".gz": true, ".bz2": true,
413+ ".jpg": true, ".jpeg": true, ".png": true, ".gif": true,
414+ ".pdf": true, ".doc": true, ".docx": true, ".xls": true,
415+ ".mp3": true, ".mp4": true, ".avi": true, ".mov": true,
416+ }
417+ if binaryExts[ext] {
418+ return true
419+ }
420+
421+ // Quick content check for files without clear extensions
422+ file, err := os.Open(filePath)
423+ if err != nil {
424+ return false // If we can't open it, let the caller handle the error
425+ }
426+ defer file.Close()
427+
428+ // Read first 512 bytes to check for null bytes
429+ buffer := make([]byte, 512)
430+ n, err := file.Read(buffer)
431+ if err != nil && err != io.EOF {
432+ return false
433+ }
434+
435+ // Check for null bytes (common in binary files)
436+ for i := 0; i < n; i++ {
437+ if buffer[i] == 0 {
438+ return true
439+ }
440+ }
441+
442+ return false
443+}
444+
399445func globToRegex(glob string) string {
400446 regexPattern := strings.ReplaceAll(glob, ".", "\\.")
401447 regexPattern = strings.ReplaceAll(regexPattern, "*", ".*")