minor improvements for better 'rustifying' of it
This commit is contained in:
parent
21b41f5593
commit
022359d3fd
@ -1,9 +1,14 @@
|
||||
use crate::tui::ratatui_screens::RatatuiScreens;
|
||||
use crate::manager::Manager;
|
||||
use crate::ping_result::PingResult;
|
||||
use crate::target_state::TargetState;
|
||||
use crate::tui::mode_adding::RatatuiAddingMode;
|
||||
use crate::tui::mode_deleting::RatatuiDeletingMode;
|
||||
use crate::tui::mode_editing::RatatuiEditingMode;
|
||||
use crate::tui::mode_monitoring::RatatuiMonitoringMode;
|
||||
use crate::tui::ratatui_screens::RatatuiScreens;
|
||||
use chrono::{DateTime, Local};
|
||||
use color_eyre::Result;
|
||||
use log::debug;
|
||||
use ratatui::prelude::*;
|
||||
use ratatui::{DefaultTerminal, Frame};
|
||||
use std::collections::BTreeMap;
|
||||
@ -15,11 +20,6 @@ use std::sync::mpsc;
|
||||
use std::sync::mpsc::Receiver;
|
||||
use std::thread;
|
||||
use std::time::{Duration, SystemTime};
|
||||
use log::debug;
|
||||
use crate::tui::mode_adding::RatatuiAddingMode;
|
||||
use crate::tui::mode_deleting::RatatuiDeletingMode;
|
||||
use crate::tui::mode_editing::RatatuiEditingMode;
|
||||
use crate::tui::mode_monitoring::RatatuiMonitoringMode;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct RatatuiApp {
|
||||
@ -33,12 +33,11 @@ pub struct RatatuiApp {
|
||||
pub trying_to_exit: bool,
|
||||
pub trying_to_delete: bool,
|
||||
pub add_host_cursor_position: usize,
|
||||
pub add_host_name: String
|
||||
pub add_host_name: String,
|
||||
}
|
||||
|
||||
/// Private Methods
|
||||
impl RatatuiApp {
|
||||
|
||||
pub fn set_filename(mut self, new_filename: String) {
|
||||
self.filename = Some(new_filename)
|
||||
}
|
||||
@ -61,10 +60,8 @@ impl RatatuiApp {
|
||||
// check for any waiting ping results...
|
||||
self.consume_waiting_results(&receiver);
|
||||
|
||||
terminal
|
||||
.draw(|frame| RatatuiMonitoringMode::render(frame, &mut self))?;
|
||||
terminal.draw(|frame| RatatuiMonitoringMode::render(frame, &mut self))?;
|
||||
RatatuiMonitoringMode::handle_crossterm_events(&mut self)?;
|
||||
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -133,29 +130,36 @@ impl RatatuiApp {
|
||||
fn get_default_targets() -> BTreeMap<String, TargetState> {
|
||||
let mut working = BTreeMap::new();
|
||||
|
||||
working.insert("1111 DNS".to_string(), TargetState {
|
||||
working.insert(
|
||||
"1111 DNS".to_string(),
|
||||
TargetState {
|
||||
name: "1111 DNS".to_string(),
|
||||
target: Ipv4Addr::new(1, 1, 1, 1),
|
||||
..TargetState::default()
|
||||
});
|
||||
working.insert("Google DNS".to_string(), TargetState {
|
||||
},
|
||||
);
|
||||
working.insert(
|
||||
"Google DNS".to_string(),
|
||||
TargetState {
|
||||
name: "Google DNS".to_string(),
|
||||
target: Ipv4Addr::new(8, 8, 8, 8),
|
||||
..TargetState::default()
|
||||
});
|
||||
working.insert("Test Site 1".to_string(), TargetState {
|
||||
},
|
||||
);
|
||||
working.insert(
|
||||
"Test Site 1".to_string(),
|
||||
TargetState {
|
||||
name: "Test Site 1".to_string(),
|
||||
target: Ipv4Addr::new(216, 234, 202, 122),
|
||||
..TargetState::default()
|
||||
});
|
||||
},
|
||||
);
|
||||
working
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Public Methods
|
||||
impl RatatuiApp {
|
||||
|
||||
fn load_hosts_from_file(file_to_load_from: PathBuf) -> BTreeMap<String, TargetState> {
|
||||
let mut working = BTreeMap::new();
|
||||
|
||||
@ -163,11 +167,14 @@ impl RatatuiApp {
|
||||
let mut rdr = csv::Reader::from_reader(the_file.unwrap());
|
||||
for result in rdr.records() {
|
||||
let record = result.unwrap();
|
||||
working.insert(record[1].to_string(), TargetState {
|
||||
working.insert(
|
||||
record[1].to_string(),
|
||||
TargetState {
|
||||
name: record[1].to_string(),
|
||||
target: Ipv4Addr::from_str(&record[0]).unwrap(),
|
||||
..TargetState::default()
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
working
|
||||
@ -180,17 +187,17 @@ impl RatatuiApp {
|
||||
working.filename = Some(file.as_os_str().to_string_lossy().parse().unwrap());
|
||||
} else {
|
||||
// working.log_event("Passed file doesnt exist looking for hosts.txt".to_string());
|
||||
if !&PathBuf::from_str("hosts.txt").unwrap().exists() {
|
||||
// working.log_event("Didnt find hosts.txt".to_string());
|
||||
working.filename = None;
|
||||
} else {
|
||||
if PathBuf::from_str("hosts.txt").unwrap().exists() {
|
||||
// working.log_event("Found hosts.txt. using it as the default".to_string());
|
||||
working.filename = Some("hosts.txt".to_string());
|
||||
} else {
|
||||
// working.log_event("Didnt find hosts.txt".to_string());
|
||||
working.filename = None;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(file) = working.filename.clone() {
|
||||
RatatuiApp::load_hosts_from_file(PathBuf::from_str(&*working.filename.clone().unwrap()).unwrap())
|
||||
RatatuiApp::load_hosts_from_file(PathBuf::from_str(file.as_str()).unwrap())
|
||||
} else {
|
||||
RatatuiApp::get_default_targets()
|
||||
}
|
||||
@ -199,7 +206,9 @@ impl RatatuiApp {
|
||||
working.filename = Some("hosts.txt".to_string());
|
||||
if let Some(ref hosts_file) = working.filename {
|
||||
if PathBuf::from_str(hosts_file).unwrap().exists() {
|
||||
RatatuiApp::load_hosts_from_file(PathBuf::from_str(&*hosts_file.clone()).unwrap())
|
||||
RatatuiApp::load_hosts_from_file(
|
||||
PathBuf::from_str(&*hosts_file.clone()).unwrap(),
|
||||
)
|
||||
} else {
|
||||
working.filename = None;
|
||||
RatatuiApp::get_default_targets()
|
||||
@ -228,10 +237,11 @@ impl RatatuiApp {
|
||||
}
|
||||
|
||||
pub fn get_log_entries(&self, how_many: u32) -> Vec<String> {
|
||||
let mut return_value = vec![];
|
||||
for current in self.log_entries.clone().into_iter().rev() {
|
||||
return_value.push(current);
|
||||
}
|
||||
return_value
|
||||
self.log_entries
|
||||
.iter()
|
||||
.rev()
|
||||
.take(how_many as usize)
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user