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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
use clap::{Parser, Subcommand, ValueEnum};
use clap_complete::Shell;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::path::PathBuf;

#[derive(Parser)]
#[clap(
    name = "rfgrep",
    author = "Khalid Hussein <kh3rld.hussein@gmail.com>",
    version,
    about = "Recursive file grep utility with advanced filtering - search, list, and analyze text files with regex support",
    long_about = r#"
rfgrep - A powerful command-line utility for recursively searching and listing files with advanced filtering capabilities.

FEATURES:
  • Advanced Search: Regex, plain text, and whole-word matching
  • File Listing: Detailed/simple output formats with extension statistics
  • Performance: Parallel processing with memory mapping for large files
  • Filtering: Extension, size, and binary file filtering
  • Utilities: Clipboard copy, dry-run mode, and progress indicators

EXAMPLES:
  # Search for "HashMap" in Rust files
  rfgrep search "HashMap" --extensions rs

  # List all Markdown files under 1MB
  rfgrep list --extensions md --max-size 1

  # Search with regex and copy to clipboard
  rfgrep search "fn\s+\w+\s*\(" regex --copy

  # Recursive search with word boundaries
  rfgrep search "test" word --recursive --extensions rs

PERFORMANCE TIPS:
  • Use --skip-binary to avoid unnecessary file checks
  • Limit scope with --extensions and --max-size
  • Use --dry-run first to preview files
  • Enable --recursive for deep directory traversal
"#,
    after_help = r#"
For more information, visit: https://github.com/rfgrep/rfgrep
"#
)]
pub struct Cli {
    #[clap(default_value = ".")]
    pub path: PathBuf,

    #[clap(long, value_parser, default_value_t = false, global = true)]
    pub verbose: bool,

    /// Suppress all non-essential output (useful for piping)
    #[clap(
        long,
        short = 'q',
        value_parser,
        default_value_t = false,
        global = true
    )]
    pub quiet: bool,

    #[clap(long, value_enum, default_value_t = ColorChoice::Auto, global = true)]
    pub color: ColorChoice,

    #[clap(long, value_parser, global = true)]
    pub log: Option<PathBuf>,

    #[clap(long, value_parser, default_value_t = false, global = true)]
    pub dry_run: bool,

    /// Allow running as root (disabled by default for safety)
    #[clap(long, value_parser, default_value_t = false, global = true)]
    pub allow_root: bool,

    #[clap(long, value_parser, global = true)]
    pub max_size: Option<usize>,

    #[clap(long, value_parser, default_value_t = false, global = true)]
    pub skip_binary: bool,

    /// Safety policy for file processing
    #[clap(long, value_enum, default_value_t = SafetyPolicy::Default, global = true)]
    pub safety_policy: SafetyPolicy,

    /// Number of threads for parallel file processing
    #[clap(long, value_parser, global = true)]
    pub threads: Option<usize>,

    #[clap(subcommand)]
    pub command: Commands,
}

#[derive(Copy, Clone, PartialEq, Eq, ValueEnum, Debug)]
pub enum ColorChoice {
    Auto,
    Always,
    Never,
}

#[derive(Subcommand, Debug)]
pub enum Commands {
    /// Run simulations and performance benchmarks to evaluate the current implementation
    #[clap(after_help = r#"
SIMULATIONS:
  Run built-in benchmark scenarios over a test corpus to evaluate performance and limitations.

EXAMPLES:
  # Run simulations in the current directory (uses bench_data if present)
  rfgrep simulate

  # Run simulations from a specific path
  rfgrep simulate --path .
"#)]
    Simulate {},

    /// Configuration management
    Config {
        #[clap(subcommand)]
        action: crate::cli_config::ConfigAction,
    },

    #[clap(after_help = r#"
SEARCH MODES:
  text    - Plain text search (default)
  word    - Whole word matching with boundaries
  regex   - Regular expression search

EXAMPLES:
  # Basic text search
  rfgrep search "error" --extensions rs

  # Word boundary search
  rfgrep search "test" word --recursive

  # Regex search for function definitions
  rfgrep search "fn\s+\w+\s*\(" regex --extensions rs

  # Search and copy results to clipboard
  rfgrep search "TODO" --copy --extensions rs,md

  # Pipe input from another command
  cat file.log | rfgrep search "error"

  # Chain with other tools
  cat .zsh_history | rfgrep search "git" -c

  # Use with command substitution
  echo "test data" | rfgrep search "test"

PERFORMANCE TIPS:
  • Use --skip-binary for faster processing
  • Limit file size with --max-size
  • Use --dry-run to preview files first
  • Pipe data directly for faster processing
"#)]
    Search {
        pattern: String,

        #[clap(long, value_enum, default_value_t = SearchMode::Text)]
        mode: SearchMode,

        #[clap(long, value_parser, default_value_t = false)]
        copy: bool,

        #[clap(long, value_enum, default_value_t = OutputFormat::Text)]
        output_format: OutputFormat,

        /// Emit newline-delimited JSON (one JSON object per match)
        #[clap(long, value_parser, default_value_t = false)]
        ndjson: bool,

        #[clap(long, value_parser, use_value_delimiter = true)]
        extensions: Option<Vec<String>>,

        /// File type handling strategy
        #[clap(long, value_enum, default_value_t = FileTypeStrategy::Default)]
        file_types: FileTypeStrategy,

        /// Include specific file types (overrides default strategy)
        #[clap(long, value_parser, use_value_delimiter = true)]
        include_extensions: Option<Vec<String>>,

        /// Exclude specific file types (overrides default strategy)
        #[clap(long, value_parser, use_value_delimiter = true)]
        exclude_extensions: Option<Vec<String>>,

        /// Search all file types (comprehensive mode)
        #[clap(long, value_parser, default_value_t = false)]
        search_all_files: bool,

        /// Only search text files (conservative mode)
        #[clap(long, value_parser, default_value_t = false)]
        text_only: bool,

        #[clap(short, long, value_parser, default_value_t = false)]
        recursive: bool,

        #[clap(long, value_parser, default_value_t = 0)]
        context_lines: usize,

        #[clap(long, value_parser, default_value_t = false)]
        case_sensitive: bool,

        #[clap(long, value_parser, default_value_t = false)]
        invert_match: bool,

        /// Per-file timeout in seconds (abort scanning a file after this many seconds)
        #[clap(long, value_parser)]
        timeout_per_file: Option<u64>,

        #[clap(long, value_parser)]
        max_matches: Option<usize>,

        #[clap(long, value_enum, default_value_t = SearchAlgorithm::BoyerMoore)]
        algorithm: SearchAlgorithm,

        /// Only show count of matches, not the matches themselves
        #[clap(long, short = 'c', value_parser, default_value_t = false)]
        count: bool,

        /// Only show filenames with matches, not the matches themselves
        #[clap(long, short = 'l', value_parser, default_value_t = false)]
        files_with_matches: bool,

        #[clap(value_parser, last = true)]
        path: Option<PathBuf>,

        /// Alternative explicit path flag (useful for scripts)
        #[clap(long, value_parser, alias = "path-flag")]
        path_flag: Option<PathBuf>,
    },

    #[clap(after_help = r#"
INTERACTIVE FEATURES:
  • Real-time search with live filtering
  • Keyboard navigation and commands
  • Result highlighting and selection
  • Save results to file
  • Memory-optimized processing

COMMANDS:
  n/new   - Start a new search
  f/filter - Filter current results
  c/clear - Clear all filters
  s/save  - Save results to file
  q/quit  - Exit interactive mode

EXAMPLES:
  # Start interactive search
  rfgrep interactive "error" --extensions rs

  # Interactive search with specific algorithm
  rfgrep interactive "test" --algorithm boyer-moore --recursive
"#)]
    Interactive {
        pattern: String,

        #[clap(long, value_enum, default_value_t = InteractiveAlgorithm::BoyerMoore)]
        algorithm: InteractiveAlgorithm,

        #[clap(long, value_parser, use_value_delimiter = true)]
        extensions: Option<Vec<String>>,

        #[clap(short, long, value_parser, default_value_t = false)]
        recursive: bool,

        #[clap(value_parser, last = true)]
        path: Option<PathBuf>,

        /// Alternative explicit path flag (useful for scripts)
        #[clap(long, value_parser, alias = "path-flag")]
        path_flag: Option<PathBuf>,
    },
    #[clap(after_help = r#"
OUTPUT FORMATS:
  Simple  - Just file paths (default)
  Long    - Detailed table with size, type, and binary info

EXAMPLES:
  # Simple file listing
  rfgrep list --extensions rs

  # Detailed listing with size info
  rfgrep list --long --extensions rs,md

  # Recursive listing with hidden files
  rfgrep list --recursive --show-hidden --extensions rs

  # List files under 1MB
  rfgrep list --max-size 1 --extensions rs

FEATURES:
  • Extension statistics and file counts
  • Binary file detection
  • Size filtering and formatting
  • Hidden file handling
  • Recursive directory traversal
"#)]
    List {
        #[clap(long, value_parser, use_value_delimiter = true)]
        extensions: Option<Vec<String>>,

        #[clap(short, long, value_parser, default_value_t = false)]
        long: bool,

        #[clap(short, long, value_parser, default_value_t = false)]
        recursive: bool,

        #[clap(long, value_parser, default_value_t = false)]
        show_hidden: bool,

        #[clap(long, value_parser)]
        max_size: Option<usize>,

        #[clap(long, value_parser)]
        min_size: Option<usize>,

        #[clap(long, value_parser, default_value_t = false)]
        detailed: bool,

        #[clap(long, value_parser, default_value_t = false)]
        simple: bool,

        #[clap(long, value_parser, default_value_t = false)]
        stats: bool,

        #[clap(long, value_enum, default_value_t = SortCriteria::Name)]
        sort: SortCriteria,

        #[clap(long, value_parser, default_value_t = false)]
        reverse: bool,

        #[clap(long, value_parser)]
        limit: Option<usize>,

        #[clap(long, value_parser, default_value_t = false)]
        copy: bool,

        #[clap(long, value_enum, default_value_t = OutputFormat::Text)]
        output_format: OutputFormat,

        // Optional trailing path allowing `rfgrep list <options> <path>`
        #[clap(value_parser, last = true)]
        path: Option<PathBuf>,

        /// Alternative explicit path flag (useful for scripts)
        #[clap(long, value_parser, alias = "path-flag")]
        path_flag: Option<PathBuf>,
    },
    #[clap(after_help = r#"
SUPPORTED SHELLS:
  bash     - Bash shell completions
  zsh      - Zsh shell completions
  fish     - Fish shell completions
  powershell - PowerShell completions
  elvish   - Elvish shell completions

EXAMPLES:
  # Generate bash completions
  rfgrep completions bash > ~/.local/share/bash-completion/completions/rfgrep

  # Generate zsh completions
  rfgrep completions zsh > ~/.zsh/completions/_rfgrep

  # Generate fish completions
  rfgrep completions fish > ~/.config/fish/completions/rfgrep.fish

SETUP:
  Add the generated completion script to your shell's completion directory
  and restart your shell or source the completion file.
"#)]
    Completions {
        #[clap(value_enum)]
        shell: Shell,
    },
    #[clap(after_help = r#"
PLUGIN MANAGEMENT:
  list     - List all available plugins
  stats    - Show plugin statistics
  info     - Show detailed plugin information
  enable   - Enable a plugin
  disable  - Disable a plugin
  priority - Set plugin priority
  config   - Show plugin configuration options
  test     - Test plugin with specific file

EXAMPLES:
  # List all plugins
  rfgrep plugins list

  # Show plugin statistics
  rfgrep plugins stats

  # Get info about text plugin
  rfgrep plugins info enhanced_text

  # Enable binary plugin
  rfgrep plugins enable enhanced_binary

  # Test text plugin on a file
  rfgrep plugins test enhanced_text README.md "example"
"#)]
    Plugins {
        #[clap(subcommand)]
        command: PluginCommands,
    },
    /// Interactive TUI mode
    #[clap(after_help = r#"
INTERACTIVE TUI MODE:
  Launch an interactive terminal user interface for searching files.

EXAMPLES:
  # Start TUI with a pattern
  rfgrep tui "search pattern"

  # Start TUI and enter pattern interactively
  rfgrep tui

  # Start TUI with specific algorithm
  rfgrep tui "pattern" --algorithm boyer-moore

  # Start TUI with case-sensitive search
  rfgrep tui "pattern" --case-sensitive

CONTROLS:
  h         - Toggle help
  q         - Quit
  ↑/↓, j/k  - Navigate matches
  ←/→, h/l  - Navigate files
  n/N       - Next/Previous match
  c         - Toggle case sensitivity
  m         - Cycle search mode
  a         - Cycle algorithm
  r         - Refresh search
  Enter     - Open file in editor
"#)]
    Tui {
        /// Search pattern
        pattern: Option<String>,
        /// Search algorithm to use
        #[clap(long, value_enum, default_value = "boyer-moore")]
        algorithm: SearchAlgorithm,
        /// Enable case-sensitive search
        #[clap(long)]
        case_sensitive: bool,
        /// Search mode
        #[clap(long, value_enum, default_value = "text")]
        mode: SearchMode,
        /// Number of context lines to show
        #[clap(long, default_value = "0")]
        context_lines: usize,
        /// Search path
        #[clap(long, default_value = ".")]
        path: String,
    },
    #[clap(hide = true)]
    Worker {
        path: std::path::PathBuf,
        pattern: String,
    },
}

#[derive(Subcommand, Debug)]
pub enum PluginCommands {
    /// List all available plugins
    List,
    /// Show plugin statistics
    Stats,
    /// Show detailed plugin information
    Info {
        /// Plugin name
        name: String,
    },
    /// Enable a plugin
    Enable {
        /// Plugin name
        name: String,
    },
    /// Disable a plugin
    Disable {
        /// Plugin name
        name: String,
    },
    /// Set plugin priority
    Priority {
        /// Plugin name
        name: String,
        /// Priority value (lower = higher priority)
        priority: u32,
    },
    /// Show plugin configuration options
    Config {
        /// Plugin name
        name: String,
    },
    /// Test plugin with specific file
    Test {
        /// Plugin name
        name: String,
        /// File path to test
        file: String,
        /// Search pattern
        pattern: String,
    },
}

#[derive(ValueEnum, Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum SearchMode {
    #[default]
    Text,
    Word,
    Regex,
}

#[derive(ValueEnum, Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum FileTypeStrategy {
    #[default]
    /// Default behavior - smart classification (recommended)
    Default,
    /// Search everything possible (comprehensive)
    Comprehensive,
    /// Only search safe text files (conservative)
    Conservative,
    /// Performance-first - skip potentially problematic files
    Performance,
}

#[derive(ValueEnum, Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum SafetyPolicy {
    #[default]
    /// Default safety policy - balanced approach
    Default,
    /// Conservative safety - strict file type checking and size limits
    Conservative,
    /// Performance mode - relaxed safety for speed
    Performance,
}

#[derive(ValueEnum, Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum InteractiveAlgorithm {
    #[default]
    BoyerMoore,
    Regex,
    Simple,
}

#[derive(ValueEnum, Clone, Debug)]
pub enum SearchAlgorithm {
    BoyerMoore,
    Regex,
    Simple,
}

#[derive(ValueEnum, Clone, Debug)]
pub enum SortCriteria {
    Name,
    Size,
    Date,
    Type,
    Path,
}

#[derive(ValueEnum, Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum OutputFormat {
    #[default]
    Text,
    Json,
    Xml,
    Html,
    Markdown,
    Csv,
    Tsv,
}

impl fmt::Display for SearchMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SearchMode::Text => write!(f, "text"),
            SearchMode::Word => write!(f, "word"),
            SearchMode::Regex => write!(f, "regex"),
        }
    }
}