Skip to content

Commit

Permalink
switch vec to maxsizevec in wifi chart
Browse files Browse the repository at this point in the history
  • Loading branch information
Chleba committed Feb 25, 2024
1 parent 28caa78 commit d65f8d5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
21 changes: 9 additions & 12 deletions src/components/wifi_chart.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::components::wifi_scan::WifiInfo;
use crate::utils::MaxSizeVec;
use chrono::Timelike;
use color_eyre::eyre::Result;
use pnet::datalink::{self, NetworkInterface};
Expand All @@ -14,7 +15,7 @@ use crate::{action::Action, tui::Frame};
#[derive(Debug)]
pub struct WifiDataset {
ssid: String,
data: Vec<(f64, f64)>,
data: MaxSizeVec<(f64, f64)>,
color: Color,
}

Expand Down Expand Up @@ -58,34 +59,30 @@ impl WifiChart {
let n = &mut self.wifi_datasets[p];
let signal: f64 = w.signal as f64;
n.data.push((self.signal_tick[1], signal * -1.0));
if n.data.len() > 50 {
n.data.remove(0);
}
} else {
self.wifi_datasets.push(WifiDataset {
ssid: w.ssid.clone(),
data: vec![(0.0, 0.0)],
color: w.color.clone(),
// data: vec![(0.0, 0.0)],
data: MaxSizeVec::new(100),
color: w.color,
});
}
}
self.signal_tick[0] += 1.0;
self.signal_tick[1] += 1.0;
}

pub fn make_chart(&mut self) -> Chart {
pub fn make_chart(&self) -> Chart {
let mut datasets = Vec::new();
let mut index = 0;

for d in &self.wifi_datasets {
let d_data = &d.data.get_vec();
let dataset = Dataset::default()
.name(d.ssid.clone())
.marker(symbols::Marker::Dot)
.style(Style::default().fg(d.color.clone()))
.style(Style::default().fg(d.color))
.graph_type(GraphType::Line)
.data(&d.data);
.data(d_data);
datasets.push(dataset);
index += 1;
}

let x_labels = [
Expand Down
3 changes: 2 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub fn get_ips4_from_cidr(cidr: Ipv4Cidr) -> Vec<Ipv4Addr> {
ips
}

#[derive(Clone, Debug)]
pub struct MaxSizeVec<T> {
p_vec: Vec<T>,
max_len: usize,
Expand All @@ -59,7 +60,7 @@ impl<T> MaxSizeVec<T> {
self.p_vec.push(item);
}

pub fn get_vec(&mut self) -> &Vec<T> {
pub fn get_vec<'a>(&'a self) -> &'a Vec<T> {
&self.p_vec
}
}
Expand Down

0 comments on commit d65f8d5

Please sign in to comment.