Skip to content
Open
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ Usage: dust --collapse=node-modules will keep the node-modules folder collapsed
## Config file

Dust has a config file where the above options can be set.
Either: `~/.config/dust/config.toml` or `~/.dust.toml`
Either: `$XDG_CONFIG_HOME/dust/config.toml` (falling back to
`~/.config/dust/config.toml`) or `~/.dust.toml`
```
$ cat ~/.config/dust/config.toml
reverse=true
Expand All @@ -155,4 +156,3 @@ Dust will not count hard links multiple times (unless you want to `-s`).

Typing `dust -n 90` will show you your 90 largest entries. `-n` is not quite like `head -n` or `tail -n`, dust is intelligent and chooses the largest entries


32 changes: 29 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,11 @@ fn convert_min_size(input: &str) -> Option<usize> {
}
}

fn get_config_locations(base: PathBuf) -> Vec<PathBuf> {
fn get_config_locations(base: PathBuf, config_home: Option<PathBuf>) -> Vec<PathBuf> {
let config_home = config_home.unwrap_or_else(|| base.join(".config"));
vec![
base.join(".dust.toml"),
base.join(".config").join("dust").join("config.toml"),
config_home.join("dust").join("config.toml"),
]
}

Expand All @@ -277,7 +278,11 @@ pub fn get_config(conf_path: Option<&String>) -> Config {
}
None => {
if let Some(home) = std::env::home_dir() {
for path in get_config_locations(home) {
let config_home = std::env::var_os("XDG_CONFIG_HOME")
.filter(|path| !path.is_empty() && Path::new(path).is_absolute())
.map(PathBuf::from);

for path in get_config_locations(home, config_home) {
if path.exists()
&& let Ok(config) = Config::from_config_file(&path)
{
Expand All @@ -299,6 +304,27 @@ mod tests {
use chrono::{Datelike, Timelike};
use clap::Parser;

#[test]
fn config_locations_use_xdg_config_home() {
let home = PathBuf::from("/home/test");
let config_home = PathBuf::from("/tmp/config");

assert_eq!(
get_config_locations(home.clone(), Some(config_home)),
vec![
home.join(".dust.toml"),
PathBuf::from("/tmp/config/dust/config.toml"),
]
);
assert_eq!(
get_config_locations(home.clone(), None),
vec![
home.join(".dust.toml"),
home.join(".config/dust/config.toml"),
]
);
}

#[test]
fn test_get_current_date_epoch_seconds() {
let epoch_seconds = get_current_date_epoch_seconds();
Expand Down