clide/src/tui/explorer.rs

71 lines
2.1 KiB
Rust
Raw Normal View History

2026-01-17 17:09:42 -05:00
use crate::tui::component::ClideComponent;
use anyhow::Result;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::prelude::Style;
use ratatui::widgets::{Block, Borders, Widget};
2026-01-17 17:09:42 -05:00
use std::fs;
use tui_tree_widget::{Tree, TreeItem};
use uuid::Uuid;
2026-01-17 17:09:42 -05:00
#[derive(Clone, Debug)]
pub struct Explorer<'a> {
root_path: &'a std::path::Path,
2026-01-17 17:09:42 -05:00
tree_items: TreeItem<'a, String>,
}
impl<'a> Explorer<'a> {
pub fn new(path: &'a std::path::Path) -> Self {
2026-01-17 17:09:42 -05:00
let mut explorer = Explorer {
root_path: path,
tree_items: Self::build_tree_from_path(path.into()),
};
explorer
}
2026-01-17 17:09:42 -05:00
pub fn draw(&self, area: Rect, buf: &mut Buffer) {}
fn build_tree_from_path(path: std::path::PathBuf) -> TreeItem<'static, String> {
let mut children = vec![];
if let Ok(entries) = fs::read_dir(&path) {
let mut paths = entries
.map(|res| res.map(|e| e.path()))
.collect::<Result<Vec<_>, std::io::Error>>()
.expect("");
paths.sort();
for path in paths {
if path.is_dir() {
children.push(Self::build_tree_from_path(path));
} else {
children.push(TreeItem::new_leaf(
Uuid::new_v4().to_string(),
path.file_name().unwrap().to_string_lossy().to_string(),
));
}
}
}
TreeItem::new(
Uuid::new_v4().to_string(),
path.file_name()
.unwrap_or_default()
.to_string_lossy()
.to_string(),
children,
)
2026-01-17 17:09:42 -05:00
.expect("Failed to build tree from path.")
}
}
2026-01-17 17:09:42 -05:00
impl<'a> Widget for &Explorer<'a> {
fn render(self, area: Rect, buf: &mut Buffer) {
Tree::new(&self.tree_items.children())
.expect("Failed to build tree.")
.style(Style::default())
.block(Block::default().borders(Borders::ALL))
.render(area, buf);
}
}
impl<'a> ClideComponent for Explorer<'a> {}