mirror of
https://github.com/bootandy/dust.git
synced 2026-06-08 11:29:05 +03:00
Feature: Config file
Read flags for several params from config file. This was a popular requested feature (many people want to run with -r on by default).
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
use clap::ArgMatches;
|
||||
use config_file::FromConfigFile;
|
||||
use serde::Deserialize;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Deserialize, Default)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Config {
|
||||
pub display_full_paths: Option<bool>,
|
||||
pub display_apparent_size: Option<bool>,
|
||||
pub reverse: Option<bool>,
|
||||
pub no_colors: Option<bool>,
|
||||
pub no_bars: Option<bool>,
|
||||
pub skip_total: Option<bool>,
|
||||
pub ignore_hidden: Option<bool>,
|
||||
pub iso: Option<bool>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn get_no_colors(&self, options: &ArgMatches) -> bool {
|
||||
Some(true) == self.no_colors || options.is_present("no_colors")
|
||||
}
|
||||
pub fn get_apparent_size(&self, options: &ArgMatches) -> bool {
|
||||
Some(true) == self.display_apparent_size || options.is_present("display_apparent_size")
|
||||
}
|
||||
pub fn get_ignore_hidden(&self, options: &ArgMatches) -> bool {
|
||||
Some(true) == self.ignore_hidden || options.is_present("ignore_hidden")
|
||||
}
|
||||
pub fn get_full_paths(&self, options: &ArgMatches) -> bool {
|
||||
Some(true) == self.display_full_paths || options.is_present("display_full_paths")
|
||||
}
|
||||
pub fn get_reverse(&self, options: &ArgMatches) -> bool {
|
||||
Some(true) == self.reverse || options.is_present("reverse")
|
||||
}
|
||||
pub fn get_no_bars(&self, options: &ArgMatches) -> bool {
|
||||
Some(true) == self.no_bars || options.is_present("no_bars")
|
||||
}
|
||||
pub fn get_iso(&self, options: &ArgMatches) -> bool {
|
||||
Some(true) == self.iso || options.is_present("iso")
|
||||
}
|
||||
pub fn get_skip_total(&self, options: &ArgMatches) -> bool {
|
||||
Some(true) == self.skip_total || options.is_present("skip_total")
|
||||
}
|
||||
}
|
||||
|
||||
fn get_config_locations(base: &Path) -> Vec<PathBuf> {
|
||||
vec![
|
||||
base.join(".dust.toml"),
|
||||
base.join(".config").join("dust").join("config.toml"),
|
||||
]
|
||||
}
|
||||
|
||||
pub fn get_config() -> Config {
|
||||
if let Some(home) = directories::BaseDirs::new() {
|
||||
for path in get_config_locations(home.home_dir()) {
|
||||
if path.exists() {
|
||||
if let Ok(config) = Config::from_config_file(path) {
|
||||
return config;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Config {
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
+14
-12
@@ -9,6 +9,7 @@ use std::process;
|
||||
use self::display::draw_it;
|
||||
use clap::{crate_version, Arg};
|
||||
use clap::{Command, Values};
|
||||
use config::get_config;
|
||||
use dir_walker::{walk_it, WalkData};
|
||||
use filter::get_biggest;
|
||||
use filter_type::get_all_file_types;
|
||||
@@ -19,6 +20,7 @@ use terminal_size::{terminal_size, Height, Width};
|
||||
use utils::get_filesystem_devices;
|
||||
use utils::simplify_dir_names;
|
||||
|
||||
mod config;
|
||||
mod dir_walker;
|
||||
mod display;
|
||||
mod display_node;
|
||||
@@ -209,15 +211,17 @@ fn main() {
|
||||
.number_of_values(1)
|
||||
.help("Specify width of output overriding the auto detection of terminal width"),
|
||||
)
|
||||
.arg(Arg::new("inputs").multiple_occurrences(true).default_value("."))
|
||||
.arg(
|
||||
Arg::new("iso")
|
||||
.short('H')
|
||||
.long("si")
|
||||
.help("print sizes in powers of 1000 (e.g., 1.1G)")
|
||||
)
|
||||
.arg(Arg::new("inputs").multiple_occurrences(true).default_value("."))
|
||||
.get_matches();
|
||||
|
||||
let config = get_config();
|
||||
|
||||
let target_dirs = options
|
||||
.values_of("inputs")
|
||||
.expect("Should be a default value here")
|
||||
@@ -254,15 +258,14 @@ fn main() {
|
||||
})
|
||||
.unwrap_or(default_height);
|
||||
|
||||
let no_colors = init_color(options.is_present("no_colors"));
|
||||
let use_apparent_size = options.is_present("display_apparent_size");
|
||||
let no_colors = init_color(config.get_no_colors(&options));
|
||||
|
||||
let ignore_directories = options
|
||||
.values_of("ignore_directory")
|
||||
.unwrap_or_default()
|
||||
.map(PathBuf::from);
|
||||
|
||||
let by_filecount = options.is_present("by_filecount");
|
||||
let ignore_hidden = options.is_present("ignore_hidden");
|
||||
let limit_filesystem = options.is_present("limit_filesystem");
|
||||
|
||||
let simplified_dirs = simplify_dir_names(target_dirs);
|
||||
@@ -279,9 +282,9 @@ fn main() {
|
||||
filter_regex: &filter_regexs,
|
||||
invert_filter_regex: &invert_filter_regexs,
|
||||
allowed_filesystems,
|
||||
use_apparent_size,
|
||||
use_apparent_size: config.get_apparent_size(&options),
|
||||
by_filecount,
|
||||
ignore_hidden,
|
||||
ignore_hidden: config.get_ignore_hidden(&options),
|
||||
};
|
||||
// Larger stack size to handle cases with lots of nested directories
|
||||
rayon::ThreadPoolBuilder::new()
|
||||
@@ -304,18 +307,17 @@ fn main() {
|
||||
if has_errors {
|
||||
eprintln!("Did not have permissions for all directories");
|
||||
}
|
||||
|
||||
if let Some(root_node) = tree {
|
||||
draw_it(
|
||||
options.is_present("display_full_paths"),
|
||||
!options.is_present("reverse"),
|
||||
config.get_full_paths(&options),
|
||||
!config.get_reverse(&options),
|
||||
no_colors,
|
||||
options.is_present("no_bars"),
|
||||
config.get_no_bars(&options),
|
||||
terminal_width,
|
||||
by_filecount,
|
||||
&root_node,
|
||||
options.is_present("iso"),
|
||||
options.is_present("skip_total"),
|
||||
config.get_iso(&options),
|
||||
config.get_skip_total(&options),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user