4cd1f90d70
This is only getting pushed for reference' sake. I don't even know how functional this version is.
84 lines
2.1 KiB
Rust
84 lines
2.1 KiB
Rust
use std::io;
|
|
use std::sync::mpsc;
|
|
|
|
use ratatui::crossterm;
|
|
use ratatui::layout::{Constraint, Layout};
|
|
use ratatui::symbols::border;
|
|
use ratatui::widgets::{Block, Widget};
|
|
use ratatui::{DefaultTerminal, Frame};
|
|
|
|
#[derive(Debug, Default)]
|
|
pub struct Tui {
|
|
// bool::default() -> false
|
|
exit: bool,
|
|
}
|
|
|
|
impl Tui {
|
|
pub fn new() -> Self {
|
|
Self::default()
|
|
}
|
|
|
|
pub fn run(
|
|
&mut self,
|
|
terminal: &mut DefaultTerminal,
|
|
rx: mpsc::Receiver<Event>,
|
|
) -> io::Result<()> {
|
|
while !self.exit {
|
|
// Render frame.
|
|
terminal.draw(|frame| self.draw(frame))?;
|
|
|
|
// Event handler
|
|
// unwraps, bc what could go wrong?
|
|
match rx.recv().unwrap() {
|
|
Event::Input(key_event) => self.handle_key_event(key_event)?,
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn draw(&self, frame: &mut Frame) {
|
|
frame.render_widget(self, frame.area());
|
|
}
|
|
|
|
pub fn handle_key_event(&mut self, key_event: crossterm::event::KeyEvent) -> io::Result<()> {
|
|
if key_event.kind == crossterm::event::KeyEventKind::Press
|
|
&& key_event.code == crossterm::event::KeyCode::Char('q')
|
|
{
|
|
self.exit = true;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
// impl on reference to avoid accidentally mutating.
|
|
impl Widget for &Tui {
|
|
fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer)
|
|
where
|
|
Self: Sized,
|
|
{
|
|
let [main_area, _info_area] =
|
|
Layout::vertical([Constraint::Percentage(75), Constraint::Fill(1)]).areas(area);
|
|
|
|
let main_block = Block::bordered()
|
|
.title(" Viewer ")
|
|
.border_set(border::THICK);
|
|
main_block.render(main_area, buf);
|
|
}
|
|
}
|
|
|
|
pub enum Event {
|
|
Input(crossterm::event::KeyEvent),
|
|
}
|
|
|
|
pub fn input_fetcher(tx: mpsc::Sender<Event>) {
|
|
loop {
|
|
// unwraps, bc what could go wrong?
|
|
match crossterm::event::read().unwrap() {
|
|
crossterm::event::Event::Key(key_event) => tx.send(Event::Input(key_event)).unwrap(),
|
|
_ => (),
|
|
}
|
|
}
|
|
}
|