From f61c5538e1c1702ff7aadde635746e96fa51aefb Mon Sep 17 00:00:00 2001 From: lukeflo Date: Sun, 1 Dec 2024 16:22:22 +0100 Subject: Add expansion of ~ to home-dir --- src/app.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src/app.rs') 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)) + } +} -- cgit v1.2.3