From 61a6a45b6c1d16663576d4a2e0a1194bd10e9032 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Tue, 9 Mar 2021 06:03:57 -0600 Subject: [PATCH] add keyboard shortcuts for navigating up and down back --- src/ui/mail_tab.rs | 28 ++++++++++++++++++++++++++++ src/ui/mod.rs | 2 ++ 2 files changed, 30 insertions(+) diff --git a/src/ui/mail_tab.rs b/src/ui/mail_tab.rs index 4bf6687..437622d 100644 --- a/src/ui/mail_tab.rs +++ b/src/ui/mail_tab.rs @@ -43,6 +43,34 @@ fn humanize_timestamp(date: DateTime) -> String { } impl MailTabState { + pub fn move_down(&mut self) { + if self.message_uids.is_empty() { + return; + } + let len = self.message_uids.len(); + if let Some(selected) = self.message_list.selected() { + if selected + 1 < len { + self.message_list.select(Some(selected + 1)); + } + } else { + self.message_list.select(Some(0)); + } + } + + pub fn move_up(&mut self) { + if self.message_uids.is_empty() { + return; + } + let len = self.message_uids.len(); + if let Some(selected) = self.message_list.selected() { + if selected >= 1 { + self.message_list.select(Some(selected - 1)); + } + } else { + self.message_list.select(Some(len - 1)); + } + } + pub fn render(&mut self, f: &mut FrameType, area: Rect) { let chunks = Layout::default() .direction(Direction::Horizontal) diff --git a/src/ui/mod.rs b/src/ui/mod.rs index dd0e779..5690c17 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -77,6 +77,8 @@ pub async fn run_ui( if let Event::Key(KeyEvent { code, .. }) = event { match code { KeyCode::Char('q') => break, + KeyCode::Char('j') => mail_tab.move_down(), + KeyCode::Char('k') => mail_tab.move_up(), _ => {} } }