feat: default option for depth from config file

- If `--depth` flag is not defined (or it has an invalid value), a value
  from the config file will be used.
- If no `depth` entry in the config file (or there is no config file),
  the default `usize::MAX` will be used.

Added test cases:
- no config and no flag defined              -> `usize::MAX` should be used
- config defined, but flag is not defined    -> config value should be used
- config is not defined, but flag is defined -> flag value should be used
- both config and flag is defined            -> flag value should be used

Additional changes:

- Fixed some clippy issues.
- Added comments to the example `config.toml` file.
  (copy from flag description)

Closes: #314

Signed-off-by: Balazs Nadasdi <efertone@pm.me>
This commit is contained in:
Efertone
2023-02-28 17:19:13 +01:00
committed by andy.boot
parent 7d13fe972c
commit 2f7c197cd7
5 changed files with 69 additions and 3 deletions

View File

@@ -3,11 +3,29 @@
# ~/.config/dust/config.toml
# ~/.dust.toml
# Print tree upside down (biggest highest)
reverse=true
# Subdirectories will not have their path shortened
display-full-paths=true
# Use file length instead of blocks
display-apparent-size=true
# No colors will be printed
no-colors=true
# No percent bars or percentages will be displayed
no-bars=true
# No total row will be displayed
skip-total=true
# Do not display hidden files
ignore-hidden=true
# print sizes in powers of 1000 (e.g., 1.1G)
iso=true
# Depth to show
depth=1

View File

@@ -23,6 +23,7 @@ pub struct Config {
pub only_dir: Option<bool>,
pub only_file: Option<bool>,
pub disable_progress: Option<bool>,
pub depth: Option<usize>,
}
impl Config {
@@ -59,6 +60,15 @@ impl Config {
pub fn get_screen_reader(&self, options: &ArgMatches) -> bool {
Some(true) == self.screen_reader || options.is_present("screen_reader")
}
pub fn get_depth(&self, options: &ArgMatches) -> usize {
if let Some(v) = options.value_of("depth") {
if let Ok(v) = v.parse::<usize>() {
return v
}
}
self.depth.unwrap_or(usize::MAX)
}
pub fn get_min_size(&self, options: &ArgMatches, iso: bool) -> Option<usize> {
let size_from_param = options.value_of("min_size");
self._get_min_size(size_from_param, iso)
@@ -137,9 +147,11 @@ pub fn get_config() -> Config {
}
}
#[cfg(test)]
mod tests {
#[allow(unused_imports)]
use super::*;
use clap::{Command, Arg, ArgMatches};
#[test]
fn test_conversion() {
@@ -164,4 +176,40 @@ mod tests {
assert_eq!(c._get_min_size(None, true), Some(1000));
assert_eq!(c._get_min_size(Some("2K"), true), Some(2000));
}
#[test]
fn test_get_depth() {
// No config and no flag.
let c = Config::default();
let args = get_args(vec![]);
assert_eq!(c.get_depth(&args), usize::MAX);
// Config is not defined and flag is defined.
let c = Config::default();
let args = get_args(vec!["dust", "--depth", "5"]);
assert_eq!(c.get_depth(&args), 5);
// Config is defined and flag is not defined.
let c = Config{
depth: Some(3),
..Default::default()
};
let args = get_args(vec![]);
assert_eq!(c.get_depth(&args), 3);
// Both config and flag are defined.
let c = Config{
depth: Some(3),
..Default::default()
};
let args = get_args(vec!["dust", "--depth", "5"]);
assert_eq!(c.get_depth(&args), 5);
}
fn get_args(args: Vec<&str>) -> ArgMatches {
Command::new("Dust")
.trailing_var_arg(true)
.arg(Arg::new("depth").long("depth").takes_value(true))
.get_matches_from(args)
}
}

View File

@@ -428,7 +428,7 @@ mod tests {
iso: false,
};
DisplayData {
initial: initial,
initial,
num_chars_needed_on_left_most: 5,
base_size: 2_u64.pow(12), // 4.0K
longest_string_length,

View File

@@ -130,7 +130,7 @@ fn main() {
.value_of_t("width")
.unwrap_or_else(|_| get_width_of_terminal());
let depth = options.value_of_t("depth").unwrap_or(usize::MAX);
let depth = config.get_depth(&options);
// If depth is set, then we set the default number_of_lines to be max
// instead of screen height

View File

@@ -147,7 +147,7 @@ pub fn test_output_screen_reader() {
assert!(output.contains("a_file 2"));
// Verify no 'symbols' reported by screen reader
assert!(!output.contains(""));
assert!(!output.contains('│'));
for block in ['█', '▓', '▒', '░'] {
assert!(!output.contains(block));