From 8f25fcaad13fc26e4f6a2f06b79cb867708492a6 Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Thu, 19 Aug 2021 20:10:58 +0200 Subject: [PATCH] improv(files): Preserve ~/ when tab-completing searches --- plugins/src/files/mod.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/plugins/src/files/mod.rs b/plugins/src/files/mod.rs index 01a0c55..6fbbc78 100644 --- a/plugins/src/files/mod.rs +++ b/plugins/src/files/mod.rs @@ -33,15 +33,17 @@ pub async fn main() { } pub struct App { + entries: BTreeMap>, + home: PathBuf, out: Unblock, search_results: Vec, - entries: BTreeMap>, } impl Default for App { fn default() -> Self { Self { entries: BTreeMap::default(), + home: std::env::home_dir().expect("no home dir"), out: async_stdout(), search_results: Vec::with_capacity(100), } @@ -58,12 +60,15 @@ impl App { pub async fn complete(&mut self, id: u32) { if let Some(selected) = self.search_results.get(id as usize) { - if let Some(string) = selected.path.to_str() { - let fill = if selected.path.is_dir() { - [string, "/"].concat() - } else { - string.to_owned() - }; + let path = match selected.path.strip_prefix(&self.home) { + Ok(path) => path, + Err(_) => &selected.path, + }; + + if let Some(string) = path.to_str() { + let prefix = if path.is_absolute() { "" } else { "~/" }; + let suffix = if path.is_dir() { "/" } else { "" }; + let fill = [prefix, string, suffix].concat(); crate::send(&mut self.out, PluginResponse::Fill(fill)).await; } @@ -72,7 +77,7 @@ impl App { pub async fn search(&mut self, query: String) { let path = if let Some(stripped) = query.strip_prefix("~/") { - std::env::home_dir().expect("no home dir").join(stripped) + self.home.join(stripped) } else { PathBuf::from(query) };