yippie
This commit is contained in:
parent
a1f49ee90e
commit
636c02ce59
1 changed files with 42 additions and 27 deletions
69
src/main.rs
69
src/main.rs
|
|
@ -9,6 +9,8 @@ use ratatui::{
|
|||
widgets::{Block, Borders, List, ListState, Padding, StatefulWidget},
|
||||
};
|
||||
|
||||
type E = std::io::Error;
|
||||
|
||||
struct App<'a> {
|
||||
window: Window<'a>,
|
||||
exit: bool,
|
||||
|
|
@ -47,17 +49,20 @@ impl App<'_> {
|
|||
self.window.absolute_path.pop();
|
||||
self.window.update();
|
||||
}
|
||||
KeyCode::Right => match self.window.state.selected() {
|
||||
Some(val) => {
|
||||
self.window
|
||||
.absolute_path
|
||||
.push(self.window.prepare_updated_list()[val].clone());
|
||||
self.window.update();
|
||||
KeyCode::Right => {
|
||||
if let Some(val) = self.window.state.selected() {
|
||||
let current_dir = self
|
||||
.window
|
||||
.prepare_updated_list()
|
||||
.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<'_> {
|
||||
fn new(absolute_path: PathBuf) -> Self {
|
||||
Window {
|
||||
widget: WindowWidget::new(Self::prepare_list(&absolute_path)),
|
||||
fn new(absolute_path: PathBuf) -> Result<Self, E> {
|
||||
Ok(Window {
|
||||
widget: WindowWidget::new(Self::prepare_list(&absolute_path)?),
|
||||
absolute_path,
|
||||
state: ListState::default().with_selected(Some(0)),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn render(&mut self, area: Rect, buf: &mut Buffer) {
|
||||
self.widget.render(area, buf, &mut self.state);
|
||||
}
|
||||
|
||||
fn prepare_list(absolute_path: &PathBuf) -> Vec<String> {
|
||||
match fs::read_dir(absolute_path) {
|
||||
Ok(val) => {
|
||||
let mut the_list: Vec<String> = Vec::new();
|
||||
fn prepare_list(absolute_path: &PathBuf) -> Result<Vec<String>, E> {
|
||||
let entries = fs::read_dir(absolute_path)?;
|
||||
let mut prepared_list = Vec::<String>::new();
|
||||
|
||||
for item in val {
|
||||
the_list.push(item.unwrap().path().display().to_string());
|
||||
for item in entries {
|
||||
match item {
|
||||
Ok(entry) => {
|
||||
let path_string = entry.path().display().to_string();
|
||||
prepared_list.push(path_string);
|
||||
}
|
||||
|
||||
return the_list;
|
||||
}
|
||||
Err(err) => {}
|
||||
Err(err) => return Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
fn prepare_updated_list(&self) -> Vec<String> {
|
||||
Ok(prepared_list)
|
||||
}
|
||||
|
||||
fn prepare_updated_list(&self) -> Result<Vec<String>, E> {
|
||||
Self::prepare_list(&self.absolute_path)
|
||||
}
|
||||
|
||||
fn update(&mut self) {
|
||||
self.widget.update(self.prepare_updated_list());
|
||||
fn update(&mut self) -> bool {
|
||||
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<()> {
|
||||
let mut terminal = ratatui::init();
|
||||
let dir = Window::new(env::current_dir().unwrap());
|
||||
let dir = Window::new(env::current_dir()?)?;
|
||||
let app_result = App {
|
||||
window: dir,
|
||||
exit: false,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue