This commit is contained in:
2025-08-23 19:32:03 -05:00
parent a493adedd0
commit 251c5a07ce
2 changed files with 82 additions and 39 deletions
+25 -16
View File
@@ -1,7 +1,6 @@
use std::ops::Index;
use std::cell::RefCell;
use std::path::PathBuf;
use std::process::Command;
use rfd::MessageDialogResult::No;
use thiserror::Error;
use crate::fleen_app::FleenError::{RootDirNonexistenceError, RootDirPopulatedError};
use crate::fleen_app::TreeEntry::{CloseDir, Dir};
@@ -11,7 +10,9 @@ pub enum FleenError {
#[error("Can't reach root dir {0}")]
RootDirNonexistenceError(PathBuf),
#[error("Root dir is nonempty, you probably don't want to create an app here: {0}")]
RootDirPopulatedError(PathBuf)
RootDirPopulatedError(PathBuf),
#[error("Failed to open {0}: {1}")]
FileOpenError(String, String)
}
#[derive(Clone, Debug)]
@@ -21,16 +22,15 @@ pub enum TreeEntry {
CloseDir
}
#[derive(Clone)]
pub struct FleenApp {
root: PathBuf,
files_cache: Option<Vec<TreeEntry>>
files_cache: RefCell<Option<Vec<TreeEntry>>>
}
impl FleenApp {
pub fn open(root: PathBuf) -> Result<Self, FleenError> {
match root.try_exists() {
Ok(true) => Ok(Self { root, files_cache: None }),
Ok(true) => Ok(Self { root, files_cache: RefCell::new(None) }),
_ => Err(RootDirNonexistenceError(root))
}
}
@@ -41,7 +41,7 @@ impl FleenApp {
if iter.next().is_some() {
Err(RootDirPopulatedError(root))
} else {
Ok(Self { root, files_cache: None })
Ok(Self { root, files_cache: RefCell::new(None) })
}
}
Err(_) => {
@@ -50,14 +50,15 @@ impl FleenApp {
}
}
pub fn file_tree_entries(&mut self) -> impl IntoIterator<Item=TreeEntry> {
if self.files_cache.is_none() {
// TODO: This is panicky as hell, make it return a Result
fn refresh_file_cache(&self, force: bool) {
if self.files_cache.borrow().is_none() || force {
let mut entries = vec![];
fn visit_dir(dir: &PathBuf, entries: &mut Vec<TreeEntry>) {
for entry in dir.read_dir().unwrap() {
let path = entry.unwrap().path();
if path.is_file() {
if path.is_file() && !path.file_name().unwrap().to_str().unwrap().starts_with('.') {
entries.push(TreeEntry::File(path))
} else if path.is_dir() {
entries.push(Dir(path.clone()));
@@ -67,15 +68,23 @@ impl FleenApp {
}
}
entries.push(Dir(self.root.clone()));
visit_dir(&self.root, &mut entries);
self.files_cache = Some(entries)
entries.push(CloseDir);
self.files_cache.replace(Some(entries));
}
self.files_cache.as_ref().unwrap().clone()
}
pub fn open_file_at_index(&self, index: usize) {
if let TreeEntry::File(path) = &self.files_cache.as_ref().unwrap()[index] {
Command::new("open").arg(path).spawn();
}
pub fn file_tree_entries(&self) -> impl IntoIterator<Item=TreeEntry> {
self.refresh_file_cache(false);
self.files_cache.borrow().clone().expect("Can't happen because we just refreshed the cache")
}
pub fn open_filename(&self, filename: &String) -> Result<(), FleenError> {
Command::new("open").arg(filename.clone()).spawn().map_err(|err| {
FleenError::FileOpenError(filename.clone(), err.to_string())
})?;
Ok(())
}
}