improv(files): Cache items in a directory
This commit is contained in:
parent
d29268d8be
commit
9c69310e09
1 changed files with 42 additions and 29 deletions
|
|
@ -1,8 +1,9 @@
|
||||||
use futures_lite::prelude::*;
|
use futures_lite::prelude::*;
|
||||||
use pop_launcher::*;
|
use pop_launcher::*;
|
||||||
use smol::Unblock;
|
use smol::Unblock;
|
||||||
use std::{borrow::Cow, io, path::PathBuf};
|
use std::{borrow::Cow, collections::BTreeMap, io, path::PathBuf};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
struct Item {
|
struct Item {
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
name: String,
|
name: String,
|
||||||
|
|
@ -34,11 +35,13 @@ pub async fn main() {
|
||||||
pub struct App {
|
pub struct App {
|
||||||
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(),
|
||||||
out: async_stdout(),
|
out: async_stdout(),
|
||||||
search_results: Vec::with_capacity(100),
|
search_results: Vec::with_capacity(100),
|
||||||
}
|
}
|
||||||
|
|
@ -93,37 +96,47 @@ impl App {
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(parent) = search_path {
|
if let Some(parent) = search_path {
|
||||||
if let Ok(dir) = parent.read_dir() {
|
let items = self.entries.entry(parent.to_owned()).or_insert_with(|| {
|
||||||
for entry in dir.filter_map(Result::ok) {
|
let mut items = Vec::new();
|
||||||
let path = entry.path();
|
if let Ok(dir) = parent.read_dir() {
|
||||||
if let Some(name) = path.file_name().and_then(|x| x.to_str()) {
|
for entry in dir.filter_map(Result::ok) {
|
||||||
if !show_hidden && name.starts_with('.') {
|
let path = entry.path();
|
||||||
continue;
|
if let Some(name) = path.file_name().and_then(|x| x.to_str()) {
|
||||||
|
items.push(Item {
|
||||||
|
icon: IconSource::Mime(if path.is_dir() {
|
||||||
|
Cow::Borrowed("inode/directory")
|
||||||
|
} else if let Some(guess) = new_mime_guess::from_path(&path).first()
|
||||||
|
{
|
||||||
|
Cow::Owned(guess.essence_str().to_owned())
|
||||||
|
} else {
|
||||||
|
Cow::Borrowed("text/plain")
|
||||||
|
}),
|
||||||
|
name: name.to_owned(),
|
||||||
|
description: path
|
||||||
|
.metadata()
|
||||||
|
.ok()
|
||||||
|
.map(|meta| {
|
||||||
|
human_format::Formatter::new()
|
||||||
|
.with_scales(human_format::Scales::Binary())
|
||||||
|
.with_units("B")
|
||||||
|
.format(meta.len() as f64)
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| String::from("N/A")),
|
||||||
|
path,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
self.search_results.push(Item {
|
|
||||||
icon: IconSource::Mime(if path.is_dir() {
|
|
||||||
Cow::Borrowed("inode/directory")
|
|
||||||
} else if let Some(guess) = new_mime_guess::from_path(&path).first() {
|
|
||||||
Cow::Owned(guess.essence_str().to_owned())
|
|
||||||
} else {
|
|
||||||
Cow::Borrowed("text/plain")
|
|
||||||
}),
|
|
||||||
name: name.to_owned(),
|
|
||||||
description: path
|
|
||||||
.metadata()
|
|
||||||
.ok()
|
|
||||||
.map(|meta| {
|
|
||||||
human_format::Formatter::new()
|
|
||||||
.with_scales(human_format::Scales::Binary())
|
|
||||||
.with_units("B")
|
|
||||||
.format(meta.len() as f64)
|
|
||||||
})
|
|
||||||
.unwrap_or_else(|| String::from("N/A")),
|
|
||||||
path,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
items
|
||||||
|
});
|
||||||
|
|
||||||
|
for item in items {
|
||||||
|
if !show_hidden && item.name.starts_with('.') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.search_results.push(item.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue