From db97ad13a6644d3fd1ed4259884d588f154a2e03 Mon Sep 17 00:00:00 2001 From: Uncle Stretch Date: Sun, 8 Dec 2024 19:18:19 +0300 Subject: [PATCH] ability to move through wallet logs Signed-off-by: Uncle Stretch --- src/components/wallet/event_logs.rs | 66 ++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/src/components/wallet/event_logs.rs b/src/components/wallet/event_logs.rs index 1f14a60..d772f12 100644 --- a/src/components/wallet/event_logs.rs +++ b/src/components/wallet/event_logs.rs @@ -1,4 +1,5 @@ use color_eyre::Result; +use crossterm::event::{KeyCode, KeyEvent}; use ratatui::{ layout::{Alignment, Constraint, Margin, Rect}, style::{Color, Style}, @@ -51,13 +52,62 @@ impl EventLogs { self.table_state.select(Some(self.logs.len() - 1)); self.scroll_state = self.scroll_state.content_length(self.logs.len()); } + + fn first_row(&mut self) { + if self.logs.len() > 0 { + self.table_state.select(Some(0)); + self.scroll_state = self.scroll_state.position(0); + } + } + + fn next_row(&mut self) { + let i = match self.table_state.selected() { + Some(i) => { + if i >= self.logs.len() - 1 { + i + } else { + i + 1 + } + }, + None => 0, + }; + self.table_state.select(Some(i)); + self.scroll_state = self.scroll_state.position(i); + } + + fn last_row(&mut self) { + if self.logs.len() > 0 { + let last = self.logs.len() - 1; + self.table_state.select(Some(last)); + self.scroll_state = self.scroll_state.position(last); + } + } + + fn previous_row(&mut self) { + let i = match self.table_state.selected() { + Some(i) => { + if i == 0 { + 0 + } else { + i - 1 + } + }, + None => 0 + }; + self.table_state.select(Some(i)); + self.scroll_state = self.scroll_state.position(i); + } } impl PartialComponent for EventLogs { fn set_active(&mut self, current_tab: CurrentTab) { match current_tab { CurrentTab::EventLogs => self.is_active = true, - _ => self.is_active = false, + _ => { + self.is_active = false; + self.table_state.select(None); + self.scroll_state = self.scroll_state.position(0); + } } } } @@ -71,10 +121,23 @@ impl Component for EventLogs { self.palette.with_hover_border_style(style.get("hover_border_style").copied()); self.palette.with_normal_title_style(style.get("normal_title_style").copied()); self.palette.with_hover_title_style(style.get("hover_title_style").copied()); + self.palette.with_highlight_style(style.get("highlight_style").copied()); + self.palette.with_scrollbar_style(style.get("scrollbar_style").copied()); } Ok(()) } + fn handle_key_event(&mut self, key: KeyEvent) -> Result> { + match key.code { + KeyCode::Up | KeyCode::Char('k') if self.is_active => self.previous_row(), + KeyCode::Down | KeyCode::Char('j') if self.is_active => self.next_row(), + KeyCode::Char('g') if self.is_active => self.first_row(), + KeyCode::Char('G') if self.is_active => self.last_row(), + _ => {}, + }; + Ok(None) + } + fn update(&mut self, action: Action) -> Result> { match action { Action::WalletLog(message, level) => self.add_new_log(message, level), @@ -111,6 +174,7 @@ impl Component for EventLogs { ], ) .column_spacing(1) + .highlight_style(self.palette.create_highlight_style()) .block(Block::bordered() .border_style(border_style) .border_type(border_type)