This commit is contained in:
benstrb 2026-01-24 14:30:12 +01:00
parent a1f49ee90e
commit 636c02ce59

View file

@ -9,6 +9,8 @@ use ratatui::{
widgets::{Block, Borders, List, ListState, Padding, StatefulWidget}, widgets::{Block, Borders, List, ListState, Padding, StatefulWidget},
}; };
type E = std::io::Error;
struct App<'a> { struct App<'a> {
window: Window<'a>, window: Window<'a>,
exit: bool, exit: bool,
@ -47,17 +49,20 @@ impl App<'_> {
self.window.absolute_path.pop(); self.window.absolute_path.pop();
self.window.update(); self.window.update();
} }
KeyCode::Right => match self.window.state.selected() { KeyCode::Right => {
Some(val) => { if let Some(val) = self.window.state.selected() {
self.window let current_dir = self
.absolute_path .window
.push(self.window.prepare_updated_list()[val].clone()); .prepare_updated_list()
self.window.update(); .expect("You should always be in a valid directory");
self.window.absolute_path.push(current_dir[val].clone());
let output = self.window.update();
if !output {
self.window.absolute_path.pop();
}
} }
None => { }
print!("hello");
}
},
_ => {} _ => {}
} }
} }
@ -74,39 +79,49 @@ struct Window<'a> {
} }
impl Window<'_> { impl Window<'_> {
fn new(absolute_path: PathBuf) -> Self { fn new(absolute_path: PathBuf) -> Result<Self, E> {
Window { Ok(Window {
widget: WindowWidget::new(Self::prepare_list(&absolute_path)), widget: WindowWidget::new(Self::prepare_list(&absolute_path)?),
absolute_path, absolute_path,
state: ListState::default().with_selected(Some(0)), state: ListState::default().with_selected(Some(0)),
} })
} }
fn render(&mut self, area: Rect, buf: &mut Buffer) { fn render(&mut self, area: Rect, buf: &mut Buffer) {
self.widget.render(area, buf, &mut self.state); self.widget.render(area, buf, &mut self.state);
} }
fn prepare_list(absolute_path: &PathBuf) -> Vec<String> { fn prepare_list(absolute_path: &PathBuf) -> Result<Vec<String>, E> {
match fs::read_dir(absolute_path) { let entries = fs::read_dir(absolute_path)?;
Ok(val) => { let mut prepared_list = Vec::<String>::new();
let mut the_list: Vec<String> = Vec::new();
for item in val { for item in entries {
the_list.push(item.unwrap().path().display().to_string()); match item {
Ok(entry) => {
let path_string = entry.path().display().to_string();
prepared_list.push(path_string);
} }
Err(err) => return Err(err),
return the_list;
} }
Err(err) => {}
} }
Ok(prepared_list)
} }
fn prepare_updated_list(&self) -> Vec<String> { fn prepare_updated_list(&self) -> Result<Vec<String>, E> {
Self::prepare_list(&self.absolute_path) Self::prepare_list(&self.absolute_path)
} }
fn update(&mut self) { fn update(&mut self) -> bool {
self.widget.update(self.prepare_updated_list()); let values = self.prepare_updated_list();
if let Ok(val) = values {
self.widget.update(val);
self.state.select_first();
return true;
}
false
} }
} }
@ -146,7 +161,7 @@ impl StatefulWidget for &mut WindowWidget<'_> {
fn main() -> io::Result<()> { fn main() -> io::Result<()> {
let mut terminal = ratatui::init(); let mut terminal = ratatui::init();
let dir = Window::new(env::current_dir().unwrap()); let dir = Window::new(env::current_dir()?)?;
let app_result = App { let app_result = App {
window: dir, window: dir,
exit: false, exit: false,