From 96e04fe1685d73254d92daea47e22f21aab4a3a1 Mon Sep 17 00:00:00 2001 From: Vaso Putica Date: Mon, 6 Oct 2025 21:22:35 +0200 Subject: [PATCH] fix: number-of-lines option in config file is ignored (#528) Co-authored-by: andy.boot --- src/config.rs | 39 +++++++++++++++++++++++++++++++++++++++ src/main.rs | 2 +- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index 80bb69e..f31fc21 100644 --- a/src/config.rs +++ b/src/config.rs @@ -36,6 +36,7 @@ pub struct Config { pub output_json: Option, pub print_errors: Option, pub files0_from: Option, + pub number_of_lines: Option, pub files_from: Option, } @@ -156,6 +157,15 @@ impl Config { Some(true) == self.output_json || options.output_json } + pub fn get_number_of_lines(&self, options: &Cli) -> Option { + let from_cmd_line = options.number_of_lines; + if from_cmd_line.is_none() { + self.number_of_lines + } else { + from_cmd_line + } + } + pub fn get_modified_time_operator(&self, options: &Cli) -> Option<(Operator, i64)> { get_filter_time_operator(options.mtime.as_ref(), get_current_date_epoch_seconds()) } @@ -389,4 +399,33 @@ mod tests { fn get_filetime_args(args: Vec<&str>) -> Cli { Cli::parse_from(args) } + + #[test] + fn test_get_number_of_lines() { + // No config and no flag. + let c = Config::default(); + let args = get_args(vec![]); + assert_eq!(c.get_number_of_lines(&args), None); + + // Config is not defined and flag is defined. + let c = Config::default(); + let args = get_args(vec!["dust", "--number-of-lines", "5"]); + assert_eq!(c.get_number_of_lines(&args), Some(5)); + + // Config is defined and flag is not defined. + let c = Config { + number_of_lines: Some(3), + ..Default::default() + }; + let args = get_args(vec![]); + assert_eq!(c.get_number_of_lines(&args), Some(3)); + + // Both config and flag are defined. + let c = Config { + number_of_lines: Some(3), + ..Default::default() + }; + let args = get_args(vec!["dust", "--number-of-lines", "5"]); + assert_eq!(c.get_number_of_lines(&args), Some(5)); + } } diff --git a/src/main.rs b/src/main.rs index cf1478c..9ecab8c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -154,7 +154,7 @@ fn main() { // If depth is set, then we set the default number_of_lines to be max // instead of screen height - let number_of_lines = match options.number_of_lines { + let number_of_lines = match config.get_number_of_lines(&options) { Some(val) => val, None => { if depth != usize::MAX {