batless 0.7.0

A fast, non-blocking code and text viewer inspired by bat
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
//! Manages application configuration by merging settings from files,
//! command-line arguments, and profiles.

use crate::config::BatlessConfig;
use crate::error::{BatlessError, BatlessResult};
use crate::formatter::OutputMode;
use clap::{CommandFactory, FromArgMatches, Parser, ValueEnum};
use is_terminal::IsTerminal;
use std::str::FromStr;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
pub struct Args {
    /// File to view
    pub file: Option<String>,

    /// Language for syntax highlighting (auto-detect if not specified)
    #[arg(long)]
    pub language: Option<String>,

    /// Limit lines shown
    #[arg(long)]
    pub max_lines: Option<usize>,

    /// Limit bytes shown
    #[arg(long)]
    pub max_bytes: Option<usize>,

    /// Output mode
    #[arg(long, value_enum)]
    pub mode: Option<CliOutputMode>,

    /// Color output control
    #[arg(long, value_enum, default_value = "auto")]
    pub color: ColorMode,

    /// Tracks whether --color was explicitly provided
    #[arg(skip)]
    pub color_specified: bool,

    /// Strip ANSI escape codes from output
    #[arg(long)]
    pub strip_ansi: bool,

    /// List all supported languages
    #[arg(long)]
    pub list_languages: bool,

    /// Generate shell completions for the specified shell
    #[arg(long, value_enum)]
    pub generate_completions: Option<Shell>,

    /// Configuration file path (defaults to auto-discovery)
    #[arg(long)]
    pub config: Option<String>,

    /// Enable debug mode with detailed processing information
    #[arg(long)]
    pub debug: bool,

    /// PAGER compatibility: equivalent to --mode plain (for cat replacement)
    #[arg(long)]
    pub plain: bool,

    /// PAGER compatibility: ignored for compatibility with other pagers
    #[arg(short = 'u', long)]
    pub unbuffered: bool,

    /// CAT compatibility: show line numbers (like cat -n)
    #[arg(short = 'n', long)]
    pub number: bool,

    /// CAT compatibility: number non-blank output lines (like cat -b)
    #[arg(short = 'b', long)]
    pub number_nonblank: bool,

    /// PAGER compatibility: ignored for compatibility with less (no title bar)
    #[arg(long)]
    pub no_title: bool,

    /// Output version information as machine-readable JSON
    #[arg(long)]
    pub version_json: bool,

    /// Pretty-print JSON output (when --mode=json)
    #[arg(long)]
    pub json_pretty: bool,

    /// Include 1-based line numbers in JSON output lines array (e.g. {"n":1,"text":"..."})
    #[arg(long)]
    pub with_line_numbers: bool,

    /// Strip comment-only lines from output
    #[arg(long)]
    pub strip_comments: bool,

    /// Strip blank lines from output
    #[arg(long)]
    pub strip_blank_lines: bool,
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
pub enum CliOutputMode {
    Plain,
    Json,
    /// Machine-readable symbol index with line ranges
    Index,
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
pub enum Shell {
    Bash,
    Zsh,
    Fish,
    #[clap(name = "power-shell")]
    Power,
}

impl From<CliOutputMode> for OutputMode {
    fn from(mode: CliOutputMode) -> Self {
        match mode {
            CliOutputMode::Plain => Self::Plain,
            CliOutputMode::Json => Self::Json,
            CliOutputMode::Index => Self::Index,
        }
    }
}

impl FromStr for OutputMode {
    type Err = BatlessError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "plain" => Ok(Self::Plain),
            "json" => Ok(Self::Json),
            "index" => Ok(Self::Index),
            _ => Err(BatlessError::ConfigurationError {
                message: format!("Invalid output mode: {s}"),
                help: Some("Valid modes are: plain, json, index".to_string()),
            }),
        }
    }
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
pub enum ColorMode {
    Auto,
    Always,
    Never,
}

/// Manages the application's configuration, merging settings from various
/// sources like files, command-line arguments, and profiles.
pub struct ConfigManager {
    args: Args,
    config: BatlessConfig,
    output_mode: OutputMode,
}

impl ConfigManager {
    /// Creates a new `ConfigManager` by parsing command-line arguments
    /// and loading configuration from files.
    pub fn new() -> BatlessResult<Self> {
        let command = Args::command();
        let matches = command.get_matches();
        let mut args = Args::from_arg_matches(&matches).map_err(|e| {
            BatlessError::config_error_with_help(
                format!("Failed to parse CLI arguments: {e}"),
                Some("Run `batless --help` for valid options".to_string()),
            )
        })?;
        args.color_specified = matches.contains_id("color");

        let mut manager = Self {
            args,
            config: BatlessConfig::default(),
            output_mode: OutputMode::Plain,
        };
        manager.load_and_apply_config()?;
        Ok(manager)
    }

    /// Creates a `ConfigManager` from a vector of argument strings.
    /// Useful for testing and programmatic usage.
    pub fn from_args_vec<I, T>(args: I) -> BatlessResult<Self>
    where
        I: IntoIterator<Item = T>,
        T: Into<std::ffi::OsString> + Clone,
    {
        let command = Args::command();
        let matches = command.try_get_matches_from(args).map_err(|e| {
            BatlessError::config_error_with_help(
                format!("Failed to parse arguments: {e}"),
                Some("Run `batless --help` for valid options".to_string()),
            )
        })?;
        let mut parsed_args = Args::from_arg_matches(&matches).map_err(|e| {
            BatlessError::config_error_with_help(
                format!("Failed to parse arguments: {e}"),
                Some("Run `batless --help` for valid options".to_string()),
            )
        })?;
        parsed_args.color_specified = matches.contains_id("color");

        let mut manager = Self {
            args: parsed_args,
            config: BatlessConfig::default(),
            output_mode: OutputMode::Plain,
        };
        manager.load_and_apply_config()?;
        Ok(manager)
    }

    /// Returns a reference to the parsed command-line arguments.
    pub const fn args(&self) -> &Args {
        &self.args
    }

    /// Returns a reference to the final, merged `BatlessConfig`.
    pub const fn config(&self) -> &BatlessConfig {
        &self.config
    }

    /// Returns the determined `OutputMode`.
    pub const fn output_mode(&self) -> OutputMode {
        self.output_mode
    }

    /// Determines the file path to process, handling stdin as well.
    pub fn file_path(&self) -> BatlessResult<String> {
        self.args.file.as_ref().map_or_else(
            || {
                if !std::io::IsTerminal::is_terminal(&std::io::stdin()) {
                    Ok("-".to_string())
                } else {
                    Err(BatlessError::config_error_with_help(
                        "File path required".to_string(),
                        Some(
                            "Specify a file to view, pipe input via stdin, or use --help for more options."
                                .to_string(),
                        ),
                    ))
                }
            },
            |file| Ok(file.clone()),
        )
    }

    /// Loads configuration from files, applies command-line arguments,
    /// and resolves profiles to create the final configuration.
    fn load_and_apply_config(&mut self) -> BatlessResult<()> {
        // 1. Load base configuration from files
        self.config = if let Some(config_path) = &self.args.config {
            let path = std::path::Path::new(config_path);
            if path.extension() == Some(std::ffi::OsStr::new("toml")) {
                BatlessConfig::from_file(path)?
            } else {
                BatlessConfig::from_json_file(path)?
            }
        } else {
            BatlessConfig::load_with_precedence()?
        };

        // 2. Apply command-line arguments
        self.apply_cli_args();

        // 3. Resolve output mode
        self.resolve_output_mode()?;

        // 4. Handle compatibility flags
        self.apply_compatibility_flags();

        // 5. Final validation
        self.config.validate()?;
        self.validate_language()?;

        Ok(())
    }

    /// Applies command-line arguments to the configuration.
    fn apply_cli_args(&mut self) {
        // Build a new config from the current one with all CLI args applied
        let mut new_config = std::mem::take(&mut self.config);

        if let Some(max_lines) = self.args.max_lines {
            new_config = new_config.with_max_lines(max_lines);
        }
        if self.args.max_bytes.is_some() {
            new_config = new_config.with_max_bytes(self.args.max_bytes);
        }
        if let Some(ref language) = self.args.language {
            new_config = new_config.with_language(Some(language.clone()));
        }
        if self.args.strip_ansi {
            new_config = new_config.with_strip_ansi(self.args.strip_ansi);
        }

        let use_color = if self.args.color_specified {
            match self.args.color {
                ColorMode::Always => true,
                ColorMode::Never => false,
                ColorMode::Auto => std::io::stdout().is_terminal(),
            }
        } else if new_config.use_color {
            std::io::stdout().is_terminal()
        } else {
            false
        };
        new_config = new_config.with_use_color(use_color);

        if self.args.json_pretty {
            new_config = new_config.with_pretty_json(true);
        }
        if self.args.with_line_numbers {
            new_config = new_config.with_json_line_numbers(true);
        }
        if self.args.strip_comments {
            new_config = new_config.with_strip_comments(true);
        }
        if self.args.strip_blank_lines {
            new_config = new_config.with_strip_blank_lines(true);
        }
        if self.args.debug {
            new_config = new_config.with_debug(self.args.debug);
        }

        self.config = new_config;
    }

    /// Resolves the output mode from the `--mode` flag.
    fn resolve_output_mode(&mut self) -> BatlessResult<()> {
        self.output_mode = self.args.mode.map_or(OutputMode::Plain, Into::into);
        Ok(())
    }

    /// Applies compatibility flags like `--plain` and `--number`.
    fn apply_compatibility_flags(&mut self) {
        if self.args.plain {
            self.output_mode = OutputMode::Plain;
            self.config = std::mem::take(&mut self.config).with_use_color(false);
        }
        if self.args.number {
            self.config = std::mem::take(&mut self.config).with_show_line_numbers(true);
        }
        if self.args.number_nonblank {
            self.config = std::mem::take(&mut self.config).with_show_line_numbers_nonblank(true);
        }
    }

    /// Validates the language setting.
    fn validate_language(&self) -> BatlessResult<()> {
        if let Some(ref lang) = self.config.language {
            crate::LanguageDetector::validate_language(lang)?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn make_manager(args: &[&str]) -> ConfigManager {
        let mut full_args = vec!["batless"];
        full_args.extend_from_slice(args);
        ConfigManager::from_args_vec(full_args).unwrap()
    }

    #[test]
    fn test_default_output_mode() {
        let mgr = make_manager(&["Cargo.toml"]);
        assert_eq!(mgr.output_mode(), OutputMode::Plain);
    }

    #[test]
    fn test_plain_mode_flag() {
        let mgr = make_manager(&["--plain", "Cargo.toml"]);
        assert_eq!(mgr.output_mode(), OutputMode::Plain);
        assert!(!mgr.config().use_color);
    }

    #[test]
    fn test_json_mode() {
        let mgr = make_manager(&["--mode=json", "Cargo.toml"]);
        assert_eq!(mgr.output_mode(), OutputMode::Json);
    }

    #[test]
    fn test_max_lines_applied() {
        let mgr = make_manager(&["--max-lines=42", "Cargo.toml"]);
        assert_eq!(mgr.config().max_lines, 42);
    }

    #[test]
    fn test_max_bytes_applied() {
        let mgr = make_manager(&["--max-bytes=1024", "--max-lines=10", "Cargo.toml"]);
        assert_eq!(mgr.config().max_bytes, Some(1024));
    }

    #[test]
    fn test_language_override() {
        let mgr = make_manager(&["--language=python", "Cargo.toml"]);
        assert_eq!(mgr.config().language, Some("python".to_string()));
    }

    #[test]
    fn test_strip_ansi() {
        let mgr = make_manager(&["--strip-ansi", "Cargo.toml"]);
        assert!(mgr.config().strip_ansi);
    }

    #[test]
    fn test_line_numbers_flag() {
        let mgr = make_manager(&["-n", "--plain", "Cargo.toml"]);
        assert!(mgr.config().show_line_numbers);
    }

    #[test]
    fn test_number_nonblank_flag() {
        let mgr = make_manager(&["-b", "--plain", "Cargo.toml"]);
        assert!(mgr.config().show_line_numbers_nonblank);
    }

    #[test]
    fn test_debug_flag() {
        let mgr = make_manager(&["--debug", "Cargo.toml"]);
        assert!(mgr.config().debug);
    }

    #[test]
    fn test_color_never() {
        let mgr = make_manager(&["--color=never", "Cargo.toml"]);
        assert!(!mgr.config().use_color);
    }

    #[test]
    fn test_json_pretty() {
        let mgr = make_manager(&["--json-pretty", "--mode=json", "Cargo.toml"]);
        assert!(mgr.config().pretty_json);
    }

    #[test]
    fn test_file_path_from_arg() {
        let mgr = make_manager(&["Cargo.toml"]);
        assert_eq!(mgr.file_path().unwrap(), "Cargo.toml");
    }

    #[test]
    fn test_file_path_missing_errors() {
        let mgr = make_manager(&["--mode=plain"]);
        let result = mgr.file_path();
        if std::io::IsTerminal::is_terminal(&std::io::stdin()) {
            assert!(result.is_err());
        } else {
            assert!(result.is_ok());
        }
    }

    #[test]
    fn test_invalid_language_rejected() {
        let result = ConfigManager::from_args_vec([
            "batless",
            "--language=nonexistent_lang_xyz",
            "Cargo.toml",
        ]);
        assert!(result.is_err());
    }

    #[test]
    fn test_output_mode_from_str() {
        assert_eq!(OutputMode::from_str("plain").unwrap(), OutputMode::Plain);
        assert_eq!(OutputMode::from_str("json").unwrap(), OutputMode::Json);
        assert_eq!(OutputMode::from_str("index").unwrap(), OutputMode::Index);
        assert!(OutputMode::from_str("highlight").is_err());
        assert!(OutputMode::from_str("summary").is_err());
        assert!(OutputMode::from_str("invalid").is_err());
    }

    #[test]
    fn test_cli_output_mode_conversion() {
        assert_eq!(OutputMode::from(CliOutputMode::Plain), OutputMode::Plain);
        assert_eq!(OutputMode::from(CliOutputMode::Json), OutputMode::Json);
        assert_eq!(OutputMode::from(CliOutputMode::Index), OutputMode::Index);
    }

    #[test]
    fn test_plain_flag_overrides_mode() {
        // --plain should override --mode=json
        let mgr = make_manager(&["--mode=json", "--plain", "Cargo.toml"]);
        assert_eq!(mgr.output_mode(), OutputMode::Plain);
    }

    #[test]
    fn test_multiple_flags_combined() {
        let mgr = make_manager(&[
            "--max-lines=100",
            "--max-bytes=5000",
            "--mode=json",
            "Cargo.toml",
        ]);
        assert_eq!(mgr.config().max_lines, 100);
        assert_eq!(mgr.config().max_bytes, Some(5000));
        assert_eq!(mgr.output_mode(), OutputMode::Json);
    }

    #[test]
    fn test_from_args_vec_invalid_args() {
        let result = ConfigManager::from_args_vec(["batless", "--nonexistent-flag"]);
        assert!(result.is_err());
    }
}