rfgrep 0.5.0

Advanced recursive file grep utility with comprehensive file type classification - search, list, and analyze 153+ file formats with intelligent filtering and safety policies
Documentation
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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
//! Interactive search engine with TUI interface
use crate::cli::InteractiveAlgorithm;
use crate::error::{Result as RfgrepResult, RfgrepError};
use crate::metrics::Metrics;
use crate::search::algorithms::*;
use crate::search::SearchEngine;
use colored::Colorize;
use std::collections::VecDeque;
use std::path::Path;
use std::sync::Arc;

/// Interactive search engine
pub struct InteractiveEngine {
    metrics: Arc<Metrics>,
    search_engine: SearchEngine,
    history: VecDeque<String>,
    max_history: usize,
}

/// Interactive search state
#[derive(Debug, Clone)]
pub struct SearchState {
    pub pattern: String,
    pub files: Vec<std::path::PathBuf>,
    pub matches: Vec<crate::processor::SearchMatch>,
    pub current_file_index: usize,
    pub current_match_index: usize,
    pub filter: Option<String>,
}

impl InteractiveEngine {
    /// Create a new interactive engine
    pub fn new(metrics: Arc<Metrics>) -> RfgrepResult<Self> {
        let search_engine = SearchEngine::new(metrics.clone())?;
        Ok(Self {
            metrics,
            search_engine,
            history: VecDeque::new(),
            max_history: 100,
        })
    }

    /// Run interactive search
    pub async fn run(
        &mut self,
        root_path: &Path,
        initial_pattern: &str,
        algorithm: InteractiveAlgorithm,
        recursive: bool,
        extensions: Option<&[String]>,
    ) -> RfgrepResult<()> {
        println!("{}", "Starting interactive search mode...".green().bold());
        println!("Pattern: {}", initial_pattern.yellow());
        println!("Algorithm: {algorithm:?}");
        println!("{}", "Press 'q' to quit, 'h' for help".dimmed());

        // Discover files
        let files = self
            .discover_files(root_path, recursive, extensions)
            .await?;
        println!("Files to search: {}", files.len());

        // Create search state
        let mut state = SearchState {
            pattern: initial_pattern.to_string(),
            files,
            matches: Vec::new(),
            current_file_index: 0,
            current_match_index: 0,
            filter: None,
        };

        // Perform initial search
        self.perform_search(&mut state, &algorithm).await?;

        // Start interactive loop
        self.interactive_loop(&mut state, &algorithm).await?;

        Ok(())
    }

    /// Discover files to search
    async fn discover_files(
        &self,
        root_path: &Path,
        recursive: bool,
        extensions: Option<&[String]>,
    ) -> RfgrepResult<Vec<std::path::PathBuf>> {
        use crate::walker::walk_dir;

        let files: Vec<_> = walk_dir(root_path, recursive, true)
            .filter(|entry| entry.path().is_file())
            .map(|entry| entry.path().to_path_buf())
            .collect();

        // Apply extension filter
        let filtered_files: Vec<_> = if let Some(extensions) = extensions {
            files
                .into_iter()
                .filter(|path| {
                    if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
                        extensions.iter().any(|e| e.eq_ignore_ascii_case(ext))
                    } else {
                        false
                    }
                })
                .collect()
        } else {
            files
        };

        Ok(filtered_files)
    }

    /// Perform search with current state
    async fn perform_search(
        &self,
        state: &mut SearchState,
        algorithm: &InteractiveAlgorithm,
    ) -> RfgrepResult<()> {
        if state.pattern.is_empty() {
            state.matches.clear();
            return Ok(());
        }

        // Create search algorithm
        let search_algorithm: Box<dyn SearchAlgorithmTrait> = match algorithm {
            InteractiveAlgorithm::BoyerMoore => Box::new(SimdSearch::new(&state.pattern)),
            InteractiveAlgorithm::Regex => Box::new(RegexSearch::new(&state.pattern)?),
            InteractiveAlgorithm::Simple => Box::new(SimdSearch::new(&state.pattern)),
        };

        // Search all files
        let mut all_matches = Vec::new();
        for file in &state.files {
            if let Ok(content) = std::fs::read_to_string(file) {
                let file_matches =
                    search_algorithm.search_with_context(&content, &state.pattern, 2);
                for mut m in file_matches {
                    m.path = file.clone();
                    all_matches.push(m);
                }
            }
        }

        // Apply filter if set
        if let Some(filter) = &state.filter {
            state.matches = all_matches
                .into_iter()
                .filter(|m| m.line.to_lowercase().contains(&filter.to_lowercase()))
                .collect();
        } else {
            state.matches = all_matches;
        }

        // Sort matches
        state.matches.sort();

        // Update metrics
        self.metrics
            .matches_found
            .inc_by(state.matches.len() as u64);

        Ok(())
    }

    /// Main interactive loop
    async fn interactive_loop(
        &mut self,
        state: &mut SearchState,
        algorithm: &InteractiveAlgorithm,
    ) -> RfgrepResult<()> {
        use std::io::{self, Write};

        loop {
            // Display current state
            self.display_state(state);

            // Get user input
            print!("\n> ");
            io::stdout().flush()?;

            let mut input = String::new();
            io::stdin().read_line(&mut input)?;
            let input = input.trim();

            if input.is_empty() {
                continue;
            }

            // Add to history
            self.add_to_history(input.to_string());

            // Process command
            match self.process_command(input, state, algorithm).await? {
                CommandResult::Continue => continue,
                CommandResult::Quit => break,
                CommandResult::Search => {
                    self.perform_search(state, algorithm).await?;
                }
            }
        }

        Ok(())
    }

    /// Display current search state
    fn display_state(&self, state: &SearchState) {
        println!("\n{}", "=".repeat(60).cyan());
        println!("Pattern: {}", state.pattern.yellow());
        println!("Files: {}", state.files.len());
        println!("Matches: {}", state.matches.len());

        if let Some(filter) = &state.filter {
            println!("Filter: {}", filter.blue());
        }

        if !state.matches.is_empty() {
            println!("\n{}", "Recent matches:".green().bold());
            let start = state.current_match_index.saturating_sub(5);
            let end = (state.current_match_index + 5).min(state.matches.len());

            for (i, m) in state.matches[start..end].iter().enumerate() {
                let actual_index = start + i;
                let marker = if actual_index == state.current_match_index {
                    "→"
                } else {
                    " "
                };
                println!(
                    "{} {}:{}:{}: {}",
                    marker.yellow(),
                    m.path.display(),
                    m.line_number,
                    m.column_start + 1,
                    m.line
                );
            }
        }
    }

    /// Process user command
    async fn process_command(
        &self,
        input: &str,
        state: &mut SearchState,
        _algorithm: &InteractiveAlgorithm,
    ) -> RfgrepResult<CommandResult> {
        let parts: Vec<&str> = input.split_whitespace().collect();
        if parts.is_empty() {
            return Ok(CommandResult::Continue);
        }

        match parts[0] {
            "q" | "quit" | "exit" => Ok(CommandResult::Quit),
            "h" | "help" => {
                self.show_help();
                Ok(CommandResult::Continue)
            }
            "s" | "search" => {
                if parts.len() > 1 {
                    state.pattern = parts[1..].join(" ");
                    Ok(CommandResult::Search)
                } else {
                    println!("Usage: search <pattern>");
                    Ok(CommandResult::Continue)
                }
            }
            "f" | "filter" => {
                if parts.len() > 1 {
                    state.filter = Some(parts[1..].join(" "));
                    Ok(CommandResult::Search)
                } else {
                    state.filter = None;
                    Ok(CommandResult::Search)
                }
            }
            "c" | "clear" => {
                state.filter = None;
                state.pattern.clear();
                Ok(CommandResult::Search)
            }
            "n" | "next" => {
                if state.current_match_index < state.matches.len().saturating_sub(1) {
                    state.current_match_index += 1;
                }
                Ok(CommandResult::Continue)
            }
            "p" | "prev" | "previous" => {
                if state.current_match_index > 0 {
                    state.current_match_index -= 1;
                }
                Ok(CommandResult::Continue)
            }
            "g" | "goto" => {
                if parts.len() > 1 {
                    if let Ok(index) = parts[1].parse::<usize>() {
                        if index < state.matches.len() {
                            state.current_match_index = index;
                        }
                    }
                }
                Ok(CommandResult::Continue)
            }
            "o" | "open" => {
                if !state.matches.is_empty() {
                    let current_match = &state.matches[state.current_match_index];
                    self.open_file(current_match)?;
                }
                Ok(CommandResult::Continue)
            }
            "save" => {
                if parts.len() > 1 {
                    self.save_results(&state.matches, parts[1])?;
                } else {
                    self.save_results(&state.matches, "results.txt")?;
                }
                Ok(CommandResult::Continue)
            }
            "stats" => {
                self.show_statistics(state);
                Ok(CommandResult::Continue)
            }
            "history" => {
                self.show_history();
                Ok(CommandResult::Continue)
            }
            _ => {
                // Treat as search pattern
                state.pattern = input.to_string();
                Ok(CommandResult::Search)
            }
        }
    }

    /// Show help information
    fn show_help(&self) {
        println!("\n{}", "Available commands:".green().bold());
        println!("  {} - Search for pattern", "search <pattern>".yellow());
        println!("  {} - Filter current results", "filter <text>".yellow());
        println!("  {} - Clear filters", "clear".yellow());
        println!("  {} - Next match", "next".yellow());
        println!("  {} - Previous match", "prev".yellow());
        println!("  {} - Go to match number", "goto <number>".yellow());
        println!("  {} - Open current match in editor", "open".yellow());
        println!("  {} - Save results to file", "save [filename]".yellow());
        println!("  {} - Show statistics", "stats".yellow());
        println!("  {} - Show command history", "history".yellow());
        println!("  {} - Show this help", "help".yellow());
        println!("  {} - Quit", "quit".yellow());
    }

    /// Open file in editor
    fn open_file(&self, match_: &crate::processor::SearchMatch) -> RfgrepResult<()> {
        let editor = std::env::var("EDITOR").unwrap_or_else(|_| "nano".to_string());
        let status = std::process::Command::new(&editor)
            .arg(&match_.path)
            .status()?;

        if !status.success() {
            eprintln!("Failed to open file in editor: {}", editor);
        }

        Ok(())
    }

    /// Save results to file
    fn save_results(
        &self,
        matches: &[crate::processor::SearchMatch],
        filename: &str,
    ) -> RfgrepResult<()> {
        let mut content = String::new();
        for m in matches {
            content.push_str(&format!(
                "{}:{}:{}: {}\n",
                m.path.display(),
                m.line_number,
                m.column_start + 1,
                m.line
            ));
        }

        std::fs::write(filename, content)?;
        println!("Results saved to {}", filename.green());

        Ok(())
    }

    /// Show search statistics
    fn show_statistics(&self, state: &SearchState) {
        println!("\n{}", "Search Statistics:".green().bold());
        println!("Pattern: {}", state.pattern.yellow());
        println!("Files searched: {}", state.files.len());
        println!("Total matches: {}", state.matches.len());

        if !state.matches.is_empty() {
            let files_with_matches: std::collections::HashSet<_> =
                state.matches.iter().map(|m| &m.path).collect();
            println!("Files with matches: {}", files_with_matches.len());

            // Extension statistics
            let mut ext_counts = std::collections::HashMap::new();
            for m in &state.matches {
                if let Some(ext) = m.path.extension().and_then(|e| e.to_str()) {
                    *ext_counts.entry(ext).or_insert(0) += 1;
                }
            }

            let mut ext_vec: Vec<_> = ext_counts.into_iter().collect();
            ext_vec.sort_by(|a, b| b.1.cmp(&a.1));

            if !ext_vec.is_empty() {
                println!("\nMatches by file type:");
                for (ext, count) in ext_vec.iter().take(5) {
                    println!("  .{}: {}", ext, count);
                }
            }
        }
    }

    /// Add command to history
    fn add_to_history(&mut self, command: String) {
        if self.history.len() >= self.max_history {
            self.history.pop_front();
        }
        self.history.push_back(command);
    }

    /// Show command history
    fn show_history(&self) {
        println!("\n{}", "Command History:".green().bold());
        for (i, cmd) in self.history.iter().enumerate() {
            println!("  {}: {}", i + 1, cmd);
        }
    }
}

/// Command processing result
#[derive(Debug)]
enum CommandResult {
    Continue,
    Quit,
    Search,
}

/// Regex search implementation for interactive mode
pub struct RegexSearch {
    pattern: String,
    regex: regex::Regex,
}

impl RegexSearch {
    pub fn new(pattern: &str) -> RfgrepResult<Self> {
        let regex = regex::Regex::new(pattern)
            .map_err(|e| RfgrepError::Other(format!("Invalid regex: {e}")))?;
        Ok(Self {
            pattern: pattern.to_string(),
            regex,
        })
    }
}

impl SearchAlgorithmTrait for RegexSearch {
    fn search(&self, text: &str, _pattern: &str) -> Vec<usize> {
        self.regex.find_iter(text).map(|m| m.start()).collect()
    }

    fn search_with_context(
        &self,
        text: &str,
        _pattern: &str,
        context_lines: usize,
    ) -> Vec<crate::processor::SearchMatch> {
        let matches = self.search(text, _pattern);
        let lines: Vec<&str> = text.lines().collect();
        let mut results = Vec::new();

        for &match_pos in &matches {
            let pre_lines = text[..match_pos].lines().count();
            let line_number = pre_lines.max(1);
            let line_index = line_number - 1;

            if line_index < lines.len() {
                let line = lines[line_index];
                let context_before = self.get_context_before(&lines, line_index, context_lines);
                let context_after = self.get_context_after(&lines, line_index, context_lines);

                let matched_text = self
                    .regex
                    .find(&text[match_pos..])
                    .map(|m| m.as_str().to_string())
                    .unwrap_or_default();

                results.push(crate::processor::SearchMatch {
                    path: std::path::PathBuf::new(),
                    line_number,
                    line: line.to_string(),
                    context_before,
                    context_after,
                    matched_text: matched_text.clone(),
                    column_start: match_pos - text[..match_pos].rfind('\n').unwrap_or(0),
                    column_end: match_pos - text[..match_pos].rfind('\n').unwrap_or(0)
                        + matched_text.len(),
                });
            }
        }

        results
    }

    fn name(&self) -> &'static str {
        "Regex"
    }
}

impl RegexSearch {
    fn get_context_before(
        &self,
        lines: &[&str],
        current_line: usize,
        context_lines: usize,
    ) -> Vec<(usize, String)> {
        let start = current_line.saturating_sub(context_lines);
        (start..current_line)
            .map(|i| (i + 1, lines[i].to_string()))
            .collect()
    }

    fn get_context_after(
        &self,
        lines: &[&str],
        current_line: usize,
        context_lines: usize,
    ) -> Vec<(usize, String)> {
        let end = (current_line + context_lines + 1).min(lines.len());
        ((current_line + 1)..end)
            .map(|i| (i + 1, lines[i].to_string()))
            .collect()
    }
}