81 lines
1.9 KiB
Rust
81 lines
1.9 KiB
Rust
use std::io;
|
|
|
|
use ratatui::{
|
|
buffer::Buffer,
|
|
layout::{Alignment, Rect},
|
|
style::Stylize,
|
|
symbols::border,
|
|
text::Line,
|
|
widgets::{
|
|
block::{Title, Position},
|
|
Block, Paragraph, Widget},
|
|
DefaultTerminal,
|
|
Frame};
|
|
|
|
|
|
fn main() -> io::Result<()> {
|
|
let mut terminal = ratatui::init();
|
|
let app_result = App::default().run(&mut terminal);
|
|
ratatui::restore();
|
|
app_result
|
|
}
|
|
|
|
|
|
#[derive(Debug, Default)]
|
|
pub struct App {
|
|
counter: u8,
|
|
exit: bool,
|
|
}
|
|
|
|
impl App {
|
|
|
|
// runs application loop till users quits
|
|
pub fn run(&mut self, terminal: &mut DefaultTerminal) -> io::Result<()> {
|
|
while !self.exit {
|
|
terminal.draw(|frame| self.draw(frame))?;
|
|
self.handle_events()?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn draw(&self, frame: &mut Frame) {
|
|
frame.render_widget(self, frame.area());
|
|
}
|
|
|
|
fn handle_events(&mut self) -> io::Result<()> {
|
|
todo!()
|
|
}
|
|
}
|
|
|
|
impl Widget for &App {
|
|
fn render(self, area: Rect, buf: &mut Buffer) {
|
|
let title = Title::from(" Counter App Tutorial ".bold());
|
|
let instructions = Title::from(Line::from(vec![
|
|
" Decrement ".into(),
|
|
"<Left>".blue().bold(),
|
|
" Increment ".into(),
|
|
"<Right>".blue().bold(),
|
|
" Quit ".into(),
|
|
"<Q> ".blue().bold(),
|
|
]));
|
|
let block = Block::bordered()
|
|
.title(title.alignment(Alignment::Center))
|
|
.title(instructions
|
|
.alignment(Alignment::Center)
|
|
.position(Position::Bottom),
|
|
)
|
|
.border_set(border::THICK);
|
|
|
|
let counter_text = Text::from(vec![Line::from(vec![
|
|
"Value: ".into(),
|
|
self.counter.to_string().yellow(),
|
|
])]);
|
|
|
|
Paragraph::new(counter_text)
|
|
.centered()
|
|
.block(block)
|
|
.render(area, buf);
|
|
}
|
|
}
|
|
|