Skip to content

Commit

Permalink
refactor interface
Browse files Browse the repository at this point in the history
  • Loading branch information
pythops committed Sep 25, 2024
1 parent 0b6612b commit b2ff062
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 86 deletions.
61 changes: 2 additions & 59 deletions oryx-tui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
26 changes: 2 additions & 24 deletions oryx-tui/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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();
Expand Down
98 changes: 95 additions & 3 deletions oryx-tui/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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);
}
}

0 comments on commit b2ff062

Please sign in to comment.