ratatui-demo/src/app.rs
Jakub Stachurski a47c45a537
initial commit
2025-08-14 23:36:57 +02:00

103 lines
3.6 KiB
Rust

use std::f64::consts::PI;
use ratatui::{layout::Rect, style::{Style, Stylize}, text::{Line, Span}, widgets::{Block, Paragraph}};
use terminput::KeyCode;
use crate::Demo;
pub(crate) struct MyApp {
counter: usize,
frame_counter: usize,
}
impl Demo for MyApp {
fn new() -> Self {
Self {
counter: 0,
frame_counter: 0,
}
}
async fn frame(&mut self, term: &mut crate::Term) -> Result<(), String> {
term.draw(|fr| {
self.frame_counter = self.frame_counter.wrapping_add(1);
let style = Style::new().light_yellow().on_black();
let block = Block::bordered()
.style(style)
.title("Example demo")
.title_bottom("Sine - made by: wilkuu (Jakub Stachurski), jakub@snt.utwente.nl");
let area = fr.area();
let barea = block.inner(area);
fr.render_widget(block, area);
let xl = barea.x;
let xr = barea.width + barea.x;
let yt = barea.y + barea.height;
let w1 = PI * 2.0 / 100.0;
let w2 = PI * 2.0 / 300.0;
let buf = fr.buffer_mut();
for x in xl..xr {
let p1 = self.frame_counter as f64 / 100.0;
let p2 = self.frame_counter as f64 / 50.0;
let y1: u16 = ((
((w1 * Into::<f64>::into(x)) + p1 as f64).sin() + 1.0)
* Into::<f64>::into(barea.height / 2-1) + Into::<f64>::into(barea.y)).round() as u16;
let y2: u16 = ((
((w2 * Into::<f64>::into(x)) + p2 as f64).sin() + 1.0)
* Into::<f64>::into(barea.height / 2-1) + Into::<f64>::into(barea.y)).round() as u16;
if y2 == y1 {
buf.set_span(x, (y1.saturating_sub(1)).max(barea.y), &Span::from("_").light_magenta(), 1);
buf.set_span(x, y1, &Span::from("#").light_magenta(), 1);
buf.set_span(x, (y1.saturating_add(1)).min(yt), &Span::from("-").light_magenta(), 1);
} else {
buf.set_span(x, (y1.saturating_sub(1)).max(barea.y), &Span::from("_").red(), 1);
buf.set_span(x, y1, &Span::from("#").red(), 1);
buf.set_span(x, (y1.saturating_add(1)).min(yt), &Span::from("-").red(), 1);
buf.set_span(x, (y2.saturating_sub(1)).max(barea.y), &Span::from("_").blue(), 1);
buf.set_span(x, y2, &Span::from("#").blue(), 1);
buf.set_span(x, (y2.saturating_add(1)).min(yt), &Span::from("-").blue(), 1);
}
}
let mut lines: Vec<Line>= Vec::new();
lines.push("Use W/S to change the counter. Enter to exit.".into());
lines.push(format!("Counter: {}", self.counter).into());
let text = Paragraph::new(lines).centered().style(style);
fr.render_widget(text, Rect::new(barea.width/2 - 25, barea.height/2, 50, 4));
}).map_err(|e| e.to_string())?;
Ok(())
}
async fn input(&mut self, event: terminput::Event) -> Result<(), String> {
if let terminput::Event::Key(kv) = event {
match kv.code {
KeyCode::Char('w') => {
self.counter = self.counter.wrapping_add(1);
},
KeyCode::Char('s') => {
self.counter = self.counter.saturating_sub(1);
},
_=> {}
}
}
Ok(())
}
async fn disconnected(&mut self) {
()
}
}