Connect to new rooms
This commit is contained in:
parent
05ac42ab78
commit
3e9ceba302
4 changed files with 52 additions and 13 deletions
|
|
@ -66,19 +66,19 @@ impl State {
|
|||
async fn reconnect(name: &str, event_tx: &mpsc::UnboundedSender<Event>) -> 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),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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<UiEvent>,
|
||||
crossterm_lock: &Arc<FairMutex<()>>,
|
||||
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) {
|
||||
|
|
|
|||
20
src/ui/util.rs
Normal file
20
src/ui/util.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue