mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-14 17:07:13 +00:00
082cbcbc50
This change simplifys UI code that uses the Frame type. E.g.: ```rust fn draw<B: Backend>(frame: &mut Frame<B>) { // ... } ``` Frame was generic over Backend because it stored a reference to the terminal in the field. Instead it now directly stores the viewport area and current buffer. These are provided at creation time and are valid for the duration of the frame. BREAKING CHANGE: Frame is no longer generic over Backend. Code that accepted `Frame<Backend>` will now need to accept `Frame`. To migrate existing code, remove any generic parameters from code that uses an instance of a Frame. E.g. the above code becomes: ```rust fn draw(frame: &mut Frame) { // ... } ```
169 lines
4.3 KiB
Rust
169 lines
4.3 KiB
Rust
use std::{
|
|
error::Error,
|
|
io,
|
|
time::{Duration, Instant},
|
|
};
|
|
|
|
use crossterm::{
|
|
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
|
|
execute,
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
};
|
|
use rand::{
|
|
distributions::{Distribution, Uniform},
|
|
rngs::ThreadRng,
|
|
};
|
|
use ratatui::{prelude::*, widgets::*};
|
|
|
|
#[derive(Clone)]
|
|
pub struct RandomSignal {
|
|
distribution: Uniform<u64>,
|
|
rng: ThreadRng,
|
|
}
|
|
|
|
impl RandomSignal {
|
|
pub fn new(lower: u64, upper: u64) -> RandomSignal {
|
|
RandomSignal {
|
|
distribution: Uniform::new(lower, upper),
|
|
rng: rand::thread_rng(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Iterator for RandomSignal {
|
|
type Item = u64;
|
|
fn next(&mut self) -> Option<u64> {
|
|
Some(self.distribution.sample(&mut self.rng))
|
|
}
|
|
}
|
|
|
|
struct App {
|
|
signal: RandomSignal,
|
|
data1: Vec<u64>,
|
|
data2: Vec<u64>,
|
|
data3: Vec<u64>,
|
|
}
|
|
|
|
impl App {
|
|
fn new() -> App {
|
|
let mut signal = RandomSignal::new(0, 100);
|
|
let data1 = signal.by_ref().take(200).collect::<Vec<u64>>();
|
|
let data2 = signal.by_ref().take(200).collect::<Vec<u64>>();
|
|
let data3 = signal.by_ref().take(200).collect::<Vec<u64>>();
|
|
App {
|
|
signal,
|
|
data1,
|
|
data2,
|
|
data3,
|
|
}
|
|
}
|
|
|
|
fn on_tick(&mut self) {
|
|
let value = self.signal.next().unwrap();
|
|
self.data1.pop();
|
|
self.data1.insert(0, value);
|
|
let value = self.signal.next().unwrap();
|
|
self.data2.pop();
|
|
self.data2.insert(0, value);
|
|
let value = self.signal.next().unwrap();
|
|
self.data3.pop();
|
|
self.data3.insert(0, value);
|
|
}
|
|
}
|
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
// setup terminal
|
|
enable_raw_mode()?;
|
|
let mut stdout = io::stdout();
|
|
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
|
|
let backend = CrosstermBackend::new(stdout);
|
|
let mut terminal = Terminal::new(backend)?;
|
|
|
|
// create app and run it
|
|
let tick_rate = Duration::from_millis(250);
|
|
let app = App::new();
|
|
let res = run_app(&mut terminal, app, tick_rate);
|
|
|
|
// restore terminal
|
|
disable_raw_mode()?;
|
|
execute!(
|
|
terminal.backend_mut(),
|
|
LeaveAlternateScreen,
|
|
DisableMouseCapture
|
|
)?;
|
|
terminal.show_cursor()?;
|
|
|
|
if let Err(err) = res {
|
|
println!("{err:?}");
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn run_app<B: Backend>(
|
|
terminal: &mut Terminal<B>,
|
|
mut app: App,
|
|
tick_rate: Duration,
|
|
) -> io::Result<()> {
|
|
let mut last_tick = Instant::now();
|
|
loop {
|
|
terminal.draw(|f| ui(f, &app))?;
|
|
|
|
let timeout = tick_rate
|
|
.checked_sub(last_tick.elapsed())
|
|
.unwrap_or_else(|| Duration::from_secs(0));
|
|
if crossterm::event::poll(timeout)? {
|
|
if let Event::Key(key) = event::read()? {
|
|
if let KeyCode::Char('q') = key.code {
|
|
return Ok(());
|
|
}
|
|
}
|
|
}
|
|
if last_tick.elapsed() >= tick_rate {
|
|
app.on_tick();
|
|
last_tick = Instant::now();
|
|
}
|
|
}
|
|
}
|
|
|
|
fn ui(f: &mut Frame, app: &App) {
|
|
let chunks = Layout::default()
|
|
.direction(Direction::Vertical)
|
|
.constraints(
|
|
[
|
|
Constraint::Length(3),
|
|
Constraint::Length(3),
|
|
Constraint::Min(0),
|
|
]
|
|
.as_ref(),
|
|
)
|
|
.split(f.size());
|
|
let sparkline = Sparkline::default()
|
|
.block(
|
|
Block::default()
|
|
.title("Data1")
|
|
.borders(Borders::LEFT | Borders::RIGHT),
|
|
)
|
|
.data(&app.data1)
|
|
.style(Style::default().fg(Color::Yellow));
|
|
f.render_widget(sparkline, chunks[0]);
|
|
let sparkline = Sparkline::default()
|
|
.block(
|
|
Block::default()
|
|
.title("Data2")
|
|
.borders(Borders::LEFT | Borders::RIGHT),
|
|
)
|
|
.data(&app.data2)
|
|
.style(Style::default().bg(Color::Green));
|
|
f.render_widget(sparkline, chunks[1]);
|
|
// Multiline
|
|
let sparkline = Sparkline::default()
|
|
.block(
|
|
Block::default()
|
|
.title("Data3")
|
|
.borders(Borders::LEFT | Borders::RIGHT),
|
|
)
|
|
.data(&app.data3)
|
|
.style(Style::default().fg(Color::Red));
|
|
f.render_widget(sparkline, chunks[2]);
|
|
}
|