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
use crate::cli::SearchMode;
use crate::error::{Result as RfgrepResult, RfgrepError};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use std::env;
use std::fs;
use std::path::{Path, PathBuf};

/// Configuration precedence (highest to lowest):
/// 1. Command-line arguments
/// 2. Environment variables (RFGREP_*)
/// 3. Project-level config (.rfgreprc in project root)
/// 4. User-level config (~/.config/rfgrep/config.toml)
/// 5. System-level config (/etc/rfgrep/config.toml)
/// 6. Built-in defaults

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(default)]
pub struct Config {
    pub search: SearchConfig,
    pub output: OutputConfig,
    pub filters: FilterConfig,
    pub performance: PerformanceConfig,
    pub git: GitConfig,
    pub compression: CompressionConfig,
    pub logging: LoggingConfig,
    pub type_definitions: TypeDefinitions,
    pub shortcuts: HashMap<String, SearchShortcut>,
    pub ui: UIConfig,
    pub experimental: ExperimentalConfig,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct SearchConfig {
    pub mode: SearchMode,
    pub case_sensitive: bool,
    pub smart_case: bool,
    pub max_file_size_mb: u64,
    pub skip_binary: bool,
    pub context_before: usize,
    pub context_after: usize,
    pub threads: usize,
    pub chunk_size: usize,
    pub algorithms: AlgorithmConfig,
}

impl Default for SearchConfig {
    fn default() -> Self {
        Self {
            mode: SearchMode::Regex,
            case_sensitive: false,
            smart_case: true,
            max_file_size_mb: 100, // 10MB
            skip_binary: true,
            context_before: 2,
            context_after: 2,
            threads: 0, // 0 = auto
            chunk_size: 100,
            algorithms: AlgorithmConfig::default(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct AlgorithmConfig {
    pub simple: String,
    pub regex: String,
    pub multi_pattern: String,
}

impl Default for AlgorithmConfig {
    fn default() -> Self {
        Self {
            simple: "boyer-moore".to_string(),
            regex: "regex-automaton".to_string(),
            multi_pattern: "aho-corasick".to_string(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct OutputConfig {
    pub format: OutputFormat,
    pub color: ColorMode,
    pub line_numbers: bool,
    pub show_filenames: bool,
    pub show_columns: bool,
    pub syntax_highlighting: bool,
    pub colors: ColorScheme,
}

impl Default for OutputConfig {
    fn default() -> Self {
        Self {
            format: OutputFormat::Text,
            color: ColorMode::Auto,
            line_numbers: true,
            show_filenames: true,
            show_columns: false,
            syntax_highlighting: true,
            colors: ColorScheme::default(),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum OutputFormat {
    Text,
    Json,
    Csv,
    Ndjson,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ColorMode {
    Auto,
    Always,
    Never,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ColorScheme {
    pub r#match: String,
    pub line_number: String,
    pub filename: String,
    pub separator: String,
}

impl Default for ColorScheme {
    fn default() -> Self {
        Self {
            r#match: "red".to_string(),
            line_number: "green".to_string(),
            filename: "blue".to_string(),
            separator: "cyan".to_string(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct FilterConfig {
    pub extensions: Vec<String>,
    pub exclude_extensions: Vec<String>,
    pub exclude_patterns: Vec<String>,
    pub ignore_directories: Vec<String>,
    pub ignore_files: Vec<String>,
    pub size: SizeFilter,
}

impl Default for FilterConfig {
    fn default() -> Self {
        Self {
            extensions: Vec::new(),
            exclude_extensions: vec![
                "o".to_string(),
                "so".to_string(),
                "dylib".to_string(),
                "exe".to_string(),
                "dll".to_string(),
                "class".to_string(),
                "pyc".to_string(),
            ],
            exclude_patterns: Vec::new(),
            ignore_directories: vec![
                "node_modules".to_string(),
                ".git".to_string(),
                "target".to_string(),
                "build".to_string(),
                "dist".to_string(),
                ".next".to_string(),
                "__pycache__".to_string(),
                ".cache".to_string(),
            ],
            ignore_files: vec![
                ".gitignore".to_string(),
                ".ignore".to_string(),
                ".rfgrepignore".to_string(),
            ],
            size: SizeFilter::default(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SizeFilter {
    pub min_size: u64,
    pub max_size: u64,
}

impl Default for SizeFilter {
    fn default() -> Self {
        Self {
            min_size: 0,
            max_size: 100 * 1024 * 1024, // 100MB
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct PerformanceConfig {
    pub mmap_threshold_mb: u64,
    pub max_memory_usage_mb: u64,
    pub chunk_size_multiplier: f64,
    pub adaptive_memory: bool,
    pub simd: String,
    pub parallel: bool,
    pub buffer_size: usize,
    pub regex_cache_size: usize,
    pub optimization: OptimizationConfig,
}

impl Default for PerformanceConfig {
    fn default() -> Self {
        Self {
            mmap_threshold_mb: 16,
            max_memory_usage_mb: 512,
            chunk_size_multiplier: 1.0,
            adaptive_memory: true,
            simd: "auto".to_string(),
            parallel: true,
            buffer_size: 65536,
            regex_cache_size: 100,
            optimization: OptimizationConfig::default(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct OptimizationConfig {
    pub literal_extraction: bool,
    pub pre_filter: bool,
    pub auto_dfa: bool,
}

impl Default for OptimizationConfig {
    fn default() -> Self {
        Self {
            literal_extraction: true,
            pre_filter: true,
            auto_dfa: true,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct GitConfig {
    pub respect_gitignore: bool,
    pub respect_global_gitignore: bool,
    pub respect_git_exclude: bool,
    pub search_dot_git: bool,
    pub submodules: GitSubmoduleConfig,
}

impl Default for GitConfig {
    fn default() -> Self {
        Self {
            respect_gitignore: true,
            respect_global_gitignore: true,
            respect_git_exclude: true,
            search_dot_git: false,
            submodules: GitSubmoduleConfig::default(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(default)]
pub struct GitSubmoduleConfig {
    pub follow: bool,
    pub search: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct CompressionConfig {
    pub enabled: bool,
    pub formats: Vec<String>,
    pub max_decompressed_size_mb: u64,
    pub cache_decompressed: bool,
}

impl Default for CompressionConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            formats: vec![
                "gzip".to_string(),
                "bzip2".to_string(),
                "xz".to_string(),
                "zstd".to_string(),
                "lz4".to_string(),
            ],
            max_decompressed_size_mb: 100,
            cache_decompressed: true,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct LoggingConfig {
    pub level: String,
    pub file: String,
    pub format: String,
}

impl Default for LoggingConfig {
    fn default() -> Self {
        Self {
            level: "info".to_string(),
            file: "".to_string(),
            format: "text".to_string(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(default)]
pub struct TypeDefinitions {
    pub rust: Vec<String>,
    pub python: Vec<String>,
    pub javascript: Vec<String>,
    pub typescript: Vec<String>,
    pub web: Vec<String>,
    pub config: Vec<String>,
    pub markdown: Vec<String>,
    pub sql: Vec<String>,
    #[serde(flatten)]
    pub custom: HashMap<String, Vec<String>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchShortcut {
    pub pattern: String,
    pub mode: SearchMode,
    #[serde(default)]
    pub extensions: Vec<String>,
    #[serde(default)]
    pub exclude_extensions: Vec<String>,
    #[serde(default)]
    pub case_sensitive: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct UIConfig {
    pub interactive: bool,
    pub show_progress: bool,
    pub pager: String,
    pub pager_command: String,
}

impl Default for UIConfig {
    fn default() -> Self {
        Self {
            interactive: true,
            show_progress: true,
            pager: "auto".to_string(),
            pager_command: "".to_string(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(default)]
pub struct ExperimentalConfig {
    pub semantic_search: bool,
    pub ai_suggestions: bool,
}

impl Config {
    /// Merge another config into this one, with other taking precedence
    pub fn merge(&mut self, other: &Config) {
        self.search = other.search.clone();
        self.output = other.output.clone();
        self.performance = other.performance.clone();

        if !other.filters.extensions.is_empty() {
            self.filters.extensions = other.filters.extensions.clone();
        }
    }

    /// Load from TOML file
    pub fn from_toml_file(path: &Path) -> RfgrepResult<Self> {
        let content = fs::read_to_string(path).map_err(|e| {
            RfgrepError::Other(format!("Failed to read config file {:?}: {}", path, e))
        })?;

        toml::from_str(&content).map_err(|e| {
            RfgrepError::Other(format!("Failed to parse TOML config {:?}: {}", path, e))
        })
    }

    /// Auto-detect format and load
    pub fn load_from_path(path: &Path) -> RfgrepResult<Self> {
        // Only TOML supported for now
        Self::from_toml_file(path)
    }

    /// Auto-detect format and load default config
    pub fn load() -> RfgrepResult<Self> {
        let config_path =
            Self::find_config_path().map_err(|e| RfgrepError::Other(e.to_string()))?;
        if let Some(path) = config_path {
            Self::from_toml_file(&path)
        } else {
            Ok(Self::default())
        }
    }

    fn find_config_path() -> RfgrepResult<Option<PathBuf>> {
        if let Some(xdg_config) = dirs::config_dir() {
            let xdg_path = xdg_config.join("rfgrep/config.toml");
            if xdg_path.exists() {
                return Ok(Some(xdg_path));
            }
        }

        if let Some(home) = dirs::home_dir() {
            let home_path = home.join(".rfgrep.toml");
            if home_path.exists() {
                return Ok(Some(home_path));
            }
        }

        let current_path = Path::new(".rfgrep.toml");
        if current_path.exists() {
            return Ok(Some(current_path.to_path_buf()));
        }

        Ok(None)
    }

    pub fn validate(&self) -> RfgrepResult<()> {
        if self.search.threads > 1024 {
            return Err(RfgrepError::Other(format!(
                "Thread count too high: {}",
                self.search.threads
            )));
        }
        Ok(())
    }
}

pub struct ConfigManager {
    pub system_config: Option<Config>,
    pub user_config: Option<Config>,
    pub project_config: Option<Config>,
    pub env_config: Config,
    pub merged_config: Config,
}

impl ConfigManager {
    pub fn new() -> RfgrepResult<Self> {
        let mut manager = Self {
            system_config: Self::load_system_config()?,
            user_config: Self::load_user_config()?,
            project_config: None, // Loaded later if needed
            env_config: Self::load_env_config()?,
            merged_config: Config::default(),
        };

        manager.merge_configs()?;
        Ok(manager)
    }

    pub fn load_for_directory(&mut self, dir: &Path) -> RfgrepResult<()> {
        if let Some(project_root) = Self::find_project_root(dir)? {
            self.project_config = Self::load_project_config(&project_root)?;
            self.merge_configs()?;
        }
        Ok(())
    }

    fn merge_configs(&mut self) -> RfgrepResult<()> {
        let mut merged = Config::default();

        if let Some(system) = &self.system_config {
            merged.merge(system);
        }
        if let Some(user) = &self.user_config {
            merged.merge(user);
        }
        if let Some(project) = &self.project_config {
            merged.merge(project);
        }
        merged.merge(&self.env_config);

        self.merged_config = merged;
        Ok(())
    }

    fn load_system_config() -> RfgrepResult<Option<Config>> {
        let path = PathBuf::from("/etc/rfgrep/config.toml");
        if path.exists() {
            Ok(Some(Config::load_from_path(&path)?))
        } else {
            Ok(None)
        }
    }

    fn load_user_config() -> RfgrepResult<Option<Config>> {
        if let Some(config_dir) = dirs::config_dir() {
            let path = config_dir.join("rfgrep/config.toml");
            if path.exists() {
                return Ok(Some(Config::load_from_path(&path)?));
            }
        }
        Ok(None)
    }

    fn load_project_config(root: &Path) -> RfgrepResult<Option<Config>> {
        let path = root.join(".rfgreprc");
        if path.exists() {
            // Try TOML parsing for now, assuming .rfgreprc is TOML
            return Ok(Some(Config::load_from_path(&path)?));
        }
        Ok(None)
    }

    fn load_env_config() -> RfgrepResult<Config> {
        let mut config = Config::default();

        if let Ok(val) = env::var("RFGREP_THREADS") {
            if let Ok(v) = val.parse() {
                config.search.threads = v;
            }
        }
        // ... load other env vars
        Ok(config)
    }

    fn find_project_root(start_dir: &Path) -> RfgrepResult<Option<PathBuf>> {
        let mut current = start_dir.to_path_buf();
        loop {
            for marker in &[".git", ".rfgreprc", "Cargo.toml"] {
                if current.join(marker).exists() {
                    return Ok(Some(current));
                }
            }
            if !current.pop() {
                break;
            }
        }
        Ok(None)
    }
}