perch/src/model.rs
2025-03-24 03:37:36 +02:00

73 lines
1.7 KiB
Rust

use crate::*;
mod column; pub use self::column::*;
mod entry; pub use self::entry::*;
mod metadata; pub use self::metadata::*;
mod task; pub use self::task::*;
/// The application state.
pub struct Taggart {
pub _root: PathBuf,
pub entries: Vec<Entry>,
pub cursor: usize,
pub offset: usize,
pub column: usize,
pub columns: Columns<Entry, fn(&Entry)->Option<Arc<str>>, fn(&mut [Entry], usize, &str)>,
pub display: Measure<TuiOut>,
pub tasks: Vec<Task>,
pub mode: Option<Mode>,
}
#[derive(Debug)]
pub enum Mode {
Help,
Edit {
value: String,
index: usize,
},
Apply {
value: bool,
},
Unsaved {
value: u8
},
}
impl Taggart {
pub fn new (root: &impl AsRef<Path>, entries: Vec<Entry>) -> Usually<Self> {
Ok(Self {
_root: root.as_ref().into(),
cursor: 0,
offset: 0,
column: 2,
display: Measure::new(),
mode: None,
columns: Columns::default(),
tasks: vec![],
entries,
})
}
}
pub(crate) fn entries_under (
entries: &mut [Entry], index: usize
) -> Option<Vec<Arc<RwLock<Metadata>>>> {
let path = if let Some(Entry { path, info, .. }) = entries.get(index)
&& let Metadata::Directory { .. } = &*info.read().unwrap()
{
Some(path.clone())
} else {
None
};
if let Some(path) = path {
let mut others = vec![];
for other in entries.iter_mut() {
if other.path.starts_with(&path) && !other.is_directory() {
others.push(other.info.clone());
}
}
Some(others)
} else {
None
}
}