Skip to content

Commit

Permalink
refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
adgaultier committed Sep 29, 2024
1 parent fe3133a commit c94047e
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 161 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ show interface:

# Run oryx
run:
cargo xtask run --release
cargo xtask run

# Build oryx
build:
Expand Down
4 changes: 3 additions & 1 deletion oryx-tui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ kanal = "0.1.0-pre8"
mimalloc = "0.1"
clap = { version = "4", features = ["derive", "cargo"] }
network-types = "0.0.7"
enum_dispatch ="0.3.13"

[[bin]]
name = "oryx"
path = "src/main.rs"
path = "src/main.rs"
Empty file removed oryx-tui/src/app_.rs
Empty file.
48 changes: 31 additions & 17 deletions oryx-tui/src/filters/direction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use ratatui::{
Frame,
};

use crate::app::FocusedBlock;
use crate::{app::FocusedBlock, MenuComponent, Scrollable};

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

Expand Down Expand Up @@ -47,6 +47,36 @@ impl Default for TrafficDirectionFilter {
}
}

impl MenuComponent for TrafficDirectionFilter {
fn set_state(&mut self, value: Option<usize>) {
self.state.select(value);
}
fn select(&mut self) {
if let Some(i) = self.state.selected() {
let traffic_direction = match i {
0 => TrafficDirection::Ingress,
_ => TrafficDirection::Egress,
};

if self.selected_direction.contains(&traffic_direction) {
self.selected_direction
.retain(|&direction| direction != traffic_direction);
} else {
self.selected_direction.push(traffic_direction);
}
}
}
}
impl Scrollable for TrafficDirectionFilter {
fn scroll_down(&mut self) {
self.state.select(Some(1));
}

fn scroll_up(&mut self) {
self.state.select(Some(0));
}
}

impl TrafficDirectionFilter {
pub fn new() -> Self {
TrafficDirectionFilter {
Expand All @@ -65,22 +95,6 @@ impl TrafficDirectionFilter {
}
}

pub fn select(&mut self) {
if let Some(i) = self.state.selected() {
let traffic_direction = match i {
0 => TrafficDirection::Ingress,
_ => TrafficDirection::Egress,
};

if self.selected_direction.contains(&traffic_direction) {
self.selected_direction
.retain(|&direction| direction != traffic_direction);
} else {
self.selected_direction.push(traffic_direction);
}
}
}

pub fn apply(&mut self) {
self.applied_direction = self.selected_direction.clone();
self.selected_direction.clear();
Expand Down
33 changes: 20 additions & 13 deletions oryx-tui/src/filters/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ratatui::{
Frame,
};

use crate::app::FocusedBlock;
use crate::{app::FocusedBlock, MenuComponent, Scrollable};

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

Expand All @@ -22,17 +22,12 @@ impl Default for LinkFilter {
Self::new()
}
}

impl LinkFilter {
pub fn new() -> Self {
Self {
state: TableState::default(),
selected_protocols: vec![LinkProtocol::Arp],
applied_protocols: Vec::new(),
}
impl MenuComponent for LinkFilter {
fn set_state(&mut self, value: Option<usize>) {
self.state.select(value);
}

pub fn select(&mut self) {
fn select(&mut self) {
if self.state.selected().is_some() {
let protocol = LinkProtocol::Arp;
if self.selected_protocols.contains(&protocol) {
Expand All @@ -42,8 +37,9 @@ impl LinkFilter {
}
}
}

pub fn scroll_down(&mut self) {
}
impl Scrollable for LinkFilter {
fn scroll_down(&mut self) {
let i = match self.state.selected() {
Some(i) => {
if i < (NB_LINK_PROTOCOL - 1).into() {
Expand All @@ -58,7 +54,7 @@ impl LinkFilter {
self.state.select(Some(i));
}

pub fn scroll_up(&mut self) {
fn scroll_up(&mut self) {
let i = match self.state.selected() {
Some(i) => {
if i > 1 {
Expand All @@ -72,6 +68,17 @@ impl LinkFilter {

self.state.select(Some(i));
}
}

impl LinkFilter {
pub fn new() -> Self {
Self {
state: TableState::default(),
selected_protocols: vec![LinkProtocol::Arp],
applied_protocols: Vec::new(),
}
}

pub fn apply(&mut self) {
self.applied_protocols = self.selected_protocols.clone();
self.selected_protocols.clear();
Expand Down
39 changes: 22 additions & 17 deletions oryx-tui/src/filters/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ratatui::{
Frame,
};

use crate::app::FocusedBlock;
use crate::{app::FocusedBlock, MenuComponent, Scrollable};

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

Expand All @@ -22,21 +22,12 @@ impl Default for NetworkFilter {
Self::new()
}
}

impl NetworkFilter {
pub fn new() -> Self {
NetworkFilter {
state: TableState::default(),
selected_protocols: vec![
NetworkProtocol::Ipv4,
NetworkProtocol::Ipv6,
NetworkProtocol::Icmp,
],
applied_protocols: Vec::new(),
}
impl MenuComponent for NetworkFilter {
fn set_state(&mut self, value: Option<usize>) {
self.state.select(value);
}

pub fn select(&mut self) {
fn select(&mut self) {
if let Some(i) = self.state.selected() {
let protocol = match i {
0 => NetworkProtocol::Ipv4,
Expand All @@ -51,8 +42,9 @@ impl NetworkFilter {
}
}
}

pub fn scroll_down(&mut self) {
}
impl Scrollable for NetworkFilter {
fn scroll_down(&mut self) {
let i = match self.state.selected() {
Some(i) => {
if i < (NB_NETWORK_PROTOCOL - 1).into() {
Expand All @@ -67,7 +59,7 @@ impl NetworkFilter {
self.state.select(Some(i));
}

pub fn scroll_up(&mut self) {
fn scroll_up(&mut self) {
let i = match self.state.selected() {
Some(i) => {
if i > 1 {
Expand All @@ -81,6 +73,19 @@ impl NetworkFilter {

self.state.select(Some(i));
}
}
impl NetworkFilter {
pub fn new() -> Self {
NetworkFilter {
state: TableState::default(),
selected_protocols: vec![
NetworkProtocol::Ipv4,
NetworkProtocol::Ipv6,
NetworkProtocol::Icmp,
],
applied_protocols: Vec::new(),
}
}

pub fn apply(&mut self) {
self.applied_protocols = self.selected_protocols.clone();
Expand Down
69 changes: 26 additions & 43 deletions oryx-tui/src/filters/start_menu.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::app::{App, FocusedBlock};

use crate::ScrollableMenuComponent;
use crossterm::event::{KeyCode, KeyEvent};

use ratatui::prelude::Stylize;
use ratatui::{
layout::{Constraint, Direction, Flex, Layout},
style::Style,
widgets::TableState,
Frame,
};
use tui_big_text::{BigText, PixelSize};
Expand All @@ -21,7 +23,7 @@ pub enum StartMenuBlock {

impl StartMenuBlock {
pub fn next(self, app: &mut App) {
self.on_unselect(app);
self.set_state(app, None);
let x = match self {
StartMenuBlock::Interface => StartMenuBlock::TransportFilter,
StartMenuBlock::TransportFilter => StartMenuBlock::NetworkFilter,
Expand All @@ -31,10 +33,10 @@ impl StartMenuBlock {
StartMenuBlock::Start => StartMenuBlock::Interface,
};
app.focused_block = FocusedBlock::StartMenuBlock(x);
x.on_select(app)
x.set_state(app, Some(0));
}
pub fn previous(self, app: &mut App) {
self.on_unselect(app);
self.set_state(app, None);
let x = match self {
StartMenuBlock::Interface => StartMenuBlock::Start,
StartMenuBlock::TransportFilter => StartMenuBlock::Interface,
Expand All @@ -44,69 +46,50 @@ impl StartMenuBlock {
StartMenuBlock::Start => StartMenuBlock::TrafficDirection,
};
app.focused_block = FocusedBlock::StartMenuBlock(x);
x.on_select(app)
x.set_state(app, Some(0));
}

pub fn app_component(self, app: &mut App) -> Option<&mut TableState> {
fn app_component(self, app: &mut App) -> Option<Box<&mut dyn ScrollableMenuComponent>> {
match self {
StartMenuBlock::Interface => Some(&mut app.interface.state),
StartMenuBlock::TransportFilter => Some(&mut (*app).filter.transport.state),
StartMenuBlock::NetworkFilter => Some(&mut (*app).filter.network.state),
StartMenuBlock::LinkFilter => Some(&mut (*app).filter.link.state),
StartMenuBlock::TrafficDirection => Some(&mut (*app).filter.traffic_direction.state),
StartMenuBlock::Interface => Some(Box::new(&mut app.interface)),
StartMenuBlock::TransportFilter => Some(Box::new(&mut app.filter.transport)),
StartMenuBlock::NetworkFilter => Some(Box::new(&mut app.filter.network)),
StartMenuBlock::LinkFilter => Some(Box::new(&mut app.filter.link)),
StartMenuBlock::TrafficDirection => Some(Box::new(&mut app.filter.traffic_direction)),
StartMenuBlock::Start => None,
}
}

fn on_select(self, app: &mut App) {
match self.app_component(app) {
Some(p) => {
p.select(Some(0));
}
None => {}
}
}
fn on_unselect(self, app: &mut App) {
fn set_state(self, app: &mut App, value: Option<usize>) {
match self.app_component(app) {
Some(p) => {
p.select(None);
}
None => {}
Some(p) => p.set_state(value),
_ => {}
}
}

pub fn scroll_up(self, app: &mut App) {
match self {
StartMenuBlock::Interface => app.interface.scroll_up(),
StartMenuBlock::TransportFilter => (*app).filter.transport.scroll_up(),
StartMenuBlock::NetworkFilter => (*app).filter.network.scroll_up(),
StartMenuBlock::LinkFilter => (*app).filter.link.scroll_up(),
StartMenuBlock::TrafficDirection => {
(*app).filter.traffic_direction.state.select(Some(0))
}
match self.app_component(app) {
Some(p) => p.scroll_up(),
_ => {}
}
}

pub fn scroll_down(self, app: &mut App) {
match self {
StartMenuBlock::Interface => app.interface.scroll_down(),
StartMenuBlock::TransportFilter => (*app).filter.transport.scroll_down(),
StartMenuBlock::NetworkFilter => (*app).filter.network.scroll_down(),
StartMenuBlock::LinkFilter => (*app).filter.link.scroll_down(),
StartMenuBlock::TrafficDirection => {
(*app).filter.traffic_direction.state.select(Some(1))
}
match self.app_component(app) {
Some(p) => p.scroll_down(),
_ => {}
}
}

pub fn handle_key_events(&mut self, key_event: KeyEvent, app: &mut App) {
match key_event.code {
KeyCode::Tab => self.next(app),
KeyCode::BackTab => self.previous(app),

KeyCode::Char('k') | KeyCode::Up => self.scroll_up(app),

KeyCode::Char('j') | KeyCode::Down => self.scroll_down(app),
KeyCode::Char(' ') => match self.app_component(app) {
Some(p) => p.select(),
_ => {}
},

_ => {}
}
Expand Down
Loading

0 comments on commit c94047e

Please sign in to comment.