Skip to content

Commit c94047e

Browse files
committed
refacto
1 parent fe3133a commit c94047e

File tree

13 files changed

+204
-161
lines changed

13 files changed

+204
-161
lines changed

Cargo.lock

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ show interface:
1616

1717
# Run oryx
1818
run:
19-
cargo xtask run --release
19+
cargo xtask run
2020

2121
# Build oryx
2222
build:

oryx-tui/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ kanal = "0.1.0-pre8"
2424
mimalloc = "0.1"
2525
clap = { version = "4", features = ["derive", "cargo"] }
2626
network-types = "0.0.7"
27+
enum_dispatch ="0.3.13"
28+
2729
[[bin]]
2830
name = "oryx"
29-
path = "src/main.rs"
31+
path = "src/main.rs"

oryx-tui/src/app_.rs

Whitespace-only changes.

oryx-tui/src/filters/direction.rs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use ratatui::{
1313
Frame,
1414
};
1515

16-
use crate::app::FocusedBlock;
16+
use crate::{app::FocusedBlock, MenuComponent, Scrollable};
1717

1818
use super::{start_menu::StartMenuBlock, update_menu::UpdateFilterMenuBlock};
1919

@@ -47,6 +47,36 @@ impl Default for TrafficDirectionFilter {
4747
}
4848
}
4949

50+
impl MenuComponent for TrafficDirectionFilter {
51+
fn set_state(&mut self, value: Option<usize>) {
52+
self.state.select(value);
53+
}
54+
fn select(&mut self) {
55+
if let Some(i) = self.state.selected() {
56+
let traffic_direction = match i {
57+
0 => TrafficDirection::Ingress,
58+
_ => TrafficDirection::Egress,
59+
};
60+
61+
if self.selected_direction.contains(&traffic_direction) {
62+
self.selected_direction
63+
.retain(|&direction| direction != traffic_direction);
64+
} else {
65+
self.selected_direction.push(traffic_direction);
66+
}
67+
}
68+
}
69+
}
70+
impl Scrollable for TrafficDirectionFilter {
71+
fn scroll_down(&mut self) {
72+
self.state.select(Some(1));
73+
}
74+
75+
fn scroll_up(&mut self) {
76+
self.state.select(Some(0));
77+
}
78+
}
79+
5080
impl TrafficDirectionFilter {
5181
pub fn new() -> Self {
5282
TrafficDirectionFilter {
@@ -65,22 +95,6 @@ impl TrafficDirectionFilter {
6595
}
6696
}
6797

68-
pub fn select(&mut self) {
69-
if let Some(i) = self.state.selected() {
70-
let traffic_direction = match i {
71-
0 => TrafficDirection::Ingress,
72-
_ => TrafficDirection::Egress,
73-
};
74-
75-
if self.selected_direction.contains(&traffic_direction) {
76-
self.selected_direction
77-
.retain(|&direction| direction != traffic_direction);
78-
} else {
79-
self.selected_direction.push(traffic_direction);
80-
}
81-
}
82-
}
83-
8498
pub fn apply(&mut self) {
8599
self.applied_direction = self.selected_direction.clone();
86100
self.selected_direction.clear();

oryx-tui/src/filters/link.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use ratatui::{
66
Frame,
77
};
88

9-
use crate::app::FocusedBlock;
9+
use crate::{app::FocusedBlock, MenuComponent, Scrollable};
1010

1111
use super::{start_menu::StartMenuBlock, update_menu::UpdateFilterMenuBlock};
1212

@@ -22,17 +22,12 @@ impl Default for LinkFilter {
2222
Self::new()
2323
}
2424
}
25-
26-
impl LinkFilter {
27-
pub fn new() -> Self {
28-
Self {
29-
state: TableState::default(),
30-
selected_protocols: vec![LinkProtocol::Arp],
31-
applied_protocols: Vec::new(),
32-
}
25+
impl MenuComponent for LinkFilter {
26+
fn set_state(&mut self, value: Option<usize>) {
27+
self.state.select(value);
3328
}
3429

35-
pub fn select(&mut self) {
30+
fn select(&mut self) {
3631
if self.state.selected().is_some() {
3732
let protocol = LinkProtocol::Arp;
3833
if self.selected_protocols.contains(&protocol) {
@@ -42,8 +37,9 @@ impl LinkFilter {
4237
}
4338
}
4439
}
45-
46-
pub fn scroll_down(&mut self) {
40+
}
41+
impl Scrollable for LinkFilter {
42+
fn scroll_down(&mut self) {
4743
let i = match self.state.selected() {
4844
Some(i) => {
4945
if i < (NB_LINK_PROTOCOL - 1).into() {
@@ -58,7 +54,7 @@ impl LinkFilter {
5854
self.state.select(Some(i));
5955
}
6056

61-
pub fn scroll_up(&mut self) {
57+
fn scroll_up(&mut self) {
6258
let i = match self.state.selected() {
6359
Some(i) => {
6460
if i > 1 {
@@ -72,6 +68,17 @@ impl LinkFilter {
7268

7369
self.state.select(Some(i));
7470
}
71+
}
72+
73+
impl LinkFilter {
74+
pub fn new() -> Self {
75+
Self {
76+
state: TableState::default(),
77+
selected_protocols: vec![LinkProtocol::Arp],
78+
applied_protocols: Vec::new(),
79+
}
80+
}
81+
7582
pub fn apply(&mut self) {
7683
self.applied_protocols = self.selected_protocols.clone();
7784
self.selected_protocols.clear();

oryx-tui/src/filters/network.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use ratatui::{
66
Frame,
77
};
88

9-
use crate::app::FocusedBlock;
9+
use crate::{app::FocusedBlock, MenuComponent, Scrollable};
1010

1111
use super::{start_menu::StartMenuBlock, update_menu::UpdateFilterMenuBlock};
1212

@@ -22,21 +22,12 @@ impl Default for NetworkFilter {
2222
Self::new()
2323
}
2424
}
25-
26-
impl NetworkFilter {
27-
pub fn new() -> Self {
28-
NetworkFilter {
29-
state: TableState::default(),
30-
selected_protocols: vec![
31-
NetworkProtocol::Ipv4,
32-
NetworkProtocol::Ipv6,
33-
NetworkProtocol::Icmp,
34-
],
35-
applied_protocols: Vec::new(),
36-
}
25+
impl MenuComponent for NetworkFilter {
26+
fn set_state(&mut self, value: Option<usize>) {
27+
self.state.select(value);
3728
}
3829

39-
pub fn select(&mut self) {
30+
fn select(&mut self) {
4031
if let Some(i) = self.state.selected() {
4132
let protocol = match i {
4233
0 => NetworkProtocol::Ipv4,
@@ -51,8 +42,9 @@ impl NetworkFilter {
5142
}
5243
}
5344
}
54-
55-
pub fn scroll_down(&mut self) {
45+
}
46+
impl Scrollable for NetworkFilter {
47+
fn scroll_down(&mut self) {
5648
let i = match self.state.selected() {
5749
Some(i) => {
5850
if i < (NB_NETWORK_PROTOCOL - 1).into() {
@@ -67,7 +59,7 @@ impl NetworkFilter {
6759
self.state.select(Some(i));
6860
}
6961

70-
pub fn scroll_up(&mut self) {
62+
fn scroll_up(&mut self) {
7163
let i = match self.state.selected() {
7264
Some(i) => {
7365
if i > 1 {
@@ -81,6 +73,19 @@ impl NetworkFilter {
8173

8274
self.state.select(Some(i));
8375
}
76+
}
77+
impl NetworkFilter {
78+
pub fn new() -> Self {
79+
NetworkFilter {
80+
state: TableState::default(),
81+
selected_protocols: vec![
82+
NetworkProtocol::Ipv4,
83+
NetworkProtocol::Ipv6,
84+
NetworkProtocol::Icmp,
85+
],
86+
applied_protocols: Vec::new(),
87+
}
88+
}
8489

8590
pub fn apply(&mut self) {
8691
self.applied_protocols = self.selected_protocols.clone();

oryx-tui/src/filters/start_menu.rs

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use crate::app::{App, FocusedBlock};
2+
3+
use crate::ScrollableMenuComponent;
24
use crossterm::event::{KeyCode, KeyEvent};
5+
36
use ratatui::prelude::Stylize;
47
use ratatui::{
58
layout::{Constraint, Direction, Flex, Layout},
69
style::Style,
7-
widgets::TableState,
810
Frame,
911
};
1012
use tui_big_text::{BigText, PixelSize};
@@ -21,7 +23,7 @@ pub enum StartMenuBlock {
2123

2224
impl StartMenuBlock {
2325
pub fn next(self, app: &mut App) {
24-
self.on_unselect(app);
26+
self.set_state(app, None);
2527
let x = match self {
2628
StartMenuBlock::Interface => StartMenuBlock::TransportFilter,
2729
StartMenuBlock::TransportFilter => StartMenuBlock::NetworkFilter,
@@ -31,10 +33,10 @@ impl StartMenuBlock {
3133
StartMenuBlock::Start => StartMenuBlock::Interface,
3234
};
3335
app.focused_block = FocusedBlock::StartMenuBlock(x);
34-
x.on_select(app)
36+
x.set_state(app, Some(0));
3537
}
3638
pub fn previous(self, app: &mut App) {
37-
self.on_unselect(app);
39+
self.set_state(app, None);
3840
let x = match self {
3941
StartMenuBlock::Interface => StartMenuBlock::Start,
4042
StartMenuBlock::TransportFilter => StartMenuBlock::Interface,
@@ -44,69 +46,50 @@ impl StartMenuBlock {
4446
StartMenuBlock::Start => StartMenuBlock::TrafficDirection,
4547
};
4648
app.focused_block = FocusedBlock::StartMenuBlock(x);
47-
x.on_select(app)
49+
x.set_state(app, Some(0));
4850
}
4951

50-
pub fn app_component(self, app: &mut App) -> Option<&mut TableState> {
52+
fn app_component(self, app: &mut App) -> Option<Box<&mut dyn ScrollableMenuComponent>> {
5153
match self {
52-
StartMenuBlock::Interface => Some(&mut app.interface.state),
53-
StartMenuBlock::TransportFilter => Some(&mut (*app).filter.transport.state),
54-
StartMenuBlock::NetworkFilter => Some(&mut (*app).filter.network.state),
55-
StartMenuBlock::LinkFilter => Some(&mut (*app).filter.link.state),
56-
StartMenuBlock::TrafficDirection => Some(&mut (*app).filter.traffic_direction.state),
54+
StartMenuBlock::Interface => Some(Box::new(&mut app.interface)),
55+
StartMenuBlock::TransportFilter => Some(Box::new(&mut app.filter.transport)),
56+
StartMenuBlock::NetworkFilter => Some(Box::new(&mut app.filter.network)),
57+
StartMenuBlock::LinkFilter => Some(Box::new(&mut app.filter.link)),
58+
StartMenuBlock::TrafficDirection => Some(Box::new(&mut app.filter.traffic_direction)),
5759
StartMenuBlock::Start => None,
5860
}
5961
}
6062

61-
fn on_select(self, app: &mut App) {
62-
match self.app_component(app) {
63-
Some(p) => {
64-
p.select(Some(0));
65-
}
66-
None => {}
67-
}
68-
}
69-
fn on_unselect(self, app: &mut App) {
63+
fn set_state(self, app: &mut App, value: Option<usize>) {
7064
match self.app_component(app) {
71-
Some(p) => {
72-
p.select(None);
73-
}
74-
None => {}
65+
Some(p) => p.set_state(value),
66+
_ => {}
7567
}
7668
}
69+
7770
pub fn scroll_up(self, app: &mut App) {
78-
match self {
79-
StartMenuBlock::Interface => app.interface.scroll_up(),
80-
StartMenuBlock::TransportFilter => (*app).filter.transport.scroll_up(),
81-
StartMenuBlock::NetworkFilter => (*app).filter.network.scroll_up(),
82-
StartMenuBlock::LinkFilter => (*app).filter.link.scroll_up(),
83-
StartMenuBlock::TrafficDirection => {
84-
(*app).filter.traffic_direction.state.select(Some(0))
85-
}
71+
match self.app_component(app) {
72+
Some(p) => p.scroll_up(),
8673
_ => {}
8774
}
8875
}
89-
9076
pub fn scroll_down(self, app: &mut App) {
91-
match self {
92-
StartMenuBlock::Interface => app.interface.scroll_down(),
93-
StartMenuBlock::TransportFilter => (*app).filter.transport.scroll_down(),
94-
StartMenuBlock::NetworkFilter => (*app).filter.network.scroll_down(),
95-
StartMenuBlock::LinkFilter => (*app).filter.link.scroll_down(),
96-
StartMenuBlock::TrafficDirection => {
97-
(*app).filter.traffic_direction.state.select(Some(1))
98-
}
77+
match self.app_component(app) {
78+
Some(p) => p.scroll_down(),
9979
_ => {}
10080
}
10181
}
82+
10283
pub fn handle_key_events(&mut self, key_event: KeyEvent, app: &mut App) {
10384
match key_event.code {
10485
KeyCode::Tab => self.next(app),
10586
KeyCode::BackTab => self.previous(app),
106-
10787
KeyCode::Char('k') | KeyCode::Up => self.scroll_up(app),
108-
10988
KeyCode::Char('j') | KeyCode::Down => self.scroll_down(app),
89+
KeyCode::Char(' ') => match self.app_component(app) {
90+
Some(p) => p.select(),
91+
_ => {}
92+
},
11093

11194
_ => {}
11295
}

0 commit comments

Comments
 (0)