From b2ff0626e5e21e162604599be15ee77528cc6905 Mon Sep 17 00:00:00 2001 From: Badr Date: Wed, 25 Sep 2024 20:57:18 +0200 Subject: [PATCH] refactor interface --- oryx-tui/src/app.rs | 61 +----------------------- oryx-tui/src/handler.rs | 26 +---------- oryx-tui/src/interface.rs | 98 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 99 insertions(+), 86 deletions(-) diff --git a/oryx-tui/src/app.rs b/oryx-tui/src/app.rs index dcf3a3d..0ac0f46 100644 --- a/oryx-tui/src/app.rs +++ b/oryx-tui/src/app.rs @@ -271,7 +271,7 @@ impl App { // interfaces self.interface - .render(frame, interface_block, &self.focused_block); + .render_on_setup(frame, interface_block, &self.focused_block); // Filters self.filter @@ -309,64 +309,7 @@ impl App { }; // Interface - let widths = [Constraint::Length(4), Constraint::Fill(1)]; - - let interface_infos = [ - Row::new(vec![ - Span::styled("Name", Style::new().bold()), - Span::from(self.interface.selected_interface.name.clone()), - ]), - Row::new(vec![ - Span::styled("Mac", Style::new().bold()), - Span::from( - self.interface - .selected_interface - .mac_address - .clone() - .unwrap_or("-".to_string()), - ), - ]), - Row::new(vec![ - Span::styled("IPv4", Style::new().bold()), - Span::from( - self.interface - .selected_interface - .addresses - .iter() - .find(|a| matches!(a, IpAddr::V4(_) | IpAddr::V6(_))) - .unwrap() - .to_string(), - ), - ]), - Row::new(vec![ - Span::styled("IPv6", Style::new().bold()), - Span::from({ - match self - .interface - .selected_interface - .addresses - .iter() - .find(|a| matches!(a, IpAddr::V6(_))) - { - Some(ip) => ip.to_string(), - None => "-".to_string(), - } - }), - ]), - ]; - - let interface_table = Table::new(interface_infos, widths).column_spacing(3).block( - Block::default() - .title(" Interface 󰲝 ") - .title_style(Style::default().bold().green()) - .title_alignment(Alignment::Center) - .padding(Padding::horizontal(2)) - .borders(Borders::ALL) - .style(Style::default()) - .border_type(BorderType::default()) - .border_style(Style::default().green()), - ); - frame.render_widget(interface_table, interface_block); + self.interface.render_on_sniffing(frame, interface_block); // Filters self.filter.render_on_sniffing(frame, filter_block); diff --git a/oryx-tui/src/handler.rs b/oryx-tui/src/handler.rs index ecee965..d241345 100644 --- a/oryx-tui/src/handler.rs +++ b/oryx-tui/src/handler.rs @@ -907,18 +907,7 @@ pub fn handle_key_events( } else { match &app.focused_block { FocusedBlock::Interface => { - let i = match app.interface.state.selected() { - Some(i) => { - if i < app.interface.interfaces.len() - 1 { - i + 1 - } else { - i - } - } - None => 0, - }; - - app.interface.state.select(Some(i)); + app.interface.scroll_down(); } FocusedBlock::NetworkFilter => { @@ -975,18 +964,7 @@ pub fn handle_key_events( } else { match &app.focused_block { FocusedBlock::Interface => { - let i = match app.interface.state.selected() { - Some(i) => { - if i > 1 { - i - 1 - } else { - 0 - } - } - None => 0, - }; - - app.interface.state.select(Some(i)); + app.interface.scroll_up(); } FocusedBlock::NetworkFilter => { app.filter.network.scroll_up(); diff --git a/oryx-tui/src/interface.rs b/oryx-tui/src/interface.rs index b640f9f..d3cde0f 100644 --- a/oryx-tui/src/interface.rs +++ b/oryx-tui/src/interface.rs @@ -2,8 +2,8 @@ use libc::{AF_INET, AF_INET6, IFF_UP}; use ratatui::{ layout::{Alignment, Constraint, Direction, Flex, Layout, Rect}, style::{Color, Style, Stylize}, - text::Line, - widgets::{Block, BorderType, Borders, Row, Table, TableState}, + text::{Line, Span}, + widgets::{Block, BorderType, Borders, Padding, Row, Table, TableState}, Frame, }; @@ -122,7 +122,41 @@ impl Interface { } } - pub fn render(&mut self, frame: &mut Frame, block: Rect, focused_block: &FocusedBlock) { + pub fn scroll_down(&mut self) { + let i = match self.state.selected() { + Some(i) => { + if i < self.interfaces.len() - 1 { + i + 1 + } else { + i + } + } + None => 0, + }; + + self.state.select(Some(i)); + } + pub fn scroll_up(&mut self) { + let i = match self.state.selected() { + Some(i) => { + if i > 1 { + i - 1 + } else { + 0 + } + } + None => 0, + }; + + self.state.select(Some(i)); + } + + pub fn render_on_setup( + &mut self, + frame: &mut Frame, + block: Rect, + focused_block: &FocusedBlock, + ) { let layout = Layout::default() .direction(Direction::Horizontal) .constraints([ @@ -210,4 +244,62 @@ impl Interface { &mut self.state, ); } + + pub fn render_on_sniffing(&mut self, frame: &mut Frame, block: Rect) { + let widths = [Constraint::Length(4), Constraint::Fill(1)]; + + let interface_infos = [ + Row::new(vec![ + Span::styled("Name", Style::new().bold()), + Span::from(self.selected_interface.name.clone()), + ]), + Row::new(vec![ + Span::styled("Mac", Style::new().bold()), + Span::from( + self.selected_interface + .mac_address + .clone() + .unwrap_or("-".to_string()), + ), + ]), + Row::new(vec![ + Span::styled("IPv4", Style::new().bold()), + Span::from( + self.selected_interface + .addresses + .iter() + .find(|a| matches!(a, IpAddr::V4(_) | IpAddr::V6(_))) + .unwrap() + .to_string(), + ), + ]), + Row::new(vec![ + Span::styled("IPv6", Style::new().bold()), + Span::from({ + match self + .selected_interface + .addresses + .iter() + .find(|a| matches!(a, IpAddr::V6(_))) + { + Some(ip) => ip.to_string(), + None => "-".to_string(), + } + }), + ]), + ]; + + let table = Table::new(interface_infos, widths).column_spacing(3).block( + Block::default() + .title(" Interface 󰲝 ") + .title_style(Style::default().bold().green()) + .title_alignment(Alignment::Center) + .padding(Padding::horizontal(2)) + .borders(Borders::ALL) + .style(Style::default()) + .border_type(BorderType::default()) + .border_style(Style::default().green()), + ); + frame.render_widget(table, block); + } }