Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config bug #60

Merged
merged 2 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "erdtree"
version = "1.4.0"
version = "1.4.1"
edition = "2021"
authors = ["Benjamin Nguyen <[email protected]>"]
description = """
Expand Down
41 changes: 38 additions & 3 deletions src/render/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -23,7 +23,7 @@ mod test;
#[derive(Parser, Debug)]
#[command(name = "erdtree")]
#[command(author = "Benjamin Nguyen. <[email protected]>")]
#[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
Expand Down Expand Up @@ -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<Self, Error> {
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")
Expand All @@ -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))?;

Expand Down Expand Up @@ -188,6 +190,39 @@ 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(args: &mut ArgMatches) {
let mut remove_if_default = |arg| {
let enabled = args
.try_get_one::<bool>(arg)
.ok()
.flatten()
.map(bool::clone)
.unwrap_or(true);

if !enabled {
let _ = args.try_remove_occurrences::<bool>(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");
}
}

#[derive(Debug)]
Expand Down
4 changes: 4 additions & 0 deletions src/render/context/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Context> {
Expand Down
1 change: 1 addition & 0 deletions tests/data/.erdtreerc
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
--level 1
--sort size
--icons