summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEfertone <efertone@pm.me>2023-02-28 17:19:13 +0100
committerandy.boot <bootandy@gmail.com>2023-03-06 21:43:15 +0000
commit2f7c197cd703e199978bcb085adba6bc01bddc25 (patch)
tree5a54e21a8241a43b25d432ecba23f5049075a1d8
parent7d13fe972cb45c4379a88d229ac5055dacf5956a (diff)
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>
-rw-r--r--config/config.toml18
-rw-r--r--src/config.rs48
-rw-r--r--src/display.rs2
-rw-r--r--src/main.rs2
-rw-r--r--tests/test_flags.rs2
5 files changed, 69 insertions, 3 deletions
diff --git a/config/config.toml b/config/config.toml
index f45a1c0..6d10c1a 100644
--- a/config/config.toml
+++ b/config/config.toml
@@ -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
diff --git a/src/config.rs b/src/config.rs
index a85e159..96d1aa5 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -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)
+ }
}
diff --git a/src/display.rs b/src/display.rs
index 9763227..e6837cd 100644
--- a/src/display.rs
+++ b/src/display.rs
@@ -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,
diff --git a/src/main.rs b/src/main.rs
index 365da1f..38ec879 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -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
diff --git a/tests/test_flags.rs b/tests/test_flags.rs
index cdf658a..583f87c 100644
--- a/tests/test_flags.rs
+++ b/tests/test_flags.rs
@@ -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));