From e2f41dc906213f24e1459efaad288062ce26f5b8 Mon Sep 17 00:00:00 2001 From: Badr Date: Sun, 12 Jan 2025 18:06:42 +0100 Subject: [PATCH] add scrollbar --- oryx-tui/src/section/metrics.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/oryx-tui/src/section/metrics.rs b/oryx-tui/src/section/metrics.rs index 99e64cc..f2e4a85 100644 --- a/oryx-tui/src/section/metrics.rs +++ b/oryx-tui/src/section/metrics.rs @@ -14,7 +14,7 @@ use ratatui::{ text::Line, widgets::{ Bar, BarChart, BarGroup, Block, BorderType, Borders, Cell, Clear, HighlightSpacing, - Padding, Row, Table, + Padding, Row, Scrollbar, ScrollbarOrientation, ScrollbarState, Table, }, Frame, }; @@ -125,7 +125,6 @@ impl Metrics { self.metrics[self.state.offset..self.state.offset + self.window_height].to_vec() }; - // for (index, port_count_metric) in self.metrics[self.state.offset..].iter().enumerate() { for (index, port_count_metric) in metrics_to_display.iter().enumerate() { let metric = { port_count_metric.lock().unwrap().clone() }; @@ -179,6 +178,23 @@ impl Metrics { }), ); } + + if self.metrics.len() > self.window_height { + let scrollbar = Scrollbar::new(ScrollbarOrientation::VerticalRight) + .begin_symbol(Some("↑")) + .end_symbol(Some("↓")); + + let mut scrollbar_state = ScrollbarState::new(self.metrics.len()) + .position(self.state.offset * self.window_height); + frame.render_stateful_widget( + scrollbar, + block.inner(Margin { + vertical: 1, + horizontal: 0, + }), + &mut scrollbar_state, + ); + } } pub fn handle_keys(&mut self, key_event: KeyEvent) {