Merge pull request #1454 from jpttrssn/expand-tilde

fix: Expand tilde in location path
This commit is contained in:
Levi Portenier 2025-12-19 12:34:57 -07:00 committed by GitHub
commit aab6bd95e2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1489,6 +1489,7 @@ impl Location {
}
pub fn with_path(&self, path: PathBuf) -> Self {
let path = Self::expand_tilde(path);
match self {
Self::Desktop(_, display, desktop_config) => {
Self::Desktop(path, display.clone(), *desktop_config)
@ -1562,6 +1563,22 @@ impl Location {
Self::Network(display_name, ..) => display_name.clone(),
}
}
/// Expand a path that starts with "~" with the
/// user's home directory
pub fn expand_tilde(path: PathBuf) -> PathBuf {
let mut components = path.components();
match components.next() {
Some(std::path::Component::Normal(os_str)) if os_str == "~" => {
if let Some(home) = dirs::home_dir() {
home.join(components.as_path())
} else {
path
}
}
_ => path,
}
}
}
pub struct TaskWrapper(pub cosmic::Task<Message>);