Skip to content

Commit

Permalink
refactor fuzzy
Browse files Browse the repository at this point in the history
  • Loading branch information
pythops committed Sep 26, 2024
1 parent 0d1c24f commit b7bb3d3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
29 changes: 1 addition & 28 deletions oryx-tui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ impl App {
pub fn new() -> Self {
let packets = Arc::new(Mutex::new(Vec::with_capacity(AppPacket::LEN * 1024 * 1024)));
let stats = Arc::new(Mutex::new(Stats::default()));
let fuzzy = Arc::new(Mutex::new(Fuzzy::default()));

let (sender, receiver) = kanal::unbounded();

Expand Down Expand Up @@ -128,32 +127,6 @@ impl App {
}
});

thread::spawn({
let fuzzy = fuzzy.clone();
let packets = packets.clone();
move || {
let mut last_index = 0;
let mut pattern = String::new();
loop {
thread::sleep(Duration::from_millis(TICK_RATE));
let packets = packets.lock().unwrap();
let mut fuzzy = fuzzy.lock().unwrap();

if fuzzy.is_enabled() && !fuzzy.filter.value().is_empty() {
let current_pattern = fuzzy.filter.value().to_owned();
if current_pattern != pattern {
fuzzy.find(packets.as_slice());
pattern = current_pattern;
last_index = packets.len();
} else {
fuzzy.append(&packets.as_slice()[last_index..]);
last_index = packets.len();
}
}
}
}
});

Self {
running: true,
help: Help::new(),
Expand All @@ -164,7 +137,7 @@ impl App {
start_sniffing: false,
packets: packets.clone(),
packets_table_state: TableState::default(),
fuzzy,
fuzzy: Fuzzy::new(packets.clone()),
notifications: Vec::new(),
manuall_scroll: false,
mode: Mode::Packet,
Expand Down
40 changes: 39 additions & 1 deletion oryx-tui/src/filters/fuzzy.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use std::{
sync::{Arc, Mutex},
thread,
time::Duration,
};

use ratatui::{
style::{Style, Stylize},
text::{Line, Span},
widgets::{Cell, TableState},
};
use tui_input::Input;

use crate::packets::packet::AppPacket;
use crate::{app::TICK_RATE, packets::packet::AppPacket};

#[derive(Debug, Clone, Default)]
pub struct Fuzzy {
Expand All @@ -18,6 +24,38 @@ pub struct Fuzzy {
}

impl Fuzzy {
pub fn new(packets: Arc<Mutex<Vec<AppPacket>>>) -> Arc<Mutex<Self>> {
let fuzzy = Arc::new(Mutex::new(Self::default()));

thread::spawn({
let fuzzy = fuzzy.clone();
let packets = packets.clone();
move || {
let mut last_index = 0;
let mut pattern = String::new();
loop {
thread::sleep(Duration::from_millis(TICK_RATE));
let packets = packets.lock().unwrap();
let mut fuzzy = fuzzy.lock().unwrap();

if fuzzy.is_enabled() && !fuzzy.filter.value().is_empty() {
let current_pattern = fuzzy.filter.value().to_owned();
if current_pattern != pattern {
fuzzy.find(packets.as_slice());
pattern = current_pattern;
last_index = packets.len();
} else {
fuzzy.append(&packets.as_slice()[last_index..]);
last_index = packets.len();
}
}
}
}
});

fuzzy
}

pub fn find(&mut self, packets: &[AppPacket]) {
self.packets = packets
.iter()
Expand Down

0 comments on commit b7bb3d3

Please sign in to comment.