Spaces:
Sleeping

File size: 1,708 Bytes
5a6158c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

import os
import re

def grep_search(directory, pattern, case_insensitive=True):
    """Searches for a regex pattern in files."""
    results = []
    flags = re.IGNORECASE if case_insensitive else 0
    try:
        for root, _, filenames in os.walk(directory):
            if '.git' in root or '__pycache__' in root or 'node_modules' in root:
                continue
                
            for filename in filenames:
                filepath = os.path.join(root, filename)
                try:
                    with open(filepath, 'r', encoding='utf-8', errors='ignore') as f:
                        lines = f.readlines()
                        for i, line in enumerate(lines):
                            if re.search(pattern, line, flags):
                                results.append(f"{filepath}:{i+1}: {line.strip()}")
                except Exception:
                    continue # Skip binary or unreadable files
                    
        return "\n".join(results[:50]) # Limit results
    except Exception as e:
        return f"Error searching: {e}"

def find_files(directory, pattern):
    """Finds files matching a glob pattern."""
    # Simple implementation using glob not actually needed since we have walker
    # Let's just fuzzy match name
    results = []
    try:
        for root, _, filenames in os.walk(directory):
            if '.git' in root or '__pycache__' in root:
                continue
            for filename in filenames:
                if pattern.lower() in filename.lower():
                    results.append(os.path.join(root, filename))
        return "\n".join(results[:50])
    except Exception as e:
        return f"Error finding files: {e}"