Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add polling of events
Signed-off-by: Dave Grantham <dwg@linuxprogrammer.org>
  • Loading branch information
Dave Grantham committed Apr 23, 2025
commit 46a33eef855f974e6acb6dbc0c76432e141f08ee
8 changes: 1 addition & 7 deletions rust-peer/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::Result;
use libp2p::identity;
use libp2p_webrtc::tokio::Certificate;
use std::path::Path;
use tokio::{fs, signal, task::JoinHandle};
use tokio::{fs, task::JoinHandle};
use tokio_util::sync::CancellationToken;
use tracing::info;

Expand Down Expand Up @@ -33,12 +33,6 @@ async fn main() -> Result<()> {
let peer_task: JoinHandle<Result<()>> = tokio::spawn(async move { peer.run().await });
let ui_task: JoinHandle<Result<()>> = tokio::spawn(async move { ui.run().await });

// wait for ctrl-c to quit
signal::ctrl_c().await?;

// cancel the shutdown token
shutdown.cancel();

// wait for the tasks to finish
let (ui_result, peer_result) = tokio::try_join!(peer_task, ui_task)?;

Expand Down
1 change: 1 addition & 0 deletions rust-peer/src/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ impl Peer {
loop {
tokio::select! {
_ = self.shutdown.cancelled() => {
info!("Shutting down the peer");
break;
}
Some(_message) = self.from_ui.recv() => {
Expand Down
66 changes: 38 additions & 28 deletions rust-peer/src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{log::Message as LogMessage, Message};
use anyhow::Result;
use crossterm::{
event::{self, Event as CEvent, KeyCode},
event::{self, Event, KeyCode, KeyEvent, KeyModifiers},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
};
Expand All @@ -18,11 +18,13 @@ use ratatui::{
use std::{
collections::{HashSet, VecDeque},
io,
time::Duration,
};
use tokio::sync::mpsc::{self, Receiver, Sender};
use tokio_util::sync::CancellationToken;
use tracing::info;

/// A simple TUI based UI for the peer
/// A simple UI for the peer
pub struct Ui {
// my peer id
me: PeerId,
Expand Down Expand Up @@ -80,11 +82,6 @@ impl Ui {

// Main loop
loop {
// Check for shutdown
if self.shutdown.is_cancelled() {
break;
}

// Process log messages
while let Ok(log) = self.from_log.try_recv() {
log_widget.add_line(log.message);
Expand Down Expand Up @@ -116,27 +113,40 @@ impl Ui {
})?;

// Handle input events
if let CEvent::Key(key) = event::read()? {
match key.code {
KeyCode::Tab => {
selected_tab = (selected_tab + 1) % 2;
}
KeyCode::Char(c) if selected_tab == 0 => {
chat_widget.input.push(c);
}
KeyCode::Backspace if selected_tab == 0 => {
chat_widget.input.pop();
}
KeyCode::Enter if selected_tab == 0 => {
self.to_peer
.send(Message::Chat {
peer: self.me,
message: chat_widget.input.clone(),
})
.await?;
chat_widget.input.clear();
if event::poll(Duration::from_millis(18))? {
if let Event::Key(key) = event::read()? {
match key {
KeyEvent {
code: KeyCode::Char('c'),
modifiers: KeyModifiers::CONTROL,
..
} => {
info!("Received Ctrl+C, shutting down...");
self.shutdown.cancel();
break;
}
_ => match key.code {
KeyCode::Tab => {
selected_tab = (selected_tab + 1) % 2;
}
KeyCode::Char(c) if selected_tab == 0 => {
chat_widget.input.push(c);
}
KeyCode::Backspace if selected_tab == 0 => {
chat_widget.input.pop();
}
KeyCode::Enter if selected_tab == 0 => {
self.to_peer
.send(Message::Chat {
peer: self.me,
message: chat_widget.input.clone(),
})
.await?;
chat_widget.input.clear();
}
_ => {}
},
}
_ => {}
}
}
}
Expand Down Expand Up @@ -252,7 +262,7 @@ impl Widget for &ChatWidget<'_> {
// calculate the layout for the top row
let top_layout = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(100), Constraint::Length(18)].as_ref())
.constraints([Constraint::Percentage(100), Constraint::Length(24)].as_ref())
.split(layout[0]);

// render the chat messages
Expand Down