Class: Clacky::Tools::Glob

Inherits:
Base
  • Object
show all
Defined in:
lib/clacky/tools/glob.rb

Constant Summary collapse

MAX_FILE_SIZE =

Maximum file size to search (1MB)

1_048_576

Instance Method Summary collapse

Methods inherited from Base

#category, #description, #name, #parameters, #to_function_definition

Instance Method Details

#execute(pattern:, base_path: ".", limit: 10, working_dir: nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/clacky/tools/glob.rb', line 36

def execute(pattern:, base_path: ".", limit: 10, working_dir: nil)
  # Validate pattern
  if pattern.nil? || pattern.strip.empty?
    return { error: "Pattern cannot be empty" }
  end

  # Expand ~ in pattern only (pattern is relative to base_path, not working_dir)
  pattern = pattern.start_with?("~") ? File.expand_path(pattern) : pattern
  # Expand base_path fully (~ and relative paths resolved against working_dir)
  base_path = expand_path(base_path, working_dir: working_dir)

  # Validate base_path
  unless Dir.exist?(base_path)
    return { error: "Base path does not exist: #{base_path}" }
  end

  begin
    expanded_path = base_path

    skipped = {
      binary: 0,
      too_large: 0,
      ignored: 0
    }

    # Auto-expand bare patterns (no slash, no **) to recursive search.
    effective_pattern = if !File.absolute_path?(pattern) &&
                            !pattern.include?("/") &&
                            !pattern.start_with?("**")
                          "**/#{pattern}"
                        else
                          pattern
                        end

    fnmatch_flags = File::FNM_PATHNAME | File::FNM_DOTMATCH

    matches = []
    Clacky::Utils::FileIgnoreHelper.walk_files(expanded_path, skipped: skipped) do |file|
      relative = file[(expanded_path.length + 1)..]

      unless File.fnmatch(effective_pattern, relative, fnmatch_flags)
        next
      end

      if Clacky::Utils::FileProcessor.binary_file_path?(file) &&
         !Clacky::Utils::FileProcessor.glob_allowed_binary?(file)
        skipped[:binary] += 1
        next
      end

      if File.size(file) > MAX_FILE_SIZE
        skipped[:too_large] += 1
        next
      end

      matches << file
    end

    # Sort by modification time (most recent first)
    matches = matches.sort_by { |path| -File.mtime(path).to_i }

    # Apply limit
    total_matches = matches.length
    matches = matches.take(limit)

    # Convert to absolute paths
    matches = matches.map { |path| File.expand_path(path) }

    {
      matches: matches,
      total_matches: total_matches,
      returned: matches.length,
      truncated: total_matches > limit,
      skipped_files: skipped,
      error: nil
    }
  rescue StandardError => e
    { error: "Failed to glob files: #{e.message}" }
  end
end

#format_call(args) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/clacky/tools/glob.rb', line 117

def format_call(args)
  pattern = args[:pattern] || args['pattern'] || ''
  base_path = args[:base_path] || args['base_path'] || '.'
  
  display_base = base_path == '.' ? '' : " in #{base_path}"
  "glob(\"#{pattern}\"#{display_base})"
end

#format_result(result) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/clacky/tools/glob.rb', line 125

def format_result(result)
  if result[:error]
    "[Error] #{result[:error]}"
  else
    count = result[:returned] || 0
    total = result[:total_matches] || 0
    truncated = result[:truncated] ? " (truncated)" : ""
    
    msg = "[OK] Found #{count}/#{total} files#{truncated}"
    
    # Add skipped files info if present
    if result[:skipped_files]
      skipped = result[:skipped_files]
      skipped_parts = []
      skipped_parts << "#{skipped[:ignored]} ignored" if skipped[:ignored] > 0
      skipped_parts << "#{skipped[:binary]} binary" if skipped[:binary] > 0
      skipped_parts << "#{skipped[:too_large]} too large" if skipped[:too_large] > 0
      
      msg += " (skipped: #{skipped_parts.join(', ')})" unless skipped_parts.empty?
    end
    
    msg
  end
end