add keyboard shortcuts for navigating up and down back

This commit is contained in:
Michael Zhang 2021-03-09 06:03:57 -06:00
parent d898e2a07e
commit 61a6a45b6c
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
2 changed files with 30 additions and 0 deletions

View file

@ -43,6 +43,34 @@ fn humanize_timestamp(date: DateTime<Local>) -> String {
} }
impl MailTabState { 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) { pub fn render(&mut self, f: &mut FrameType, area: Rect) {
let chunks = Layout::default() let chunks = Layout::default()
.direction(Direction::Horizontal) .direction(Direction::Horizontal)

View file

@ -77,6 +77,8 @@ pub async fn run_ui(
if let Event::Key(KeyEvent { code, .. }) = event { if let Event::Key(KeyEvent { code, .. }) = event {
match code { match code {
KeyCode::Char('q') => break, KeyCode::Char('q') => break,
KeyCode::Char('j') => mail_tab.move_down(),
KeyCode::Char('k') => mail_tab.move_up(),
_ => {} _ => {}
} }
} }