Skip to content

Commit 53a884a

Browse files
committed
tokio select futures
1 parent bcbbd06 commit 53a884a

3 files changed

Lines changed: 128 additions & 19 deletions

File tree

Cargo.lock

Lines changed: 96 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ ctrlc = "3.4"
1111
ratatui = "0.29"
1212
serialport = "4.3"
1313
tokio = { version = "1.0", features = ["full"] }
14+
futures = "0.3"

src/main.rs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -582,29 +582,41 @@ async fn run_ui<B: Backend>(
582582
let mut app_state = AppState::new();
583583

584584
while ui_config.running.load(Ordering::SeqCst) && !app_state.should_quit {
585-
// Check for UI messages (like quit from Ctrl-C)
586-
if let Ok(msg) = ui_rx.try_recv() {
587-
match msg {
588-
UiMessage::Quit => {
589-
app_state.should_quit = true;
590-
break;
585+
tokio::select! {
586+
// UI messages (like quit from Ctrl-C)
587+
msg = ui_rx.recv() => {
588+
if let Some(msg) = msg {
589+
match msg {
590+
UiMessage::Quit => {
591+
app_state.should_quit = true;
592+
break;
593+
}
594+
}
591595
}
592596
}
593-
}
594597

595-
// Check for serial data
596-
if let Ok(data) = serial_rx.try_recv() {
597-
match data {
598-
SerialData::Received(line) => {
599-
app_state.add_output(line);
598+
// Serial data
599+
data = serial_rx.recv() => {
600+
if let Some(data) = data {
601+
match data {
602+
SerialData::Received(line) => {
603+
app_state.add_output(line);
604+
}
605+
}
600606
}
601607
}
602-
}
603608

604-
// Handle keyboard input
605-
if event::poll(Duration::from_millis(5))? {
606-
match event::read()? {
607-
Event::Key(k) if k.kind == KeyEventKind::Press => {
609+
// Keyboard input - async wrapper for crossterm events
610+
key_result = async {
611+
if event::poll(Duration::from_millis(0)).unwrap_or(false) {
612+
event::read()
613+
} else {
614+
tokio::time::sleep(Duration::from_millis(1)).await;
615+
return Err(std::io::Error::new(std::io::ErrorKind::WouldBlock, "no input"));
616+
}
617+
} => {
618+
if let Ok(Event::Key(k)) = key_result {
619+
if k.kind == KeyEventKind::Press {
608620
match k.code {
609621
KeyCode::Char(c)
610622
if k.modifiers.contains(KeyModifiers::CONTROL)
@@ -695,12 +707,12 @@ async fn run_ui<B: Backend>(
695707
}
696708
_ => {}
697709
}
710+
}
698711
}
699-
_ => {}
700712
}
701713
}
702714

703-
// Render the UI
715+
// Render the UI after processing events
704716
terminal.draw(|f| draw_ui(f, &mut app_state))?;
705717
}
706718

0 commit comments

Comments
 (0)