diff options
-rw-r--r-- | rust/src/main.rs | 55 |
1 files changed, 34 insertions, 21 deletions
diff --git a/rust/src/main.rs b/rust/src/main.rs index 4b79973..6e10938 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -88,6 +88,10 @@ async fn send_command(conn: &Connection, cmd: Command) -> io::Result<Reply> { } } +static STATUS_ROOM: Lazy<Word> = Lazy::new(|| { + Word::try_from("*status".to_string()).unwrap() +}); + type Stdout = MouseTerminal<AlternateScreen<RawTerminal<io::Stdout>>>; // type Stdout = MouseTerminal<io::Stdout>; @@ -146,13 +150,17 @@ enum HItem { impl App { fn new(conn: Connection, stdout: Stdout, username: Word) -> Self { let termsize = (0, 0); + + let mut init_rooms_map = HashMap::new(); + init_rooms_map.insert(STATUS_ROOM.clone(), RoomData::default()); + let state = State { stdout, termsize, layout: Layout::default(), - roomlist: Vec::new(), - rooms: HashMap::new(), - currentroom: Word::default(), + roomlist: vec![STATUS_ROOM.clone()], + rooms: init_rooms_map, + currentroom: STATUS_ROOM.clone(), msgs: HashMap::new(), }; App { @@ -172,7 +180,7 @@ impl App { let mut state = self.state.lock().await; let toremove = state.roomlist.iter() - .filter(|r| !newrooms.contains(r)) + .filter(|&r| *r != *STATUS_ROOM && !newrooms.contains(r)) .cloned() .collect::<HashSet<_>>(); state.roomlist.retain(|r| !toremove.contains(r)); @@ -196,7 +204,7 @@ impl App { if state.rooms.insert(room.clone(), RoomData::default()).is_none() { state.roomlist.push(room.clone()); - if state.currentroom.len() == 0 { + if state.currentroom == *STATUS_ROOM { state.currentroom = room.clone(); } } @@ -320,8 +328,8 @@ impl App { let mut state = self.state.lock().await; let current_index = state.roomlist.iter().enumerate() - .filter(|&t| *t.1 == state.currentroom) - .map(|t| t.0) + .filter(|&(_, r)| *r == state.currentroom) + .map(|(i, _)| i) .next().unwrap_or(0); let index = (current_index as isize + delta) .max(0).min(state.roomlist.len() as isize - 1); @@ -381,18 +389,24 @@ impl App { let app = self.clone(); task::spawn(async move { let room = app.state.lock().await.currentroom.clone(); - let line = Line::try_from(msg).unwrap(); - if let Ok(Reply::Number(id)) = send_command(&app.conn, Command::Send { - room_name: room.clone(), reply_on: None, message: line.clone() - }).await { - app.add_message(Message { - id: Id::try_from(id).unwrap(), - reply_on: None, - roomname: room, - username: app.username.clone(), - timestamp: std::time::SystemTime::now(), - message: line, - }).await.unwrap(); + if room == *STATUS_ROOM { + app.append_history_item(&STATUS_ROOM, + HItem::Service("Cannot send messages in this magic status room".to_string()) + ).await.unwrap(); + } else { + let line = Line::try_from(msg).unwrap(); + if let Ok(Reply::Number(id)) = send_command(&app.conn, Command::Send { + roomname: room.clone(), reply_on: None, message: line.clone() + }).await { + app.add_message(Message { + id: Id::try_from(id).unwrap(), + reply_on: None, + roomname: room, + username: app.username.clone(), + timestamp: std::time::SystemTime::now(), + message: line, + }).await.unwrap(); + } } }); } @@ -402,8 +416,7 @@ impl App { } Ok((cmd, _)) => { - let room = self.state.lock().await.currentroom.clone(); - self.append_history_item(&room, HItem::Service(format!("Unknown command: '{}'", cmd))).await?; + self.append_history_item(&STATUS_ROOM, HItem::Service(format!("Unknown command: '{}'", cmd))).await?; } } |