From 3e9ceba302341599ff574ed3dad08b4897d7630d Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 27 Jun 2022 10:14:30 +0200 Subject: [PATCH] Connect to new rooms --- src/euph/room.rs | 25 ++++++++++++++----------- src/ui.rs | 3 ++- src/ui/rooms.rs | 17 ++++++++++++++++- src/ui/util.rs | 20 ++++++++++++++++++++ 4 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 src/ui/util.rs diff --git a/src/euph/room.rs b/src/euph/room.rs index 8296711..a8b8783 100644 --- a/src/euph/room.rs +++ b/src/euph/room.rs @@ -66,19 +66,19 @@ impl State { async fn reconnect(name: &str, event_tx: &mpsc::UnboundedSender) -> anyhow::Result<()> { loop { info!("e&{}: connecting", name); - let (conn_tx, mut conn_rx) = match Self::connect(name).await? { - Some(conn) => conn, - None => continue, - }; - info!("e&{}: connected", name); - event_tx.send(Event::Connected(conn_tx))?; + if let Some((conn_tx, mut conn_rx)) = Self::connect(name).await? { + info!("e&{}: connected", name); + event_tx.send(Event::Connected(conn_tx))?; - while let Ok(data) = conn_rx.recv().await { - event_tx.send(Event::Data(data))?; + while let Ok(data) = conn_rx.recv().await { + event_tx.send(Event::Data(data))?; + } + + info!("e&{}: disconnected", name); + event_tx.send(Event::Disconnected)?; + } else { + info!("e&{}: could not connect", name); } - - info!("e&{}: disconnected", name); - event_tx.send(Event::Disconnected)?; time::sleep(Duration::from_secs(5)).await; // TODO Make configurable } } @@ -91,6 +91,9 @@ impl State { Err(tungstenite::Error::Http(resp)) if resp.status().is_client_error() => { bail!("room {name} doesn't exist"); } + Err(tungstenite::Error::Url(_) | tungstenite::Error::HttpFormat(_)) => { + bail!("format error for room {name}"); + } Err(_) => Ok(None), } } diff --git a/src/ui.rs b/src/ui.rs index 41ba3c2..7f7e7d9 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,4 +1,5 @@ mod rooms; +mod util; use std::sync::{Arc, Weak}; use std::time::Duration; @@ -194,7 +195,7 @@ impl Ui { match self.mode { Mode::Main => { self.rooms - .handle_key_event(terminal, size, &self.event_tx, event) + .handle_key_event(terminal, size, &self.event_tx, crossterm_lock, event) .await } Mode::Log => self.log_chat.handle_navigation(terminal, size, event).await, diff --git a/src/ui/rooms.rs b/src/ui/rooms.rs index 9fe2fd8..42c55c5 100644 --- a/src/ui/rooms.rs +++ b/src/ui/rooms.rs @@ -1,6 +1,8 @@ use std::collections::{HashMap, HashSet}; +use std::sync::Arc; use crossterm::event::{KeyCode, KeyEvent}; +use parking_lot::FairMutex; use tokio::sync::mpsc; use toss::frame::{Frame, Pos, Size}; use toss::terminal::Terminal; @@ -9,7 +11,7 @@ use crate::chat::Chat; use crate::euph; use crate::vault::{EuphMsg, EuphVault, Vault}; -use super::UiEvent; +use super::{util, UiEvent}; mod style { use crossterm::style::{ContentStyle, Stylize}; @@ -159,6 +161,7 @@ impl Rooms { terminal: &mut Terminal, size: Size, ui_event_tx: &mpsc::UnboundedSender, + crossterm_lock: &Arc>, event: KeyEvent, ) { if let Some(focus) = &self.focus { @@ -203,6 +206,18 @@ impl Rooms { } } } + KeyCode::Char('C') => { + if let Some(room) = util::prompt(terminal, crossterm_lock) { + let room = room.trim().to_string(); + self.euph_rooms.entry(room.clone()).or_insert_with(|| { + euph::Room::new( + room.clone(), + self.vault.euph(room), + ui_event_tx.clone(), + ) + }); + } + } KeyCode::Char('d') => { if let Some(cursor) = &self.cursor { if let Some(room) = rooms.get(cursor.index) { diff --git a/src/ui/util.rs b/src/ui/util.rs new file mode 100644 index 0000000..e045b4c --- /dev/null +++ b/src/ui/util.rs @@ -0,0 +1,20 @@ +use std::sync::Arc; + +use parking_lot::FairMutex; +use toss::terminal::Terminal; + +pub fn prompt(terminal: &mut Terminal, crossterm_lock: &Arc>) -> Option { + let content = { + let _guard = crossterm_lock.lock(); + terminal.suspend().expect("could not suspend"); + let content = edit::edit("").expect("could not edit"); + terminal.unsuspend().expect("could not unsuspend"); + content + }; + + if content.trim().is_empty() { + None + } else { + Some(content) + } +}