aboutsummaryrefslogtreecommitdiff
path: root/src/app.rs
diff options
context:
space:
mode:
authorlukeflo2024-12-01 16:22:22 +0100
committerlukeflo2024-12-01 16:22:22 +0100
commitf61c5538e1c1702ff7aadde635746e96fa51aefb (patch)
tree0a9534b457f855ee9b8010a9cb977556fd9d0cda /src/app.rs
parente50efa9b061785c2bfb809857f3f727882cc5886 (diff)
downloadbibiman-f61c5538e1c1702ff7aadde635746e96fa51aefb.tar.gz
bibiman-f61c5538e1c1702ff7aadde635746e96fa51aefb.zip
Add expansion of ~ to home-dir
Diffstat (limited to 'src/app.rs')
-rw-r--r--src/app.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/app.rs b/src/app.rs
index cd27996..54d42b6 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -24,6 +24,7 @@ use crate::tui::popup::PopupKind;
use crate::tui::{self, Tui};
use crate::{bibiman::Bibiman, tui::commands::CmdAction};
use std::ffi::OsStr;
+use std::path::PathBuf;
use std::process::{Command, Stdio};
use tui::Event;
use tui_input::backend::crossterm::EventHandler;
@@ -335,6 +336,11 @@ pub fn open_connected_file(file: &OsStr) -> Result<()> {
}
};
+ // If necessary, replace ~ with /home dir
+ let file = PathBuf::from(file);
+
+ let file = expand_home(&file);
+
// Pass filepath as argument, pipe stdout and stderr to /dev/null
// to keep the TUI clean (where is it piped on Windows???)
let _ = Command::new(&cmd)
@@ -380,3 +386,33 @@ pub fn prepare_weblink(url: &str) -> String {
url.to_string()
}
}
+
+fn expand_home(path: &PathBuf) -> PathBuf {
+ // let path = PathBuf::from(path);
+ if path.starts_with("~") {
+ let mut home = dirs::home_dir().unwrap();
+ let path = path.strip_prefix("~").unwrap();
+ home.push(path);
+ home
+ } else {
+ path.into()
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+
+ #[test]
+ fn test_home_expansion() {
+ let path: PathBuf = "~/path/to/file.txt".into();
+
+ let path = expand_home(&path);
+
+ let home: String = dirs::home_dir().unwrap().to_str().unwrap().to_string();
+
+ let full_path = home + "/path/to/file.txt";
+
+ assert_eq!(path, PathBuf::from(full_path))
+ }
+}