diff --git a/src/main.rs b/src/main.rs index 385f2d0..cdfece6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,6 +36,7 @@ impl App<'_> { .constraints(vec![Constraint::Percentage(50), Constraint::Percentage(50)]) .split(frame.area()); self.window.render(main_area[0], frame.buffer_mut()); + self.preview.see(&self.window); self.preview.render(main_area[1], frame.buffer_mut()); } @@ -197,9 +198,11 @@ impl Preview<'_> { self.widget.update(everything_went_well); } Err(err) => match err.kind() { - ErrorKind::NotADirectory => self.widget.not_a_dir(), + ErrorKind::NotADirectory => { + self.widget.not_a_dir(&PathBuf::from(&prepared_list[state])) + } ErrorKind::PermissionDenied => self.widget.permission_denied(), - ErrorKind::NotFound => {} + ErrorKind::NotFound => self.widget.not_found(), _ => panic!("It's a different kind of error:\n{err:?}"), }, } @@ -221,9 +224,7 @@ impl PreviewWidget<'_> { .padding(Padding::symmetric(1, 1)) .fg(Color::Blue); - self.list = List::new(prepared_list) - .block(block) - .highlight_style(Color::Cyan); + self.list = List::new(prepared_list).block(block) } fn new(prepared_list: Vec) -> Self { @@ -235,13 +236,41 @@ impl PreviewWidget<'_> { preview_widget } - fn not_a_dir(&mut self) { - let block = Block::new() - .borders(Borders::ALL) - .padding(Padding::symmetric(1, 1)) - .fg(Color::Blue); + fn not_a_dir(&mut self, absolute_path: &PathBuf) { + match absolute_path.is_file() { + true => { + let block = Block::new() + .borders(Borders::ALL) + .padding(Padding::symmetric(1, 1)) + .fg(Color::Blue); - self.list = List::default().block(block) + match fs::read_to_string(absolute_path) { + Ok(contents) => { + self.list = List::new(vec![contents]).block(block); + } + Err(err) => match err.kind() { + ErrorKind::FileTooLarge => {} + ErrorKind::InvalidData => { + self.invalid_data(); + } + ErrorKind::PermissionDenied => self.permission_denied(), + _ => panic!("Problem with reading the contents of the file:\n{err:?}"), + }, + } + } + false => { + let block = Block::new() + .borders(Borders::ALL) + .padding(Padding::symmetric(1, 1)) + .fg(Color::Blue); + + self.list = List::new(vec![ + "This is neither a directory or a file\nI have no idea what to do with this", + ]) + .block(block) + .style(Color::Red) + } + } } fn permission_denied(&mut self) { @@ -250,10 +279,33 @@ impl PreviewWidget<'_> { .padding(Padding::symmetric(1, 1)) .fg(Color::Blue); - self.list = List::new(vec!["permission denied"]) + self.list = List::new(vec!["Permission Denied"]) .block(block) .style(Color::Red) } + + fn invalid_data(&mut self) { + let block = Block::new() + .borders(Borders::ALL) + .padding(Padding::symmetric(1, 1)) + .fg(Color::Blue); + + self.list = List::new(vec!["You're trying to read an invalid kind of data"]) + .block(block) + .style(Color::Red) + } + + fn not_found(&mut self) { + let block = Block::new() + .borders(Borders::ALL) + .padding(Padding::symmetric(1, 1)) + .fg(Color::Blue); + + self.list = List::new(vec!["How the fuck..."]) + .block(block) + .style(Color::Red) + .italic() + } } impl StatefulWidget for &mut PreviewWidget<'_> { @@ -267,11 +319,10 @@ impl StatefulWidget for &mut PreviewWidget<'_> { fn main() -> io::Result<()> { let mut terminal = ratatui::init(); let dir = Window::new(env::current_dir()?)?; + let files = PreviewWidget::new(Window::prepare_list(&env::current_dir()?)?); let app_result = App { window: dir, - preview: Preview { - widget: PreviewWidget::new(Window::prepare_list(&env::current_dir().unwrap()).unwrap()), - }, + preview: Preview { widget: files }, exit: false, } .run(&mut terminal);