This commit is contained in:
Jakub Stachurski 2025-08-14 23:47:31 +02:00
parent a47c45a537
commit c93ae13ea4
No known key found for this signature in database

View file

@ -5,10 +5,11 @@ use terminput::KeyCode;
use crate::Demo; use crate::Demo;
pub(crate) struct MyApp { pub(crate) struct MyApp {
// Put in any variables here
counter: usize, counter: usize,
frame_counter: usize, frame_counter: usize,
} }
// Implement the [`crate::Demo`] trait to outline the behaviour
impl Demo for MyApp { impl Demo for MyApp {
fn new() -> Self { fn new() -> Self {
Self { Self {
@ -18,20 +19,30 @@ impl Demo for MyApp {
} }
async fn frame(&mut self, term: &mut crate::Term) -> Result<(), String> { async fn frame(&mut self, term: &mut crate::Term) -> Result<(), String> {
// This example draws two moving sine waves on the screen.
// Put in your behaviour here
// Draw function
term.draw(|fr| { term.draw(|fr| {
self.frame_counter = self.frame_counter.wrapping_add(1); self.frame_counter = self.frame_counter.wrapping_add(1);
let style = Style::new().light_yellow().on_black();
// Color and the draw area
let style = Style::new().light_yellow().on_black();
let area = fr.area();
// Create a block with title and credits
let block = Block::bordered() let block = Block::bordered()
.style(style) .style(style)
.title("Example demo") .title("Example demo: Sine")
.title_bottom("Sine - made by: wilkuu (Jakub Stachurski), jakub@snt.utwente.nl"); .title_bottom("By: wilkuu (Jakub Stachurski), jakub@snt.utwente.nl");
let area = fr.area(); // Area inside of the block
let barea = block.inner(area); let barea = block.inner(area);
// Draw the block
fr.render_widget(block, area); fr.render_widget(block, area);
// Demo drawing logic replace with own stuff
let xl = barea.x; let xl = barea.x;
let xr = barea.width + barea.x; let xr = barea.width + barea.x;
@ -54,6 +65,7 @@ impl Demo for MyApp {
* Into::<f64>::into(barea.height / 2-1) + Into::<f64>::into(barea.y)).round() as u16; * Into::<f64>::into(barea.height / 2-1) + Into::<f64>::into(barea.y)).round() as u16;
if y2 == y1 { if y2 == y1 {
// Individual character drawing, there is probably a better way to achieve this.
buf.set_span(x, (y1.saturating_sub(1)).max(barea.y), &Span::from("_").light_magenta(), 1); 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, &Span::from("#").light_magenta(), 1);
buf.set_span(x, (y1.saturating_add(1)).min(yt), &Span::from("-").light_magenta(), 1); buf.set_span(x, (y1.saturating_add(1)).min(yt), &Span::from("-").light_magenta(), 1);
@ -68,6 +80,7 @@ impl Demo for MyApp {
} }
} }
// Center text
let mut lines: Vec<Line>= Vec::new(); let mut lines: Vec<Line>= Vec::new();
lines.push("Use W/S to change the counter. Enter to exit.".into()); lines.push("Use W/S to change the counter. Enter to exit.".into());
lines.push(format!("Counter: {}", self.counter).into()); lines.push(format!("Counter: {}", self.counter).into());
@ -75,13 +88,13 @@ impl Demo for MyApp {
let text = Paragraph::new(lines).centered().style(style); let text = Paragraph::new(lines).centered().style(style);
fr.render_widget(text, Rect::new(barea.width/2 - 25, barea.height/2, 50, 4)); fr.render_widget(text, Rect::new(barea.width/2 - 25, barea.height/2, 50, 4));
}).map_err(|e| e.to_string())?; }).map_err(|e| e.to_string())?;
Ok(()) Ok(())
} }
async fn input(&mut self, event: terminput::Event) -> Result<(), String> { async fn input(&mut self, event: terminput::Event) -> Result<(), String> {
// Handle input using terminput.
if let terminput::Event::Key(kv) = event { if let terminput::Event::Key(kv) = event {
match kv.code { match kv.code {
KeyCode::Char('w') => { KeyCode::Char('w') => {