diff --git a/src/ui.rs b/src/ui.rs index 7d4d007..41ba3c2 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -192,7 +192,11 @@ impl Ui { } match self.mode { - Mode::Main => self.rooms.handle_key_event(terminal, size, event).await, + Mode::Main => { + self.rooms + .handle_key_event(terminal, size, &self.event_tx, 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 98a51f9..9fe2fd8 100644 --- a/src/ui/rooms.rs +++ b/src/ui/rooms.rs @@ -1,6 +1,7 @@ use std::collections::{HashMap, HashSet}; use crossterm::event::{KeyCode, KeyEvent}; +use tokio::sync::mpsc; use toss::frame::{Frame, Pos, Size}; use toss::terminal::Terminal; @@ -8,6 +9,8 @@ use crate::chat::Chat; use crate::euph; use crate::vault::{EuphMsg, EuphVault, Vault}; +use super::UiEvent; + mod style { use crossterm::style::{ContentStyle, Stylize}; @@ -53,7 +56,14 @@ impl Rooms { } async fn rooms(&self) -> Vec { - let mut rooms = self.vault.euph_rooms().await; + let mut rooms = HashSet::new(); + for room in self.vault.euph_rooms().await { + rooms.insert(room); + } + for room in self.euph_rooms.keys().cloned() { + rooms.insert(room); + } + let mut rooms = rooms.into_iter().collect::>(); rooms.sort_unstable(); rooms } @@ -134,11 +144,23 @@ impl Rooms { for x in 0..size.width { frame.write(Pos::new(x.into(), y), " ", style); } - frame.write(Pos::new(0, y), &format!("&{room}"), style); + let suffix = if self.euph_rooms.contains_key(room) { + "*" + } else { + "" + }; + let room_str = format!("&{room}{suffix}"); + frame.write(Pos::new(0, y), &room_str, style); } } - pub async fn handle_key_event(&mut self, terminal: &mut Terminal, size: Size, event: KeyEvent) { + pub async fn handle_key_event( + &mut self, + terminal: &mut Terminal, + size: Size, + ui_event_tx: &mpsc::UnboundedSender, + event: KeyEvent, + ) { if let Some(focus) = &self.focus { if event.code == KeyCode::Esc { self.focus = None; @@ -167,6 +189,27 @@ impl Rooms { cursor.line -= 1; } } + KeyCode::Char('c') => { + if let Some(cursor) = &self.cursor { + if let Some(room) = rooms.get(cursor.index) { + let room = room.clone(); + 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) { + self.euph_rooms.remove(room); + } + } + } _ => {} } }