From 4c5463951837274cf81e30766888e785d55804d6 Mon Sep 17 00:00:00 2001 From: Jarkko Toivanen Date: Thu, 1 Aug 2024 02:43:59 +0300 Subject: [PATCH] Disabling event polling -> Blocking handling Reduces flickering --- src/main.rs | 68 ++++++++++++++++++++++++----------------------------- 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/src/main.rs b/src/main.rs index d51a40d..9dce40a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,9 @@ use crossterm::{ - event::{poll, read, Event, KeyCode}, + event::{read, Event, KeyCode}, terminal, ExecutableCommand, cursor, }; use std::panic; -use std::{ - io::{stdout, Result, Write, Stdout}, - time::Duration, -}; +use std::io::{stdout, Result, Write, Stdout}; #[derive(Default)] @@ -37,6 +34,7 @@ fn main() -> Result<()> { impl Nuudel { pub fn run(&mut self, stdout: &mut Stdout) -> Result<()> { + self.draw_terminal(stdout)?; while !self.quitting { self.handle_events()?; self.draw_terminal(stdout)?; @@ -61,39 +59,37 @@ impl Nuudel { } fn handle_events(&mut self) -> Result<()> { - if poll(Duration::from_secs(0))? { - match read()? { - Event::Key(event) => match event.code { - KeyCode::Char('q') => { - self.quit(); - }, - KeyCode::Char('a') => { - print!("█"); - }, - KeyCode::Left => { - if self.cursorx > 0 { - self.cursorx -= 1; - self.cursorx = self.cursorx.clamp(0, terminal::size()?.0-1); - } - }, - KeyCode::Right => { - self.cursorx += 1; + match read()? { + Event::Key(event) => match event.code { + KeyCode::Char('q') => { + self.quit(); + }, + KeyCode::Char('a') => { + print!("█"); + }, + KeyCode::Left => { + if self.cursorx > 0 { + self.cursorx -= 1; self.cursorx = self.cursorx.clamp(0, terminal::size()?.0-1); - }, - KeyCode::Up => { - if self.cursory > 0 { - self.cursory -= 1; - } - self.cursory = self.cursory.clamp(0, terminal::size()?.1-1); - }, - KeyCode::Down => { - self.cursory += 1; - self.cursory = self.cursory.clamp(0, terminal::size()?.1-1); - }, - _ => {} + } + }, + KeyCode::Right => { + self.cursorx += 1; + self.cursorx = self.cursorx.clamp(0, terminal::size()?.0-1); + }, + KeyCode::Up => { + if self.cursory > 0 { + self.cursory -= 1; + } + self.cursory = self.cursory.clamp(0, terminal::size()?.1-1); + }, + KeyCode::Down => { + self.cursory += 1; + self.cursory = self.cursory.clamp(0, terminal::size()?.1-1); }, _ => {} - } + }, + _ => {} } Ok(()) } @@ -131,8 +127,6 @@ impl Nuudel { stdout.execute(cursor::MoveTo(self.cursorx, self.cursory))?; stdout.execute(cursor::Show)?; - std::thread::sleep(Duration::from_millis(16)); - Ok(()) } }