Connect to new rooms

This commit is contained in:
Joscha 2022-06-27 10:14:30 +02:00
parent 05ac42ab78
commit 3e9ceba302
4 changed files with 52 additions and 13 deletions

View file

@ -66,19 +66,19 @@ impl State {
async fn reconnect(name: &str, event_tx: &mpsc::UnboundedSender<Event>) -> anyhow::Result<()> { async fn reconnect(name: &str, event_tx: &mpsc::UnboundedSender<Event>) -> anyhow::Result<()> {
loop { loop {
info!("e&{}: connecting", name); info!("e&{}: connecting", name);
let (conn_tx, mut conn_rx) = match Self::connect(name).await? { if let Some((conn_tx, mut conn_rx)) = Self::connect(name).await? {
Some(conn) => conn, info!("e&{}: connected", name);
None => continue, event_tx.send(Event::Connected(conn_tx))?;
};
info!("e&{}: connected", name);
event_tx.send(Event::Connected(conn_tx))?;
while let Ok(data) = conn_rx.recv().await { while let Ok(data) = conn_rx.recv().await {
event_tx.send(Event::Data(data))?; 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 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() => { Err(tungstenite::Error::Http(resp)) if resp.status().is_client_error() => {
bail!("room {name} doesn't exist"); bail!("room {name} doesn't exist");
} }
Err(tungstenite::Error::Url(_) | tungstenite::Error::HttpFormat(_)) => {
bail!("format error for room {name}");
}
Err(_) => Ok(None), Err(_) => Ok(None),
} }
} }

View file

@ -1,4 +1,5 @@
mod rooms; mod rooms;
mod util;
use std::sync::{Arc, Weak}; use std::sync::{Arc, Weak};
use std::time::Duration; use std::time::Duration;
@ -194,7 +195,7 @@ impl Ui {
match self.mode { match self.mode {
Mode::Main => { Mode::Main => {
self.rooms self.rooms
.handle_key_event(terminal, size, &self.event_tx, event) .handle_key_event(terminal, size, &self.event_tx, crossterm_lock, event)
.await .await
} }
Mode::Log => self.log_chat.handle_navigation(terminal, size, event).await, Mode::Log => self.log_chat.handle_navigation(terminal, size, event).await,

View file

@ -1,6 +1,8 @@
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use crossterm::event::{KeyCode, KeyEvent}; use crossterm::event::{KeyCode, KeyEvent};
use parking_lot::FairMutex;
use tokio::sync::mpsc; use tokio::sync::mpsc;
use toss::frame::{Frame, Pos, Size}; use toss::frame::{Frame, Pos, Size};
use toss::terminal::Terminal; use toss::terminal::Terminal;
@ -9,7 +11,7 @@ use crate::chat::Chat;
use crate::euph; use crate::euph;
use crate::vault::{EuphMsg, EuphVault, Vault}; use crate::vault::{EuphMsg, EuphVault, Vault};
use super::UiEvent; use super::{util, UiEvent};
mod style { mod style {
use crossterm::style::{ContentStyle, Stylize}; use crossterm::style::{ContentStyle, Stylize};
@ -159,6 +161,7 @@ impl Rooms {
terminal: &mut Terminal, terminal: &mut Terminal,
size: Size, size: Size,
ui_event_tx: &mpsc::UnboundedSender<UiEvent>, ui_event_tx: &mpsc::UnboundedSender<UiEvent>,
crossterm_lock: &Arc<FairMutex<()>>,
event: KeyEvent, event: KeyEvent,
) { ) {
if let Some(focus) = &self.focus { 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') => { KeyCode::Char('d') => {
if let Some(cursor) = &self.cursor { if let Some(cursor) = &self.cursor {
if let Some(room) = rooms.get(cursor.index) { if let Some(room) = rooms.get(cursor.index) {

20
src/ui/util.rs Normal file
View file

@ -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<FairMutex<()>>) -> Option<String> {
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)
}
}