the source code is awful, but works ig?
This commit is contained in:
parent
a11c853267
commit
2153bfef4c
1 changed files with 66 additions and 15 deletions
81
src/main.rs
81
src/main.rs
|
|
@ -36,6 +36,7 @@ impl App<'_> {
|
||||||
.constraints(vec![Constraint::Percentage(50), Constraint::Percentage(50)])
|
.constraints(vec![Constraint::Percentage(50), Constraint::Percentage(50)])
|
||||||
.split(frame.area());
|
.split(frame.area());
|
||||||
self.window.render(main_area[0], frame.buffer_mut());
|
self.window.render(main_area[0], frame.buffer_mut());
|
||||||
|
self.preview.see(&self.window);
|
||||||
self.preview.render(main_area[1], frame.buffer_mut());
|
self.preview.render(main_area[1], frame.buffer_mut());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -197,9 +198,11 @@ impl Preview<'_> {
|
||||||
self.widget.update(everything_went_well);
|
self.widget.update(everything_went_well);
|
||||||
}
|
}
|
||||||
Err(err) => match err.kind() {
|
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::PermissionDenied => self.widget.permission_denied(),
|
||||||
ErrorKind::NotFound => {}
|
ErrorKind::NotFound => self.widget.not_found(),
|
||||||
_ => panic!("It's a different kind of error:\n{err:?}"),
|
_ => panic!("It's a different kind of error:\n{err:?}"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
@ -221,9 +224,7 @@ impl PreviewWidget<'_> {
|
||||||
.padding(Padding::symmetric(1, 1))
|
.padding(Padding::symmetric(1, 1))
|
||||||
.fg(Color::Blue);
|
.fg(Color::Blue);
|
||||||
|
|
||||||
self.list = List::new(prepared_list)
|
self.list = List::new(prepared_list).block(block)
|
||||||
.block(block)
|
|
||||||
.highlight_style(Color::Cyan);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new(prepared_list: Vec<String>) -> Self {
|
fn new(prepared_list: Vec<String>) -> Self {
|
||||||
|
|
@ -235,13 +236,41 @@ impl PreviewWidget<'_> {
|
||||||
preview_widget
|
preview_widget
|
||||||
}
|
}
|
||||||
|
|
||||||
fn not_a_dir(&mut self) {
|
fn not_a_dir(&mut self, absolute_path: &PathBuf) {
|
||||||
let block = Block::new()
|
match absolute_path.is_file() {
|
||||||
.borders(Borders::ALL)
|
true => {
|
||||||
.padding(Padding::symmetric(1, 1))
|
let block = Block::new()
|
||||||
.fg(Color::Blue);
|
.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) {
|
fn permission_denied(&mut self) {
|
||||||
|
|
@ -250,10 +279,33 @@ impl PreviewWidget<'_> {
|
||||||
.padding(Padding::symmetric(1, 1))
|
.padding(Padding::symmetric(1, 1))
|
||||||
.fg(Color::Blue);
|
.fg(Color::Blue);
|
||||||
|
|
||||||
self.list = List::new(vec!["permission denied"])
|
self.list = List::new(vec!["Permission Denied"])
|
||||||
.block(block)
|
.block(block)
|
||||||
.style(Color::Red)
|
.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<'_> {
|
impl StatefulWidget for &mut PreviewWidget<'_> {
|
||||||
|
|
@ -267,11 +319,10 @@ impl StatefulWidget for &mut PreviewWidget<'_> {
|
||||||
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()?)?;
|
let dir = Window::new(env::current_dir()?)?;
|
||||||
|
let files = PreviewWidget::new(Window::prepare_list(&env::current_dir()?)?);
|
||||||
let app_result = App {
|
let app_result = App {
|
||||||
window: dir,
|
window: dir,
|
||||||
preview: Preview {
|
preview: Preview { widget: files },
|
||||||
widget: PreviewWidget::new(Window::prepare_list(&env::current_dir().unwrap()).unwrap()),
|
|
||||||
},
|
|
||||||
exit: false,
|
exit: false,
|
||||||
}
|
}
|
||||||
.run(&mut terminal);
|
.run(&mut terminal);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue