From a716166eda8a8a3de31fe79453aaa589893f9f90 Mon Sep 17 00:00:00 2001 From: Himadri Bhattacharjee <107522312+lavafroth@users.noreply.github.com> Date: Fri, 18 Apr 2025 07:59:25 +0530 Subject: [PATCH] feat: scaffolding for parsing commands --- src/main.rs | 66 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9702988..1eacf4d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -201,23 +201,26 @@ impl AppServer { let id = self.id; tokio::spawn(async move { let mut clients = clients.write().await; - let Client { - terminal, textarea, .. - } = clients.get_mut(&id).unwrap(); + let client = clients.get_mut(&id).unwrap(); - terminal - .draw(|f| { - let buf = f.buffer_mut(); - let area = Layout::default() - .direction(Direction::Vertical) - .constraints(vec![Constraint::Fill(1), Constraint::Length(4)]) - .split(buf.area)[1]; - for i in 0..(area.width * area.y) as usize { - buf.content[i].set_skip(true); - } - f.render_widget(&*textarea, area); - }) - .unwrap(); + let res = client.terminal.draw(|f| { + let buf = f.buffer_mut(); + let area = Layout::default() + .direction(Direction::Vertical) + .constraints(vec![Constraint::Fill(1), Constraint::Length(4)]) + .split(buf.area)[1]; + for i in 0..(area.width * area.y) as usize { + buf.content[i].set_skip(true); + } + f.render_widget(&client.textarea, area); + }); + if let Err(e) = res { + log::error!( + "failed to render the chat interface for client {}: {}", + client.channel, + e + ) + } }); } } @@ -329,9 +332,13 @@ impl Handler for AppServer { text }; let name = self.entity().await.name().to_string(); - let message = format!("[{name}]: {text}"); - self.app.write().await.history.push(message); - self.render().await; + // TODO: handle commands + let Some(command) = Command::parse(&text) else { + let message = format!("[{name}]: {text}"); + self.app.write().await.history.push(message); + self.render().await; + return Ok(()); + }; } // Alt-Return for multiline [27, 13] => { @@ -450,6 +457,27 @@ impl Drop for AppServer { } } +pub enum Command { + Add { key: String }, + Rename { from: String, to: String }, +} + +impl Command { + fn parse(text: &str) -> Option { + let split: Vec<&str> = text.split_whitespace().collect(); + Some(match &split[..] { + ["/add", key] => Self::Add { + key: key.to_string(), + }, + ["/rename", to, from] => Self::Rename { + to: to.to_string(), + from: from.to_string(), + }, + _ => return None, + }) + } +} + #[derive(Parser, Debug, Clone)] #[command(version, about)] struct Args {