Skip to content

Commit

Permalink
Resolve '~' to home directory for 'todo-file' and 'done-file'
Browse files Browse the repository at this point in the history
This adds the ability to resolve a '~' used in the 'todo-file' and
'done-file' variable of the configuration to be resolved by the 'dirs'
crate to the home directory. The path is only changed when 'dirs' can
resolve the home directory, so it should be save on windows and
unsupported systems.
  • Loading branch information
ezickler committed Jan 13, 2024
1 parent ca701ee commit a3323a8
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,16 +744,20 @@ fn detect_filenames(matches: &Matches, conf: &mut Conf) {
conf.todo_file = PathBuf::from(val);
}
}

resolve_home_directory(&mut conf.todo_file);

if conf.todo_file.is_dir() {
conf.todo_file.push(TODO_FILE);
}

if let Some(val) = matches.opt_str("done-file") {
if !val.is_empty() {
let pb = PathBuf::from(val.clone());
let mut pb = PathBuf::from(val.clone());
if pb.parent() == Some(&PathBuf::from("")) {
conf.done_file = conf.todo_file.with_file_name(val);
} else {
resolve_home_directory(&mut pb);
conf.done_file = pb;
}
}
Expand All @@ -766,6 +770,14 @@ fn detect_filenames(matches: &Matches, conf: &mut Conf) {
}
}

fn resolve_home_directory(path: &mut PathBuf) {
if let Ok(path_striped) = path.strip_prefix("~") {
if let Some(home) = dirs::home_dir() {
*path = home.join(path_striped);
}
}
}

fn read_color(clr: &Option<String>) -> Result<ColorSpec> {
let s = match clr {
Some(ss) => ss,
Expand Down

0 comments on commit a3323a8

Please sign in to comment.