mirror of
https://github.com/bootandy/dust.git
synced 2025-12-12 07:40:40 -08:00
fix: number-of-lines option in config file is ignored (#528)
Co-authored-by: andy.boot <bootandy@gmail.com>
This commit is contained in:
@@ -36,6 +36,7 @@ pub struct Config {
|
||||
pub output_json: Option<bool>,
|
||||
pub print_errors: Option<bool>,
|
||||
pub files0_from: Option<String>,
|
||||
pub number_of_lines: Option<usize>,
|
||||
pub files_from: Option<String>,
|
||||
}
|
||||
|
||||
@@ -156,6 +157,15 @@ impl Config {
|
||||
Some(true) == self.output_json || options.output_json
|
||||
}
|
||||
|
||||
pub fn get_number_of_lines(&self, options: &Cli) -> Option<usize> {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user