improv(files): Preserve ~/ when tab-completing searches
This commit is contained in:
parent
78704bba85
commit
8f25fcaad1
1 changed files with 13 additions and 8 deletions
|
|
@ -33,15 +33,17 @@ pub async fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
|
entries: BTreeMap<PathBuf, Vec<Item>>,
|
||||||
|
home: PathBuf,
|
||||||
out: Unblock<io::Stdout>,
|
out: Unblock<io::Stdout>,
|
||||||
search_results: Vec<Item>,
|
search_results: Vec<Item>,
|
||||||
entries: BTreeMap<PathBuf, Vec<Item>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for App {
|
impl Default for App {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
entries: BTreeMap::default(),
|
entries: BTreeMap::default(),
|
||||||
|
home: std::env::home_dir().expect("no home dir"),
|
||||||
out: async_stdout(),
|
out: async_stdout(),
|
||||||
search_results: Vec::with_capacity(100),
|
search_results: Vec::with_capacity(100),
|
||||||
}
|
}
|
||||||
|
|
@ -58,12 +60,15 @@ impl App {
|
||||||
|
|
||||||
pub async fn complete(&mut self, id: u32) {
|
pub async fn complete(&mut self, id: u32) {
|
||||||
if let Some(selected) = self.search_results.get(id as usize) {
|
if let Some(selected) = self.search_results.get(id as usize) {
|
||||||
if let Some(string) = selected.path.to_str() {
|
let path = match selected.path.strip_prefix(&self.home) {
|
||||||
let fill = if selected.path.is_dir() {
|
Ok(path) => path,
|
||||||
[string, "/"].concat()
|
Err(_) => &selected.path,
|
||||||
} else {
|
};
|
||||||
string.to_owned()
|
|
||||||
};
|
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;
|
crate::send(&mut self.out, PluginResponse::Fill(fill)).await;
|
||||||
}
|
}
|
||||||
|
|
@ -72,7 +77,7 @@ impl App {
|
||||||
|
|
||||||
pub async fn search(&mut self, query: String) {
|
pub async fn search(&mut self, query: String) {
|
||||||
let path = if let Some(stripped) = query.strip_prefix("~/") {
|
let path = if let Some(stripped) = query.strip_prefix("~/") {
|
||||||
std::env::home_dir().expect("no home dir").join(stripped)
|
self.home.join(stripped)
|
||||||
} else {
|
} else {
|
||||||
PathBuf::from(query)
|
PathBuf::from(query)
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue