ability to move through wallet logs

Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
This commit is contained in:
Uncle Stretch 2024-12-08 19:18:19 +03:00
parent be92585661
commit db97ad13a6
Signed by: str3tch
GPG Key ID: 84F3190747EE79AA

View File

@ -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<Option<Action>> {
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<Option<Action>> {
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)