Skip to content

Commit 6cbdb06

Browse files
authored
chore(examples): refactor some examples (#578)
* 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]>
1 parent 27c5637 commit 6cbdb06

File tree

9 files changed

+12
-31
lines changed

9 files changed

+12
-31
lines changed

examples/barchart.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,7 @@ fn run_app<B: Backend>(
119119
loop {
120120
terminal.draw(|f| ui(f, &app))?;
121121

122-
let timeout = tick_rate
123-
.checked_sub(last_tick.elapsed())
124-
.unwrap_or_else(|| Duration::from_secs(0));
122+
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
125123
if crossterm::event::poll(timeout)? {
126124
if let Event::Key(key) = event::read()? {
127125
if let KeyCode::Char('q') = key.code {

examples/chart.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,7 @@ fn run_app<B: Backend>(
125125
loop {
126126
terminal.draw(|f| ui(f, &app))?;
127127

128-
let timeout = tick_rate
129-
.checked_sub(last_tick.elapsed())
130-
.unwrap_or_else(|| Duration::from_secs(0));
128+
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
131129
if crossterm::event::poll(timeout)? {
132130
if let Event::Key(key) = event::read()? {
133131
if let KeyCode::Char('q') = key.code {

examples/demo/crossterm.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ fn run_app<B: Backend>(
5050
loop {
5151
terminal.draw(|f| ui::draw(f, &mut app))?;
5252

53-
let timeout = tick_rate
54-
.checked_sub(last_tick.elapsed())
55-
.unwrap_or_else(|| Duration::from_secs(0));
53+
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
5654
if crossterm::event::poll(timeout)? {
5755
if let Event::Key(key) = event::read()? {
5856
if key.kind == KeyEventKind::Press {

examples/demo/termwiz.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::{
22
error::Error,
3-
io,
43
time::{Duration, Instant},
54
};
65

@@ -32,19 +31,17 @@ fn run_app(
3231
terminal: &mut Terminal<TermwizBackend>,
3332
mut app: App,
3433
tick_rate: Duration,
35-
) -> io::Result<()> {
34+
) -> Result<(), Box<dyn Error>> {
3635
let mut last_tick = Instant::now();
3736
loop {
3837
terminal.draw(|f| ui::draw(f, &mut app))?;
3938

40-
let timeout = tick_rate
41-
.checked_sub(last_tick.elapsed())
42-
.unwrap_or_else(|| Duration::from_secs(0));
43-
if let Ok(Some(input)) = terminal
39+
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
40+
if let Some(input) = terminal
4441
.backend_mut()
4542
.buffered_terminal_mut()
4643
.terminal()
47-
.poll_input(Some(timeout))
44+
.poll_input(Some(timeout))?
4845
{
4946
match input {
5047
InputEvent::Key(key_code) => match key_code.key {

examples/inline.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,7 @@ fn input_handling(tx: mpsc::Sender<Event>) {
9898
let mut last_tick = Instant::now();
9999
loop {
100100
// poll for tick rate duration, if no events, sent tick event.
101-
let timeout = tick_rate
102-
.checked_sub(last_tick.elapsed())
103-
.unwrap_or_else(|| Duration::from_secs(0));
101+
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
104102
if crossterm::event::poll(timeout).unwrap() {
105103
match crossterm::event::read().unwrap() {
106104
crossterm::event::Event::Key(key) => tx.send(Event::Input(key)).unwrap(),

examples/list.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,7 @@ fn run_app<B: Backend>(
175175
loop {
176176
terminal.draw(|f| ui(f, &mut app))?;
177177

178-
let timeout = tick_rate
179-
.checked_sub(last_tick.elapsed())
180-
.unwrap_or_else(|| Duration::from_secs(0));
178+
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
181179
if crossterm::event::poll(timeout)? {
182180
if let Event::Key(key) = event::read()? {
183181
if key.kind == KeyEventKind::Press {

examples/paragraph.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ fn run_app<B: Backend>(
6464
loop {
6565
terminal.draw(|f| ui(f, &app))?;
6666

67-
let timeout = tick_rate
68-
.checked_sub(last_tick.elapsed())
69-
.unwrap_or_else(|| Duration::from_secs(0));
67+
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
7068
if crossterm::event::poll(timeout)? {
7169
if let Event::Key(key) = event::read()? {
7270
if let KeyCode::Char('q') = key.code {

examples/scrollbar.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ fn run_app<B: Backend>(
5757
loop {
5858
terminal.draw(|f| ui(f, &mut app))?;
5959

60-
let timeout = tick_rate
61-
.checked_sub(last_tick.elapsed())
62-
.unwrap_or_else(|| Duration::from_secs(0));
60+
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
6361
if crossterm::event::poll(timeout)? {
6462
if let Event::Key(key) = event::read()? {
6563
match key.code {

examples/sparkline.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,7 @@ fn run_app<B: Backend>(
109109
loop {
110110
terminal.draw(|f| ui(f, &app))?;
111111

112-
let timeout = tick_rate
113-
.checked_sub(last_tick.elapsed())
114-
.unwrap_or_else(|| Duration::from_secs(0));
112+
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
115113
if crossterm::event::poll(timeout)? {
116114
if let Event::Key(key) = event::read()? {
117115
if let KeyCode::Char('q') = key.code {

0 commit comments

Comments
 (0)