diff options
-rw-r--r-- | rust/src/main.rs | 59 |
1 files changed, 40 insertions, 19 deletions
diff --git a/rust/src/main.rs b/rust/src/main.rs index 937cc88..1ce64c6 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -9,7 +9,7 @@ use once_cell::sync::Lazy; use tokio::{runtime, task}; use tokio::sync::Mutex as AsyncMutex; use termion::{clear, color, cursor}; -use termion::event::Key; +use termion::event::{Event, Key, MouseEvent, MouseButton}; use termion::raw::{IntoRawMode, RawTerminal}; use termion::input::{TermRead, MouseTerminal}; use termion::screen::AlternateScreen; @@ -315,19 +315,16 @@ impl App { } } - async fn switch_buffer(self: &Arc<Self>, delta: isize) -> io::Result<()> { - { - let mut state = self.state.lock().await; - let current_index = - state.roomlist.iter().enumerate() - .filter(|&t| *t.1 == state.currentroom) - .map(|t| t.0) - .next().unwrap_or(0); - let index = (current_index as isize + delta) - .max(0).min(state.roomlist.len() as isize - 1); - state.currentroom = state.roomlist[index as usize].clone(); - } - self.full_redraw().await + async fn switch_buffer_rel(self: &Arc<Self>, delta: isize) -> io::Result<()> { + let mut state = self.state.lock().await; + let current_index = + state.roomlist.iter().enumerate() + .filter(|&t| *t.1 == state.currentroom) + .map(|t| t.0) + .next().unwrap_or(0); + let index = (current_index as isize + delta) + .max(0).min(state.roomlist.len() as isize - 1); + state.switch_buffer(index as usize) } // Returns whether application should quit @@ -335,8 +332,8 @@ impl App { match key { Key::Ctrl('c') => { return Ok(true); } Key::Ctrl('l') => self.full_redraw().await?, - Key::F(5) => self.switch_buffer(-1).await?, - Key::F(6) => self.switch_buffer(1).await?, + Key::F(5) => self.switch_buffer_rel(-1).await?, + Key::F(6) => self.switch_buffer_rel(1).await?, _ => { let submitted = { let mut state = self.state.lock().await; @@ -355,6 +352,15 @@ impl App { Ok(false) } + async fn on_mouse(self: &Arc<Self>, x: u16, y: u16) -> io::Result<()> { + let mut state = self.state.lock().await; + if x < state.layout.rlsepx && usize::from(y) < state.roomlist.len() { + state.switch_buffer(usize::from(y))?; + } + + Ok(()) + } + // Returns whether application should quit async fn handle_input(self: &Arc<Self>, line: &str) -> io::Result<bool> { let parsed = if line.starts_with("//") { @@ -497,6 +503,11 @@ impl State { self.rooms.get_mut(room).unwrap().history.push(item); self.full_redraw() } + + fn switch_buffer(&mut self, index: usize) -> io::Result<()> { + self.currentroom = self.roomlist[index].clone(); + self.full_redraw() + } } async fn pushchan_thread(mut chan: mpsc::Receiver<PushMessage>, app: Arc<App>) { @@ -541,9 +552,19 @@ async fn async_main() -> io::Result<()> { app_clone.fetch_rooms_list().await.unwrap(); }); - for key in io::stdin().keys() { - if app.on_key(key?).await? { - break; + for event in io::stdin().events() { + match event? { + Event::Key(key) => { + if app.on_key(key).await? { + break; + } + } + + Event::Mouse(MouseEvent::Press(MouseButton::Left, x, y)) => { + app.on_mouse(x - 1, y - 1).await?; + } + + _ => {} } } |