From 03dfe10f3e2c616fa4b3b6b947b4e714eb21c031 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 18 Jun 2022 17:22:57 +0200 Subject: [PATCH] Separate navigation and messaging --- cove-tui/src/chat.rs | 32 ++++++++++++++++----------- cove-tui/src/chat/tree.rs | 28 +++++++++++++++--------- cove-tui/src/chat/tree/action.rs | 37 +++++++++++--------------------- cove-tui/src/ui.rs | 7 +++--- 4 files changed, 53 insertions(+), 51 deletions(-) diff --git a/cove-tui/src/chat.rs b/cove-tui/src/chat.rs index 1885273..d1a0a36 100644 --- a/cove-tui/src/chat.rs +++ b/cove-tui/src/chat.rs @@ -54,28 +54,36 @@ impl> Chat { } } -pub enum Handled { - Ok, - NewMessage { parent: Option, content: String }, -} - impl> Chat { - pub async fn handle_key_event( + pub async fn handle_navigation( &mut self, - event: KeyEvent, terminal: &mut Terminal, size: Size, - crossterm_lock: &Arc>, - ) -> Handled { + event: KeyEvent, + ) { match self.mode { Mode::Tree => { self.tree - .handle_key_event( - crossterm_lock, + .handle_navigation(&mut self.store, &mut self.cursor, terminal, size, event) + .await + } + } + } + + pub async fn handle_messaging( + &mut self, + terminal: &mut Terminal, + crossterm_lock: &Arc>, + event: KeyEvent, + ) -> Option<(Option, String)> { + match self.mode { + Mode::Tree => { + self.tree + .handle_messaging( &mut self.store, &mut self.cursor, terminal, - size, + crossterm_lock, event, ) .await diff --git a/cove-tui/src/chat/tree.rs b/cove-tui/src/chat/tree.rs index 54c41a8..6123599 100644 --- a/cove-tui/src/chat/tree.rs +++ b/cove-tui/src/chat/tree.rs @@ -15,7 +15,7 @@ use toss::terminal::Terminal; use crate::store::{Msg, MsgStore}; -use super::{Cursor, Handled}; +use super::Cursor; pub struct TreeView { // pub focus: Option, @@ -31,17 +31,15 @@ impl TreeView { } } - pub async fn handle_key_event>( + pub async fn handle_navigation>( &mut self, - l: &Arc>, s: &mut S, c: &mut Option>, t: &mut Terminal, z: Size, event: KeyEvent, - ) -> Handled { + ) { match event.code { - // Cursor movement KeyCode::Char('k') => self.move_up(s, c, t.frame(), z).await, KeyCode::Char('j') => self.move_down(s, c, t.frame(), z).await, KeyCode::Char('K') => self.move_up_sibling(s, c, t.frame(), z).await, @@ -50,14 +48,24 @@ impl TreeView { KeyCode::Char('g') => self.move_to_first(s, c, t.frame(), z).await, KeyCode::Char('G') => self.move_to_last(s, c, t.frame(), z).await, KeyCode::Esc => *c = None, // TODO Make 'G' do the same thing? - // Writing messages - KeyCode::Char('r') => return Self::reply_normal(l, s, c, t).await, - KeyCode::Char('R') => return Self::reply_alternate(l, s, c, t).await, - KeyCode::Char('t') | KeyCode::Char('T') => return Self::create_new_thread(l, t).await, _ => {} } + } - Handled::Ok + pub async fn handle_messaging>( + &mut self, + s: &mut S, + c: &mut Option>, + t: &mut Terminal, + l: &Arc>, + event: KeyEvent, + ) -> Option<(Option, String)> { + match event.code { + KeyCode::Char('r') => Self::reply_normal(s, c, t, l).await, + KeyCode::Char('R') => Self::reply_alternate(s, c, t, l).await, + KeyCode::Char('t') | KeyCode::Char('T') => Self::create_new_thread(t, l).await, + _ => None, + } } pub async fn render>( diff --git a/cove-tui/src/chat/tree/action.rs b/cove-tui/src/chat/tree/action.rs index 0dce400..b43cec4 100644 --- a/cove-tui/src/chat/tree/action.rs +++ b/cove-tui/src/chat/tree/action.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use parking_lot::FairMutex; use toss::terminal::Terminal; -use crate::chat::{Cursor, Handled}; +use crate::chat::Cursor; use crate::store::{Msg, MsgStore}; use super::TreeView; @@ -26,11 +26,11 @@ impl TreeView { } pub async fn reply_normal>( - crossterm_lock: &Arc>, store: &S, cursor: &Option>, terminal: &mut Terminal, - ) -> Handled { + crossterm_lock: &Arc>, + ) -> Option<(Option, String)> { if let Some(cursor) = cursor { let tree = store.tree(store.path(&cursor.id).await.first()).await; let parent_id = if tree.next_sibling(&cursor.id).is_some() { @@ -52,23 +52,20 @@ impl TreeView { }; if let Some(content) = Self::prompt_msg(crossterm_lock, terminal) { - return Handled::NewMessage { - parent: Some(parent_id), - content, - }; + return Some((Some(parent_id), content)); } } - Handled::Ok + None } /// Does approximately the opposite of [`Self::reply_normal`]. pub async fn reply_alternate>( - crossterm_lock: &Arc>, store: &S, cursor: &Option>, terminal: &mut Terminal, - ) -> Handled { + crossterm_lock: &Arc>, + ) -> Option<(Option, String)> { if let Some(cursor) = cursor { let tree = store.tree(store.path(&cursor.id).await.first()).await; let parent_id = if tree.next_sibling(&cursor.id).is_none() { @@ -84,27 +81,17 @@ impl TreeView { }; if let Some(content) = Self::prompt_msg(crossterm_lock, terminal) { - return Handled::NewMessage { - parent: Some(parent_id), - content, - }; + return Some((Some(parent_id), content)); } } - Handled::Ok + None } pub async fn create_new_thread( - crossterm_lock: &Arc>, terminal: &mut Terminal, - ) -> Handled { - if let Some(content) = Self::prompt_msg(crossterm_lock, terminal) { - Handled::NewMessage { - parent: None, - content, - } - } else { - Handled::Ok - } + crossterm_lock: &Arc>, + ) -> Option<(Option, String)> { + Self::prompt_msg(crossterm_lock, terminal).map(|c| (None, c)) } } diff --git a/cove-tui/src/ui.rs b/cove-tui/src/ui.rs index 6824af6..8b18194 100644 --- a/cove-tui/src/ui.rs +++ b/cove-tui/src/ui.rs @@ -192,14 +192,13 @@ impl Ui { match self.visible { Visible::Main => { + self.chat.handle_navigation(terminal, size, event).await; self.chat - .handle_key_event(event, terminal, size, crossterm_lock) + .handle_messaging(terminal, crossterm_lock, event) .await; } Visible::Log => { - self.log_chat - .handle_key_event(event, terminal, size, crossterm_lock) - .await; + self.log_chat.handle_navigation(terminal, size, event).await; } }