ghost-eye/src/components/validator/peers.rs
Uncle Stretch bae21bb505
rustfmt whole project
Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
2025-11-13 17:10:08 +03:00

208 lines
6.5 KiB
Rust

use color_eyre::Result;
use crossterm::event::{KeyCode, KeyEvent};
use ratatui::layout::{Constraint, Margin};
use ratatui::{
layout::{Alignment, Rect},
text::Text,
widgets::{
Block, Cell, Padding, Row, Scrollbar, ScrollbarOrientation, ScrollbarState, Table,
TableState,
},
Frame,
};
use super::{Component, CurrentTab, PartialComponent};
use crate::types::PeerInformation;
use crate::{action::Action, config::Config, palette::StylePalette};
pub struct Peers {
is_active: bool,
palette: StylePalette,
scroll_state: ScrollbarState,
table_state: TableState,
peers: Vec<PeerInformation>,
}
impl Default for Peers {
fn default() -> Self {
Self::new()
}
}
impl Peers {
pub fn new() -> Self {
Self {
is_active: false,
scroll_state: ScrollbarState::new(0),
table_state: TableState::new(),
peers: Vec::new(),
palette: StylePalette::default(),
}
}
fn update_peers(&mut self, peers: Vec<PeerInformation>) {
if self.peers.len() > peers.len() {
if let Some(_) = self.table_state.selected() {
self.last_row();
}
}
self.peers = peers;
self.scroll_state = self.scroll_state.content_length(self.peers.len());
}
fn first_row(&mut self) {
if self.peers.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.peers.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.peers.len() > 0 {
let last = self.peers.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 Peers {
fn set_active(&mut self, current_tab: CurrentTab) {
match current_tab {
CurrentTab::Peers => self.is_active = true,
_ => {
self.is_active = false;
self.table_state.select(None);
self.scroll_state = self.scroll_state.position(0);
}
}
}
}
impl Component for Peers {
fn register_config_handler(&mut self, config: Config) -> Result<()> {
if let Some(style) = config.styles.get(&crate::app::Mode::Validator) {
self.palette
.with_normal_style(style.get("normal_style").copied());
self.palette
.with_hover_style(style.get("hover_style").copied());
self.palette
.with_normal_border_style(style.get("normal_border_style").copied());
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 update(&mut self, action: Action) -> Result<Option<Action>> {
match action {
Action::SetConnectedPeers(peers) => self.update_peers(peers),
_ => {}
};
Ok(None)
}
fn handle_key_event(&mut self, key: KeyEvent) -> Result<Option<Action>> {
if self.is_active {
match key.code {
KeyCode::Up | KeyCode::Char('k') => self.previous_row(),
KeyCode::Down | KeyCode::Char('j') => self.next_row(),
KeyCode::Char('g') => self.first_row(),
KeyCode::Char('G') => self.last_row(),
_ => {}
};
}
Ok(None)
}
fn draw(&mut self, frame: &mut Frame, area: Rect) -> Result<()> {
let [_, _, place, _] = super::validator_layout(area);
let (border_style, border_type) = self.palette.create_border_style(self.is_active);
let table = Table::new(
self.peers.iter().map(|info| {
Row::new(vec![
Cell::from(Text::from(info.peer_id.clone()).alignment(Alignment::Left)),
Cell::from(Text::from(info.roles.clone()).alignment(Alignment::Center)),
Cell::from(Text::from(info.best_hash.to_string()).alignment(Alignment::Center)),
Cell::from(
Text::from(info.best_number.to_string()).alignment(Alignment::Right),
),
])
}),
[
Constraint::Fill(1),
Constraint::Length(11),
Constraint::Length(11),
Constraint::Length(11),
],
)
.highlight_style(self.palette.create_highlight_style())
.column_spacing(1)
.block(
Block::bordered()
.border_style(border_style)
.border_type(border_type)
.padding(Padding::right(2))
.title_alignment(Alignment::Right)
.title_style(self.palette.create_title_style(false))
.title("My Peers"),
);
let scrollbar = Scrollbar::default()
.orientation(ScrollbarOrientation::VerticalRight)
.begin_symbol(None)
.end_symbol(None)
.style(self.palette.create_scrollbar_style());
frame.render_stateful_widget(table, place, &mut self.table_state);
frame.render_stateful_widget(
scrollbar,
place.inner(Margin {
vertical: 1,
horizontal: 1,
}),
&mut self.scroll_state,
);
Ok(())
}
}