2023-06-12 05:07:15 +00:00
|
|
|
use std::{error::Error, io};
|
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
use crossterm::{
|
|
|
|
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
|
|
|
|
execute,
|
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
|
|
};
|
2023-07-16 09:11:59 +00:00
|
|
|
use ratatui::{prelude::*, widgets::*};
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2021-12-23 17:46:25 +00:00
|
|
|
#[derive(Default)]
|
2016-11-02 18:17:18 +00:00
|
|
|
struct Label<'a> {
|
|
|
|
text: &'a str,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Widget for Label<'a> {
|
2019-12-15 20:38:18 +00:00
|
|
|
fn render(self, area: Rect, buf: &mut Buffer) {
|
2018-09-07 20:24:52 +00:00
|
|
|
buf.set_string(area.left(), area.top(), self.text, Style::default());
|
2016-11-02 18:17:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Label<'a> {
|
2019-12-15 20:38:18 +00:00
|
|
|
fn text(mut self, text: &'a str) -> Label<'a> {
|
2016-11-02 18:17:18 +00:00
|
|
|
self.text = text;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 01:02:51 +00:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
2021-11-01 21:27:46 +00:00
|
|
|
// setup terminal
|
|
|
|
enable_raw_mode()?;
|
|
|
|
let mut stdout = io::stdout();
|
|
|
|
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
|
|
|
|
let backend = CrosstermBackend::new(stdout);
|
2018-09-23 18:59:51 +00:00
|
|
|
let mut terminal = Terminal::new(backend)?;
|
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
// create app and run it
|
|
|
|
let res = run_app(&mut terminal);
|
|
|
|
|
|
|
|
// restore terminal
|
|
|
|
disable_raw_mode()?;
|
|
|
|
execute!(
|
|
|
|
terminal.backend_mut(),
|
|
|
|
LeaveAlternateScreen,
|
|
|
|
DisableMouseCapture
|
|
|
|
)?;
|
|
|
|
terminal.show_cursor()?;
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
if let Err(err) = res {
|
2023-05-22 03:46:02 +00:00
|
|
|
println!("{err:?}");
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> io::Result<()> {
|
2018-09-23 18:59:51 +00:00
|
|
|
loop {
|
2021-11-01 21:27:46 +00:00
|
|
|
terminal.draw(ui)?;
|
|
|
|
|
|
|
|
if let Event::Key(key) = event::read()? {
|
2021-12-23 17:46:25 +00:00
|
|
|
if let KeyCode::Char('q') = key.code {
|
|
|
|
return Ok(());
|
2018-12-07 15:17:33 +00:00
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
fn ui<B: Backend>(f: &mut Frame<B>) {
|
|
|
|
let size = f.size();
|
|
|
|
let label = Label::default().text("Test");
|
|
|
|
f.render_widget(label, size);
|
2016-11-02 18:17:18 +00:00
|
|
|
}
|