From 52313fbe65b8e49720e1c9323402e33531fb7d04 Mon Sep 17 00:00:00 2001 From: Benjamin Nguyen Date: Sun, 12 Mar 2023 23:27:28 -0700 Subject: [PATCH 1/2] fix config bug --- src/render/context/mod.rs | 26 ++++++++++++++++++++++++-- src/render/context/test.rs | 4 ++++ tests/data/.erdtreerc | 1 + 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/render/context/mod.rs b/src/render/context/mod.rs index 7d5b9927..7c6d6121 100644 --- a/src/render/context/mod.rs +++ b/src/render/context/mod.rs @@ -2,7 +2,7 @@ use super::{ disk_usage::{DiskUsage, PrefixKind}, order::SortType, }; -use clap::{CommandFactory, Error as ClapError, FromArgMatches, Parser}; +use clap::{ArgMatches, CommandFactory, Error as ClapError, FromArgMatches, Parser}; use ignore::overrides::{Override, OverrideBuilder}; use std::{ convert::From, @@ -106,7 +106,7 @@ impl Context { /// Initializes [Context], optionally reading in the configuration file to override defaults. /// Arguments provided will take precedence over config. pub fn init() -> Result { - let clargs = Context::command().args_override_self(true).get_matches(); + let mut clargs = Context::command().args_override_self(true).get_matches(); let no_config = clargs .get_one("no_config") @@ -124,6 +124,8 @@ impl Context { let mut ctx = Context::from_arg_matches(&config_args).map_err(|e| Error::Config(e))?; + Self::remove_bool_opts(&mut clargs); + ctx.update_from_arg_matches(&clargs) .map_err(|e| Error::ArgParse(e))?; @@ -188,6 +190,26 @@ impl Context { builder.build() } + + /// This is an unfortunate hack to remove default boolean arguments that override the config + /// defaults. Basically how it works is we parse the os args normally, create a [Context] from + /// the config file, then we update the [Context] with the os args; the problem is that the os + /// args come with defaults from [clap] which are all false which then overrides the config. A + /// problem for later. + fn remove_bool_opts(clargs: &mut ArgMatches) { + let _ = clargs.try_remove_occurrences::("icons"); + let _ = clargs.try_remove_occurrences::("I"); + let _ = clargs.try_remove_occurrences::("glob_case_insensitive"); + let _ = clargs.try_remove_occurrences::("hidden"); + let _ = clargs.try_remove_occurrences::("ignore-git"); + let _ = clargs.try_remove_occurrences::("ignore-git-ignore"); + let _ = clargs.try_remove_occurrences::("i"); + let _ = clargs.try_remove_occurrences::("prune"); + let _ = clargs.try_remove_occurrences::("dirs_first"); + let _ = clargs.try_remove_occurrences::("follow_links"); + let _ = clargs.try_remove_occurrences::("S"); + let _ = clargs.try_remove_occurrences::("suppress_size"); + } } #[derive(Debug)] diff --git a/src/render/context/test.rs b/src/render/context/test.rs index bfb53cc8..1e0fcb63 100644 --- a/src/render/context/test.rs +++ b/src/render/context/test.rs @@ -22,6 +22,10 @@ fn config() { SortType::Size, "Failed to properly read 'sort' from config" ); + + let icons = context.icons; + + assert!(icons, "Failed to propertly read 'icons' from config") } fn context_from_config() -> Option { diff --git a/tests/data/.erdtreerc b/tests/data/.erdtreerc index 5f10c759..dc8d7f41 100644 --- a/tests/data/.erdtreerc +++ b/tests/data/.erdtreerc @@ -1,2 +1,3 @@ --level 1 --sort size +--icons From ee700c3a145ec4ec3e9688cbddbf0454acebdfa8 Mon Sep 17 00:00:00 2001 From: Benjamin Nguyen Date: Sun, 12 Mar 2023 23:35:56 -0700 Subject: [PATCH 2/2] hack fix --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/render/context/mod.rs | 41 ++++++++++++++++++++++++++------------- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f4ddf8fa..ab0950a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -166,7 +166,7 @@ dependencies = [ [[package]] name = "erdtree" -version = "1.4.0" +version = "1.4.1" dependencies = [ "ansi_term", "clap", diff --git a/Cargo.toml b/Cargo.toml index 7d28ce45..a00fce69 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "erdtree" -version = "1.4.0" +version = "1.4.1" edition = "2021" authors = ["Benjamin Nguyen "] description = """ diff --git a/src/render/context/mod.rs b/src/render/context/mod.rs index 7c6d6121..7cd1e3c8 100644 --- a/src/render/context/mod.rs +++ b/src/render/context/mod.rs @@ -23,7 +23,7 @@ mod test; #[derive(Parser, Debug)] #[command(name = "erdtree")] #[command(author = "Benjamin Nguyen. ")] -#[command(version = "1.4.0")] +#[command(version = "1.4.1")] #[command(about = "erdtree (et) is a multi-threaded filetree visualizer and disk usage analyzer.", long_about = None)] pub struct Context { /// Root directory to traverse; defaults to current working directory @@ -196,19 +196,32 @@ impl Context { /// the config file, then we update the [Context] with the os args; the problem is that the os /// args come with defaults from [clap] which are all false which then overrides the config. A /// problem for later. - fn remove_bool_opts(clargs: &mut ArgMatches) { - let _ = clargs.try_remove_occurrences::("icons"); - let _ = clargs.try_remove_occurrences::("I"); - let _ = clargs.try_remove_occurrences::("glob_case_insensitive"); - let _ = clargs.try_remove_occurrences::("hidden"); - let _ = clargs.try_remove_occurrences::("ignore-git"); - let _ = clargs.try_remove_occurrences::("ignore-git-ignore"); - let _ = clargs.try_remove_occurrences::("i"); - let _ = clargs.try_remove_occurrences::("prune"); - let _ = clargs.try_remove_occurrences::("dirs_first"); - let _ = clargs.try_remove_occurrences::("follow_links"); - let _ = clargs.try_remove_occurrences::("S"); - let _ = clargs.try_remove_occurrences::("suppress_size"); + fn remove_bool_opts(args: &mut ArgMatches) { + let mut remove_if_default = |arg| { + let enabled = args + .try_get_one::(arg) + .ok() + .flatten() + .map(bool::clone) + .unwrap_or(true); + + if !enabled { + let _ = args.try_remove_occurrences::(arg); + } + }; + + remove_if_default("icons"); + remove_if_default("I"); + remove_if_default("glob_case_insensitive"); + remove_if_default("hidden"); + remove_if_default("ignore-git"); + remove_if_default("ignore-git-ignore"); + remove_if_default("i"); + remove_if_default("prune"); + remove_if_default("dirs_first"); + remove_if_default("follow_links"); + remove_if_default("S"); + remove_if_default("suppress_size"); } }