use crate::structured::RowOperation;
use super::{Row, path::PathAdapter, treez::Adapter};
#[derive(Clone)]
pub struct Document {
rows: Vec<Row>,
position: usize,
}
impl Document {
pub fn new(rows: Vec<Row>) -> Self {
Self { rows, position: 0 }
}
pub fn from_path(path: &std::path::Path) -> anyhow::Result<Self> {
Ok(Self::new(PathAdapter.create_rows(&path.to_path_buf())?))
}
}
impl Document {
pub fn rows(&self) -> &[Row] {
&self.rows
}
pub fn get(&self) -> Vec<String> {
self.rows
.get(self.position)
.map(|row| row.path.clone())
.unwrap_or_default()
}
pub fn extract_rows_from_current(&self, n: usize) -> Vec<Row> {
self.rows.extract(self.position, n)
}
pub fn toggle(&mut self) {
let index = self.rows.toggle(self.position);
self.position = index;
}
pub fn set_nodes_visibility(&mut self, collapsed: bool) {
self.rows.set_rows_visibility(collapsed);
self.position = self.rows.head();
}
pub fn up(&mut self) -> bool {
let index = self.rows.up(self.position);
let ret = index != self.position;
self.position = index;
ret
}
pub fn head(&mut self) -> bool {
self.position = self.rows.head();
true
}
pub fn down(&mut self) -> bool {
let index = self.rows.down(self.position);
let ret = index != self.position;
self.position = index;
ret
}
pub fn tail(&mut self) -> bool {
self.position = self.rows.tail();
true
}
}