Skip to content

Commit 234a2a6

Browse files
committed
add table scrollbar & select/scroll actions
1 parent ff4c71c commit 234a2a6

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

src/components/discovery.rs

+38-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use cidr::Ipv4Cidr;
22
use color_eyre::eyre::Result;
3+
use color_eyre::owo_colors::OwoColorize;
34
use dns_lookup::{lookup_addr, lookup_host};
45
use futures::future::join_all;
56

@@ -105,12 +106,10 @@ impl Discovery {
105106
let active_interface = self.active_interface.clone().unwrap();
106107

107108
let ipv4 = active_interface
108-
.clone()
109109
.ips
110110
.iter()
111111
.find(|f| f.is_ipv4())
112-
.unwrap()
113-
.clone();
112+
.unwrap();
114113
let source_ip: Ipv4Addr = ipv4.ip().to_string().parse().unwrap();
115114

116115
let (mut sender, _) = match pnet::datalink::channel(&active_interface, Default::default()) {
@@ -278,7 +277,9 @@ impl Discovery {
278277
}
279278

280279
fn set_scrollbar_height(&mut self) {
281-
self.scrollbar_state.content_length(self.scanned_ips.len() - 1);
280+
self.scrollbar_state = self
281+
.scrollbar_state
282+
.content_length(self.scanned_ips.len() - 1);
282283
}
283284

284285
fn previous_in_table(&mut self) {
@@ -293,7 +294,7 @@ impl Discovery {
293294
None => 0,
294295
};
295296
self.table_state.select(Some(index));
296-
// self.
297+
self.scrollbar_state = self.scrollbar_state.position(index);
297298
}
298299

299300
fn next_in_table(&mut self) {
@@ -308,7 +309,7 @@ impl Discovery {
308309
None => 0,
309310
};
310311
self.table_state.select(Some(index));
311-
// self.
312+
self.scrollbar_state = self.scrollbar_state.position(index);
312313
}
313314

314315
fn make_table(scanned_ips: Vec<ScannedIp>, ip_num: i32) -> Table<'static> {
@@ -357,20 +358,34 @@ impl Discovery {
357358
.position(ratatui::widgets::block::Position::Top)
358359
.alignment(Alignment::Left),
359360
)
361+
.title(
362+
ratatui::widgets::block::Title::from(Line::from(vec![
363+
Span::styled("|", Style::default().fg(Color::Yellow)),
364+
String::from(char::from_u32(0x25b2).unwrap_or('>')).red(),
365+
String::from(char::from_u32(0x25bc).unwrap_or('>')).red(),
366+
Span::styled("Select|", Style::default().fg(Color::Yellow)),
367+
]))
368+
.position(ratatui::widgets::block::Position::Bottom)
369+
.alignment(Alignment::Right),
370+
)
360371
.border_style(Style::default().fg(Color::Rgb(100, 100, 100)))
361-
.borders(Borders::ALL)
362-
.padding(Padding::new(1, 0, 2, 0)),
372+
.borders(Borders::ALL), // .padding(Padding::new(1, 0, 2, 0)),
363373
)
364374
.highlight_symbol(String::from(char::from_u32(0x25b7).unwrap_or('>')).red())
365375
.column_spacing(1);
366376
table
367377
}
368378

369-
fn make_scrollbar(&mut self) -> Scrollbar {
379+
pub fn make_scrollbar<'a>() -> Scrollbar<'a> {
380+
// let s_start = String::from(char::from_u32(0x25b2).unwrap_or('#'));
381+
// let s_end = String::from(char::from_u32(0x25bc).unwrap_or('#'));
370382
let scrollbar = Scrollbar::default()
371383
.orientation(ScrollbarOrientation::VerticalRight)
384+
.style(Style::default().fg(Color::Rgb(100, 100, 100)))
372385
.begin_symbol(None)
373386
.end_symbol(None);
387+
// .begin_symbol(Some(s_start))
388+
// .end_symbol(Some(s_end));
374389
scrollbar
375390
}
376391

@@ -421,7 +436,7 @@ impl Discovery {
421436

422437
impl Component for Discovery {
423438
fn init(&mut self, area: Rect) -> Result<()> {
424-
if self.cidr == None {
439+
if self.cidr.is_none() {
425440
let cidr_range = "192.168.1.0/24";
426441
self.set_cidr(String::from(cidr_range), false);
427442
}
@@ -480,7 +495,7 @@ impl Component for Discovery {
480495
if let Action::ActiveInterface(ref interface) = action {
481496
let intf = interface.clone();
482497
// -- first time scan after setting of interface
483-
if self.active_interface == None {
498+
if self.active_interface.is_none() {
484499
self.scan();
485500
}
486501
self.active_interface = Some(intf);
@@ -518,7 +533,18 @@ impl Component for Discovery {
518533
f.render_stateful_widget(table, table_rect, &mut self.table_state.clone());
519534

520535
// -- SCROLLBAR
521-
// let scrollbar = self.make_scrollbar();
536+
let scrollbar = Self::make_scrollbar();
537+
let mut scroll_rect = table_rect;
538+
scroll_rect.y += 3;
539+
scroll_rect.height -= 3;
540+
f.render_stateful_widget(
541+
scrollbar,
542+
scroll_rect.inner(&Margin {
543+
vertical: 1,
544+
horizontal: 1,
545+
}),
546+
&mut self.scrollbar_state,
547+
);
522548

523549
// -- ERROR
524550
if self.cidr_error {

0 commit comments

Comments
 (0)