From e780e6c829f028ed7430c458f6002ac7c5409174 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Thu, 17 Sep 2020 22:15:17 +0200 Subject: rust: Switch buffers using mouse --- rust/src/main.rs | 59 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 19 deletions(-) (limited to 'rust/src') 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, 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, 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, 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, line: &str) -> io::Result { 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, app: Arc) { @@ -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?; + } + + _ => {} } } -- cgit v1.2.3-70-g09d2