Skip to content

Commit

Permalink
chore(examples): refactor some examples (#578)
Browse files Browse the repository at this point in the history
* chore(examples): Simplify timeout calculation with `Duration::saturating_sub`

Signed-off-by: rhysd <[email protected]>

* fix(examples): Do not ignore errors from `poll_input` call

Signed-off-by: rhysd <[email protected]>

---------

Signed-off-by: rhysd <[email protected]>
  • Loading branch information
rhysd authored Oct 20, 2023
1 parent 27c5637 commit 6cbdb06
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 31 deletions.
4 changes: 1 addition & 3 deletions examples/barchart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ fn run_app<B: Backend>(
loop {
terminal.draw(|f| ui(f, &app))?;

let timeout = tick_rate
.checked_sub(last_tick.elapsed())
.unwrap_or_else(|| Duration::from_secs(0));
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
if crossterm::event::poll(timeout)? {
if let Event::Key(key) = event::read()? {
if let KeyCode::Char('q') = key.code {
Expand Down
4 changes: 1 addition & 3 deletions examples/chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ fn run_app<B: Backend>(
loop {
terminal.draw(|f| ui(f, &app))?;

let timeout = tick_rate
.checked_sub(last_tick.elapsed())
.unwrap_or_else(|| Duration::from_secs(0));
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
if crossterm::event::poll(timeout)? {
if let Event::Key(key) = event::read()? {
if let KeyCode::Char('q') = key.code {
Expand Down
4 changes: 1 addition & 3 deletions examples/demo/crossterm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ fn run_app<B: Backend>(
loop {
terminal.draw(|f| ui::draw(f, &mut app))?;

let timeout = tick_rate
.checked_sub(last_tick.elapsed())
.unwrap_or_else(|| Duration::from_secs(0));
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
if crossterm::event::poll(timeout)? {
if let Event::Key(key) = event::read()? {
if key.kind == KeyEventKind::Press {
Expand Down
11 changes: 4 additions & 7 deletions examples/demo/termwiz.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{
error::Error,
io,
time::{Duration, Instant},
};

Expand Down Expand Up @@ -32,19 +31,17 @@ fn run_app(
terminal: &mut Terminal<TermwizBackend>,
mut app: App,
tick_rate: Duration,
) -> io::Result<()> {
) -> Result<(), Box<dyn Error>> {
let mut last_tick = Instant::now();
loop {
terminal.draw(|f| ui::draw(f, &mut app))?;

let timeout = tick_rate
.checked_sub(last_tick.elapsed())
.unwrap_or_else(|| Duration::from_secs(0));
if let Ok(Some(input)) = terminal
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
if let Some(input) = terminal
.backend_mut()
.buffered_terminal_mut()
.terminal()
.poll_input(Some(timeout))
.poll_input(Some(timeout))?
{
match input {
InputEvent::Key(key_code) => match key_code.key {
Expand Down
4 changes: 1 addition & 3 deletions examples/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ fn input_handling(tx: mpsc::Sender<Event>) {
let mut last_tick = Instant::now();
loop {
// poll for tick rate duration, if no events, sent tick event.
let timeout = tick_rate
.checked_sub(last_tick.elapsed())
.unwrap_or_else(|| Duration::from_secs(0));
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
if crossterm::event::poll(timeout).unwrap() {
match crossterm::event::read().unwrap() {
crossterm::event::Event::Key(key) => tx.send(Event::Input(key)).unwrap(),
Expand Down
4 changes: 1 addition & 3 deletions examples/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,7 @@ fn run_app<B: Backend>(
loop {
terminal.draw(|f| ui(f, &mut app))?;

let timeout = tick_rate
.checked_sub(last_tick.elapsed())
.unwrap_or_else(|| Duration::from_secs(0));
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
if crossterm::event::poll(timeout)? {
if let Event::Key(key) = event::read()? {
if key.kind == KeyEventKind::Press {
Expand Down
4 changes: 1 addition & 3 deletions examples/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ fn run_app<B: Backend>(
loop {
terminal.draw(|f| ui(f, &app))?;

let timeout = tick_rate
.checked_sub(last_tick.elapsed())
.unwrap_or_else(|| Duration::from_secs(0));
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
if crossterm::event::poll(timeout)? {
if let Event::Key(key) = event::read()? {
if let KeyCode::Char('q') = key.code {
Expand Down
4 changes: 1 addition & 3 deletions examples/scrollbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ fn run_app<B: Backend>(
loop {
terminal.draw(|f| ui(f, &mut app))?;

let timeout = tick_rate
.checked_sub(last_tick.elapsed())
.unwrap_or_else(|| Duration::from_secs(0));
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
if crossterm::event::poll(timeout)? {
if let Event::Key(key) = event::read()? {
match key.code {
Expand Down
4 changes: 1 addition & 3 deletions examples/sparkline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ fn run_app<B: Backend>(
loop {
terminal.draw(|f| ui(f, &app))?;

let timeout = tick_rate
.checked_sub(last_tick.elapsed())
.unwrap_or_else(|| Duration::from_secs(0));
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
if crossterm::event::poll(timeout)? {
if let Event::Key(key) = event::read()? {
if let KeyCode::Char('q') = key.code {
Expand Down

0 comments on commit 6cbdb06

Please sign in to comment.