Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 17780c02d5 | |||
| 141d0ee899 | |||
| 022359d3fd | |||
| 21b41f5593 | |||
| 9ff76954e0 | |||
| 8d0452d752 | |||
| 49092d9ad7 | |||
| 0261914cda | |||
| f5fa9877b3 | |||
| 7d830c4e3b | |||
| 6d55d96ca7 |
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -709,7 +709,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "pp"
|
||||
version = "0.2.0-PREVIEW"
|
||||
version = "0.2.2-PREVIEW"
|
||||
dependencies = [
|
||||
"ansi_term",
|
||||
"chrono",
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "pp"
|
||||
version = "0.2.0-PREVIEW"
|
||||
version = "0.2.2-PREVIEW"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
|
||||
@ -1,14 +1,18 @@
|
||||
address,name
|
||||
10.3.100.1,Belleville Gateway
|
||||
10.11.31.3,Belleville VPN 11-31
|
||||
10.12.32.1,Belleville VPN 12-32
|
||||
10.11.31.1,Belleville VPN 11-31 (P)
|
||||
10.11.31.3,Belleville VPN 11-31 (B)
|
||||
10.12.32.1,Belleville VPN 12-32 (P)
|
||||
10.12.32.3,Belleville VPN 12-32 (B)
|
||||
192.186.110.6,Belleville Cogeco
|
||||
129.222.197.36,Belleville Starlink
|
||||
10.2.100.1,Lindsay Gateway
|
||||
24.143.184.98,Lindsay Cogeco
|
||||
192.168.1.50,Lindsay Sign
|
||||
10.11.21.1,Lindsay VPN 11-21
|
||||
10.12.22.1,Lindsay VPN 11-22
|
||||
10.11.21.1,Lindsay VPN 11-21 (P)
|
||||
10.11.21.2,Lindsay VPN 11-21 (L)
|
||||
10.11.21.1,Lindsay VPN 11-22 (P)
|
||||
10.12.22.2,Lindsay VPN 11-22 (L)
|
||||
8.8.8.8,Google DNS
|
||||
1.1.1.1,1111 DNS
|
||||
192.168.0.1,Peterborough Gateway
|
||||
|
||||
@ -5,6 +5,6 @@ use clap::Parser;
|
||||
#[command(version, about, long_about = None)]
|
||||
pub struct AppSettings {
|
||||
/// File of list of hosts
|
||||
#[arg(short, long)]
|
||||
#[arg(short, long, default_value = None)]
|
||||
pub ping_host_file: Option<PathBuf>,
|
||||
}
|
||||
}
|
||||
|
||||
207
src/bin/pp.rs
207
src/bin/pp.rs
@ -1,201 +1,14 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::io::BufRead;
|
||||
use std::net::Ipv4Addr;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::str::FromStr;
|
||||
use std::sync::mpsc;
|
||||
use std::time::{Duration, SystemTime};
|
||||
use clap::Parser;
|
||||
use pp::ping_result::PingResult;
|
||||
use pp::ping_request::PingRequest;
|
||||
use pp::manager::Manager;
|
||||
use pp::{duration_to_string, SECONDS_BETWEEN_DISPLAY};
|
||||
use pp::target_state::TargetState;
|
||||
use std::{env, error::Error, ffi::OsString, process};
|
||||
use color_eyre::owo_colors::OwoColorize;
|
||||
use crossterm::style::Stylize;
|
||||
use log::debug;
|
||||
use ratatui::widgets::TableState;
|
||||
use pp::app_settings::AppSettings;
|
||||
use pp::tui::ratatui_app::RatatuiApp;
|
||||
fn main() -> color_eyre::Result<()> {
|
||||
// find out what file we are using to get our hosts
|
||||
let settings = AppSettings::parse();
|
||||
|
||||
struct PPState {}
|
||||
|
||||
impl PPState {
|
||||
pub fn get_default_targets() -> BTreeMap<String, TargetState> {
|
||||
let mut working = BTreeMap::new();
|
||||
working.insert("Localhost".to_string(),
|
||||
TargetState {
|
||||
name: "Localhost".to_string(),
|
||||
target: Ipv4Addr::new(127, 0, 0, 1),
|
||||
..TargetState::default()
|
||||
},
|
||||
);
|
||||
|
||||
working.insert("Home Gateway".to_string(),
|
||||
TargetState {
|
||||
name: "Home Gateway".to_string(),
|
||||
target: Ipv4Addr::new(172, 24, 0, 1),
|
||||
..TargetState::default()
|
||||
},
|
||||
);
|
||||
|
||||
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 {
|
||||
name: "Google DNS".to_string(),
|
||||
target: Ipv4Addr::new(8, 8, 8, 8),
|
||||
..TargetState::default()
|
||||
},
|
||||
);
|
||||
|
||||
working.insert("Site IP 1".to_string(),
|
||||
TargetState {
|
||||
name: "Site IP 1".to_string(),
|
||||
target: Ipv4Addr::new(216, 121, 247, 231),
|
||||
..TargetState::default()
|
||||
},
|
||||
);
|
||||
working.insert("Site IP 2".to_string(),
|
||||
TargetState {
|
||||
name: "Site IP 2".to_string(),
|
||||
target: Ipv4Addr::new(216, 234, 202, 122),
|
||||
..TargetState::default()
|
||||
},
|
||||
);
|
||||
working.insert("Site IP 3".to_string(),
|
||||
TargetState {
|
||||
name: "Site IP 3".to_string(),
|
||||
target: Ipv4Addr::new(24, 143, 184, 98),
|
||||
..TargetState::default()
|
||||
},
|
||||
);
|
||||
working
|
||||
}
|
||||
|
||||
pub fn build_targets_from_file(filename: Option<PathBuf>) -> BTreeMap<String, TargetState> {
|
||||
// PPState::get_default_targets();
|
||||
if let Some(file) = filename {
|
||||
let mut working = BTreeMap::new();
|
||||
if !&file.exists() {
|
||||
debug!("Cant load hosts from {:?}. Using default host list.", file.clone().as_os_str());
|
||||
// use
|
||||
PPState::get_default_targets()
|
||||
} else {
|
||||
debug!("LOADING HOSTS FROM {:?}", file.to_str());
|
||||
let file = File::open(file);
|
||||
let mut rdr = csv::Reader::from_reader(file.unwrap());
|
||||
for result in rdr.records() {
|
||||
let record = result.unwrap();
|
||||
working.insert(record[1].to_string(),
|
||||
TargetState {
|
||||
name: record[1].to_string(),
|
||||
target: Ipv4Addr::from_str(&record[0]).unwrap(),
|
||||
alive: false,
|
||||
last_alive_change: SystemTime::now(),
|
||||
last_rtt: 0,
|
||||
});
|
||||
}
|
||||
working
|
||||
}
|
||||
} else {
|
||||
PPState::get_default_targets()
|
||||
}
|
||||
}
|
||||
color_eyre::install()?;
|
||||
let terminal = ratatui::init();
|
||||
let result = RatatuiApp::new(settings.ping_host_file).run(terminal);
|
||||
ratatui::restore();
|
||||
result
|
||||
}
|
||||
fn ips_from_state(to_read_from: BTreeMap<String, TargetState>) -> Vec<Ipv4Addr> {
|
||||
let mut working: Vec<Ipv4Addr> = vec![];
|
||||
for current in to_read_from {
|
||||
working.push(current.1.target);
|
||||
}
|
||||
working
|
||||
}
|
||||
|
||||
/// Simple program to greet a person
|
||||
|
||||
|
||||
fn main() {
|
||||
// Get App Settings
|
||||
let settings = AppSettings::parse();
|
||||
|
||||
print!("Prep to load targets...");
|
||||
let file_to_check = match settings.ping_host_file {
|
||||
None => {
|
||||
PathBuf::from("./hosts.txt")
|
||||
}
|
||||
Some(actual) => {
|
||||
actual
|
||||
}
|
||||
};
|
||||
let mut targets = PPState::build_targets_from_file(Some(file_to_check));
|
||||
|
||||
// channel to send requests to ping
|
||||
let (ping_response_sender, ping_response_listener) = mpsc::channel::<PingResult>();
|
||||
|
||||
println!("Setting up requests for {} hosts.", targets.len());
|
||||
Manager::spawn_manager_thread(ips_from_state(targets.clone()), ping_response_sender.clone());
|
||||
|
||||
let mut display_loop_start = SystemTime::now();
|
||||
let mut duration_since_last_loop = SystemTime::now().duration_since(display_loop_start).unwrap();
|
||||
loop {
|
||||
let now = SystemTime::now();
|
||||
if let Ok(response) = ping_response_listener.recv_timeout(Duration::from_millis(100)) {
|
||||
let local_targets = targets.clone();
|
||||
for (_, (name, current_state)) in local_targets.iter().enumerate() {
|
||||
if current_state.target == response.target {
|
||||
let last_alive_change = if response.success == current_state.alive {
|
||||
current_state.last_alive_change
|
||||
} else {
|
||||
SystemTime::now()
|
||||
};
|
||||
|
||||
let new_state = TargetState {
|
||||
name: current_state.name.clone(),
|
||||
target: current_state.target,
|
||||
alive: response.success,
|
||||
last_rtt: response.rtt,
|
||||
last_alive_change,
|
||||
};
|
||||
targets.insert(name.clone(), new_state);
|
||||
}
|
||||
}
|
||||
}
|
||||
duration_since_last_loop = now
|
||||
.duration_since(display_loop_start)
|
||||
.expect("unable to figure out how long ago we displayed stuff");
|
||||
if duration_since_last_loop.as_secs() > SECONDS_BETWEEN_DISPLAY as u64 {
|
||||
println!("DISPLAY LOOP");
|
||||
println!("Host \t\t\t\t\t | Alive \t | RTT \t\t");
|
||||
for (name, current_result) in targets.clone() {
|
||||
let time_since_last_change = now
|
||||
.duration_since(current_result.last_alive_change)
|
||||
.unwrap_or(Duration::from_secs(0));
|
||||
let mut target_string = format!("{} ({})", name, current_result.target);
|
||||
while target_string.len() < 34 {
|
||||
target_string.push(' ');
|
||||
// target_string = format!("{} ", target_string);
|
||||
}
|
||||
|
||||
target_string = if current_result.alive {
|
||||
target_string.green().to_string()
|
||||
} else {
|
||||
target_string.red().to_string()
|
||||
};
|
||||
|
||||
println!("{} \t | {} \t | {}\t | Changed {} ago",
|
||||
target_string,
|
||||
current_result.alive,
|
||||
current_result.last_rtt,
|
||||
duration_to_string(time_since_last_change)
|
||||
);
|
||||
}
|
||||
display_loop_start = now;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,13 +0,0 @@
|
||||
use clap::Parser;
|
||||
use pp::app_settings::AppSettings;
|
||||
use pp::tui::ratatui_app::RatatuiApp;
|
||||
fn main() -> color_eyre::Result<()> {
|
||||
// find out what file we are using to get our hosts
|
||||
let settings = AppSettings::parse();
|
||||
|
||||
color_eyre::install()?;
|
||||
let terminal = ratatui::init();
|
||||
let result = RatatuiApp::new(settings.ping_host_file).run(terminal);
|
||||
ratatui::restore();
|
||||
result
|
||||
}
|
||||
@ -1,3 +1,7 @@
|
||||
pub mod target_state_widget;
|
||||
pub mod mode_editing;
|
||||
pub mod mode_monitoring;
|
||||
pub mod ratatui_app;
|
||||
mod ratatui_ui;
|
||||
pub mod target_state_widget;
|
||||
mod ratatui_screens;
|
||||
mod mode_deleting;
|
||||
mod mode_adding;
|
||||
|
||||
34
src/tui/mode_adding.rs
Normal file
34
src/tui/mode_adding.rs
Normal file
@ -0,0 +1,34 @@
|
||||
use std::time::Duration;
|
||||
use ratatui::Frame;
|
||||
use crate::tui::ratatui_app::RatatuiApp;
|
||||
use color_eyre::Result;
|
||||
use crossterm::event;
|
||||
use crossterm::event::{Event, KeyCode, KeyEventKind};
|
||||
use ratatui::widgets::{Block, Paragraph};
|
||||
use crate::tui::ratatui_screens::RatatuiScreens::Monitoring;
|
||||
|
||||
pub struct RatatuiAddingMode {}
|
||||
impl RatatuiAddingMode {
|
||||
pub fn render(frame: &mut Frame, state: &mut RatatuiApp) {
|
||||
frame.render_widget(
|
||||
Paragraph::new("Not Yet Written.")
|
||||
.block(Block::bordered())
|
||||
.centered(),
|
||||
frame.area()
|
||||
);
|
||||
}
|
||||
|
||||
pub fn handle_crossterm_events(app: &mut RatatuiApp) -> Result<()> {
|
||||
if event::poll(Duration::from_millis(100))? {
|
||||
match event::read()? {
|
||||
Event::Key(key) if key.kind == KeyEventKind::Press => match key.code {
|
||||
KeyCode::Esc => app.set_screen(Monitoring),
|
||||
_ => {}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
59
src/tui/mode_deleting.rs
Normal file
59
src/tui/mode_deleting.rs
Normal file
@ -0,0 +1,59 @@
|
||||
use std::time::Duration;
|
||||
use crossterm::event;
|
||||
use crossterm::event::{Event, KeyCode, KeyEventKind};
|
||||
use ratatui::Frame;
|
||||
use ratatui::style::Stylize;
|
||||
use ratatui::text::Line;
|
||||
use ratatui::widgets::{Block, Paragraph};
|
||||
use crate::tui::ratatui_app::RatatuiApp;
|
||||
use crate::tui::ratatui_screens::RatatuiScreens::Monitoring;
|
||||
use color_eyre::Result;
|
||||
|
||||
pub struct RatatuiDeletingMode {}
|
||||
|
||||
impl RatatuiDeletingMode {
|
||||
pub fn render(frame: &mut Frame, state: &mut RatatuiApp) {
|
||||
let title = Line::from("Delete Host").bold().red().centered();
|
||||
let as_list = state.state.clone();
|
||||
let mut nice_status = None;
|
||||
for (index, (label, data)) in as_list.iter().enumerate() {
|
||||
if index == state.selected_host {
|
||||
nice_status = Some(data);
|
||||
}
|
||||
}
|
||||
let mut body = format!("Do you really want to delete {} (Y/N)", nice_status.unwrap().name);
|
||||
|
||||
frame.render_widget(
|
||||
Paragraph::new(body)
|
||||
.block(Block::bordered().title(title))
|
||||
.centered(),
|
||||
frame.area()
|
||||
);
|
||||
}
|
||||
|
||||
pub fn handle_crossterm_events(app: &mut RatatuiApp) -> Result<()> {
|
||||
if event::poll(Duration::from_millis(100))? {
|
||||
match event::read()? {
|
||||
Event::Key(key) if key.kind == KeyEventKind::Press => match key.code {
|
||||
KeyCode::Enter | KeyCode::Char('y') | KeyCode::Char('Y') => {
|
||||
let as_list = &app.state.clone();
|
||||
for (index, (label, _)) in as_list.iter().enumerate() {
|
||||
if index == app.selected_host {
|
||||
app.state.remove(label).unwrap();
|
||||
app.set_screen(Monitoring)
|
||||
}
|
||||
}
|
||||
}
|
||||
KeyCode::Esc | KeyCode::Char('n') | KeyCode::Char('N') => {
|
||||
app.set_screen(Monitoring)
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
62
src/tui/mode_editing.rs
Normal file
62
src/tui/mode_editing.rs
Normal file
@ -0,0 +1,62 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::time::Duration;
|
||||
use crossterm::event;
|
||||
use crossterm::event::{Event, KeyCode, KeyEventKind};
|
||||
use ratatui::Frame;
|
||||
use ratatui::prelude::Line;
|
||||
use ratatui::style::Stylize;
|
||||
use ratatui::widgets::{Block, Paragraph};
|
||||
use crate::target_state::TargetState;
|
||||
use crate::tui::ratatui_app::RatatuiApp;
|
||||
use color_eyre::Result;
|
||||
use crate::tui::ratatui_screens::RatatuiScreens::Monitoring;
|
||||
|
||||
pub struct RatatuiEditingMode {}
|
||||
|
||||
impl RatatuiEditingMode {
|
||||
pub fn handle_crossterm_events(app: &mut RatatuiApp) -> Result<()> {
|
||||
if event::poll(Duration::from_millis(100))? {
|
||||
match event::read()? {
|
||||
Event::Key(key) if key.kind == KeyEventKind::Press => match key.code {
|
||||
KeyCode::Up => {
|
||||
println!("UP")
|
||||
}
|
||||
KeyCode::Down => {
|
||||
println!("Down")
|
||||
}
|
||||
KeyCode::Char('+') => {
|
||||
println!("ADD HOST")
|
||||
}
|
||||
KeyCode::Char('-') => {
|
||||
println!("Delete Host")
|
||||
}
|
||||
KeyCode::Char('q') => {
|
||||
app.set_running(false);
|
||||
}
|
||||
KeyCode::Esc => {
|
||||
app.set_screen(Monitoring);
|
||||
}
|
||||
KeyCode::Char('s') => {
|
||||
println!("Save to existing file.");
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn render(frame: &mut Frame, state: &mut BTreeMap<String, TargetState>) {
|
||||
let title = Line::from("Editing Hosts").bold().blue().centered();
|
||||
|
||||
let body_text = "This is the body text for editing hosts\n'q' to exit <esc> to return to monitoring";
|
||||
|
||||
frame.render_widget(
|
||||
Paragraph::new(body_text)
|
||||
.centered()
|
||||
.block(Block::new().title(title).blue()),
|
||||
frame.area(),
|
||||
);
|
||||
}
|
||||
}
|
||||
295
src/tui/mode_monitoring.rs
Normal file
295
src/tui/mode_monitoring.rs
Normal file
@ -0,0 +1,295 @@
|
||||
use crate::duration_to_string;
|
||||
use crate::target_state::TargetState;
|
||||
use crate::tui::ratatui_app::RatatuiApp;
|
||||
use color_eyre::Result;
|
||||
use crossterm::event;
|
||||
use crossterm::event::{Event, KeyCode, KeyEventKind};
|
||||
use ratatui::Frame;
|
||||
use ratatui::layout::Flex;
|
||||
use ratatui::prelude::*;
|
||||
use ratatui::widgets::{
|
||||
Block, Borders, Cell, Clear, List, ListItem, Paragraph, Row, Table, TableState,
|
||||
};
|
||||
use std::time::{Duration, SystemTime};
|
||||
|
||||
pub struct RatatuiMonitoringMode {}
|
||||
|
||||
impl RatatuiMonitoringMode {
|
||||
fn render_hosts_list(frame: &mut Frame, state: &mut RatatuiApp, area: Rect) {
|
||||
// Headers
|
||||
let headers = ["Host", "RTT", "Last Change"]
|
||||
.iter()
|
||||
.map(|h| Cell::from(*h));
|
||||
let header = Row::new(headers)
|
||||
.style(Style::default().fg(Color::Yellow).bold())
|
||||
.bottom_margin(1);
|
||||
|
||||
// Rows
|
||||
let mut rows = vec![];
|
||||
|
||||
for (_, current) in state.state.iter() {
|
||||
let name_field = format!(
|
||||
"{:<40}",
|
||||
format!("{} ({})", current.name.clone(), current.target.clone())
|
||||
);
|
||||
|
||||
let name_style = if current.alive {
|
||||
Style::default().fg(Color::Green)
|
||||
} else {
|
||||
Style::default().fg(Color::Red)
|
||||
};
|
||||
|
||||
rows.push(Row::new(vec![
|
||||
Cell::from(name_field).style(name_style),
|
||||
Cell::from(current.last_rtt.to_string()),
|
||||
Cell::from(format!(
|
||||
"{} ago.",
|
||||
duration_to_string(
|
||||
SystemTime::now()
|
||||
.duration_since(current.last_alive_change)
|
||||
.unwrap()
|
||||
)
|
||||
)),
|
||||
]));
|
||||
}
|
||||
|
||||
// Table Builder
|
||||
let table = Table::new(rows, vec![
|
||||
Constraint::Min(30),
|
||||
Constraint::Min(6),
|
||||
Constraint::Min(5),
|
||||
Constraint::Min(30),
|
||||
])
|
||||
.header(header)
|
||||
.block(
|
||||
Block::default()
|
||||
.title(Line::from(format!("PP v{}", env!("CARGO_PKG_VERSION"))))
|
||||
.borders(Borders::ALL),
|
||||
)
|
||||
.widths(&[
|
||||
Constraint::Fill(2), // Host
|
||||
Constraint::Min(4), // RTT
|
||||
Constraint::Min(30), // TIme Since
|
||||
])
|
||||
.row_highlight_style(
|
||||
Style::default()
|
||||
.bg(Color::Blue)
|
||||
.fg(Color::White)
|
||||
.add_modifier(Modifier::BOLD),
|
||||
)
|
||||
.highlight_symbol(">> ");
|
||||
|
||||
// frame.render_widget(table, layouts[0]);
|
||||
frame.render_stateful_widget(table, area, &mut make_hosts_list_state(state.selected_host));
|
||||
}
|
||||
|
||||
fn render_logs(frame: &mut Frame, state: &mut RatatuiApp, logs_layout: Rect) {
|
||||
let list_items: Vec<ListItem> = state
|
||||
.get_log_entries(10)
|
||||
.iter()
|
||||
.map(|entry| ListItem::new(entry.clone()))
|
||||
.collect();
|
||||
|
||||
// Log
|
||||
frame.render_widget(
|
||||
List::new(list_items)
|
||||
.block(Block::default().title("Logs").borders(Borders::ALL))
|
||||
.highlight_style(
|
||||
Style::default()
|
||||
.fg(Color::Yellow)
|
||||
.add_modifier(Modifier::BOLD),
|
||||
),
|
||||
logs_layout,
|
||||
);
|
||||
}
|
||||
|
||||
fn render_footer_block(frame: &mut Frame, footer_layout: Rect) {
|
||||
frame.render_widget(
|
||||
Paragraph::new(
|
||||
"Press <ESC> or q to exit | Press d to delete host | Press a to add host",
|
||||
)
|
||||
.block(Block::bordered())
|
||||
.centered(),
|
||||
footer_layout,
|
||||
);
|
||||
}
|
||||
pub fn render(frame: &mut Frame, state: &mut RatatuiApp) {
|
||||
let layouts = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Fill(1), // Top Bar
|
||||
Constraint::Length(10), // Hosts
|
||||
Constraint::Length(3), // Logs
|
||||
])
|
||||
.split(frame.area());
|
||||
|
||||
Self::render_hosts_list(frame, state, layouts[0]);
|
||||
Self::render_logs(frame, state, layouts[1]);
|
||||
Self::render_footer_block(frame, layouts[2]);
|
||||
|
||||
Self::add_popup(frame, state);
|
||||
Self::exit_popup(frame, state);
|
||||
Self::delete_popup(frame, state);
|
||||
}
|
||||
|
||||
fn add_popup(frame: &mut Frame, state: &RatatuiApp) {
|
||||
if state.showing_add_popup {
|
||||
let area = RatatuiMonitoringMode::popup_area(frame.area(), 10, 40);
|
||||
|
||||
frame.render_widget(Clear, area);
|
||||
|
||||
frame.render_widget(
|
||||
Paragraph::new(format!("Host Address : {}", state.add_host_name))
|
||||
.block(Block::bordered().title("Label")),
|
||||
area,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn exit_popup(frame: &mut Frame, state: &RatatuiApp) {
|
||||
if state.trying_to_exit {
|
||||
let area = RatatuiMonitoringMode::popup_area(frame.area(), 5, 20);
|
||||
|
||||
frame.render_widget(Clear, area);
|
||||
frame.render_widget(
|
||||
Paragraph::new("Are you sure? (Y/N)").block(Block::bordered().title("Exit")),
|
||||
area,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn delete_popup(frame: &mut Frame, state: &RatatuiApp) {
|
||||
if state.trying_to_delete {
|
||||
let as_list = state.state.clone();
|
||||
let mut host_name = String::new();
|
||||
for (index, (label, _)) in as_list.iter().enumerate() {
|
||||
if index == state.selected_host {
|
||||
host_name = label.clone();
|
||||
}
|
||||
}
|
||||
let block = Paragraph::new(format!("Delete {}", host_name))
|
||||
.centered()
|
||||
.block(Block::bordered());
|
||||
|
||||
frame.render_widget(block, frame.area());
|
||||
}
|
||||
}
|
||||
|
||||
fn popup_area(area: Rect, percent_x: u16, percent_y: u16) -> Rect {
|
||||
let vertical = Layout::vertical([Constraint::Percentage(percent_x)]).flex(Flex::Center);
|
||||
let horizontal = Layout::horizontal([Constraint::Percentage(percent_y)]).flex(Flex::Center);
|
||||
let [area] = vertical.areas(area);
|
||||
let [area] = horizontal.areas(area);
|
||||
area
|
||||
}
|
||||
|
||||
fn handle_add_popup_inputs(app: &mut RatatuiApp, key: KeyCode) {
|
||||
match key {
|
||||
KeyCode::Esc => {
|
||||
app.showing_add_popup = false;
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
app.add_host_name.remove(app.add_host_name.len() - 1);
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
app.state.insert(app.add_host_name.clone(), TargetState {
|
||||
name: app.add_host_name.clone(),
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
KeyCode::Down => {
|
||||
dbg!("Move down to next field");
|
||||
}
|
||||
KeyCode::Up => {
|
||||
dbg!("Move up to previous field");
|
||||
}
|
||||
_ => {
|
||||
app.add_host_name = format!("{}{}", app.add_host_name, key);
|
||||
//dbg!(format!("ADD_HOST_NAME: {}", app.add_host_name.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_exit_popup_inputs(app: &mut RatatuiApp, key: KeyCode) {
|
||||
match key {
|
||||
KeyCode::Char('y') | KeyCode::Char('Y') => {
|
||||
app.set_running(false);
|
||||
}
|
||||
KeyCode::Char('n') | KeyCode::Char('N') => {
|
||||
app.trying_to_exit = false;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_monitoring_screen_inputs(app: &mut RatatuiApp, key: KeyCode) {
|
||||
// Default monitoring Screen
|
||||
match key {
|
||||
KeyCode::Down => {
|
||||
if app.selected_host + 1 == app.state.len() {
|
||||
app.selected_host = 0;
|
||||
} else {
|
||||
app.selected_host += 1;
|
||||
}
|
||||
}
|
||||
KeyCode::Up => {
|
||||
if app.selected_host == 0 {
|
||||
app.selected_host = app.state.len() - 1;
|
||||
} else {
|
||||
app.selected_host -= 1;
|
||||
}
|
||||
}
|
||||
KeyCode::Esc | KeyCode::Char('q') | KeyCode::Char('Q') => {
|
||||
app.trying_to_exit = true;
|
||||
}
|
||||
KeyCode::Char('a') | KeyCode::Char('A') => {
|
||||
app.showing_add_popup = true;
|
||||
}
|
||||
KeyCode::Char('d') | KeyCode::Char('D') => {
|
||||
app.trying_to_delete = true;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_delete_popup_inputs(app: &mut RatatuiApp, key: KeyCode) {
|
||||
match key {
|
||||
KeyCode::Char('y') | KeyCode::Char('Y') => {
|
||||
app.remove_selected_host();
|
||||
app.trying_to_delete = false;
|
||||
}
|
||||
KeyCode::Char('n') | KeyCode::Char('N') => {
|
||||
dbg!("Do not delete this item.");
|
||||
app.trying_to_delete = false;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_crossterm_events(app: &mut RatatuiApp) -> Result<()> {
|
||||
if event::poll(Duration::from_millis(100))? {
|
||||
match event::read()? {
|
||||
Event::Key(key) if key.kind == KeyEventKind::Press => {
|
||||
// adding mode
|
||||
if app.showing_add_popup {
|
||||
Self::handle_add_popup_inputs(app, key.code);
|
||||
} else if app.trying_to_exit {
|
||||
Self::handle_exit_popup_inputs(app, key.code);
|
||||
} else if app.trying_to_delete {
|
||||
Self::handle_delete_popup_inputs(app, key.code);
|
||||
} else {
|
||||
Self::handle_monitoring_screen_inputs(app, key.code);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn make_hosts_list_state(selected: usize) -> TableState {
|
||||
let mut state = TableState::default();
|
||||
state.select(Some(selected));
|
||||
state
|
||||
}
|
||||
@ -1,4 +1,16 @@
|
||||
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;
|
||||
use std::fs::File;
|
||||
use std::net::Ipv4Addr;
|
||||
@ -6,36 +18,50 @@ use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
use std::sync::mpsc;
|
||||
use std::sync::mpsc::Receiver;
|
||||
use std::thread;
|
||||
use std::time::{Duration, SystemTime};
|
||||
use chrono::{DateTime, Local};
|
||||
use crossterm::event;
|
||||
use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
|
||||
use ratatui::{DefaultTerminal, Frame};
|
||||
use ratatui::prelude::*;
|
||||
use ratatui::widgets::{Block, Paragraph};
|
||||
use crate::duration_to_string;
|
||||
use crate::manager::Manager;
|
||||
use crate::ping_result::PingResult;
|
||||
use crate::target_state::TargetState;
|
||||
use crate::tui::ratatui_ui::RatatuiUi;
|
||||
|
||||
#[derive(Default)]
|
||||
pub enum RatatuiScreens {
|
||||
#[default]
|
||||
Monitoring,
|
||||
Exiting,
|
||||
Editing
|
||||
}
|
||||
const LOG_ENTRIES_DEFAULT_COUNT: u32 = 10;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct RatatuiApp {
|
||||
running: bool,
|
||||
pub(crate) state: BTreeMap<String, TargetState>,
|
||||
pub state: BTreeMap<String, TargetState>,
|
||||
current_screen: RatatuiScreens,
|
||||
pub(crate) log_entries: Vec<String>
|
||||
log_entries: Vec<String>,
|
||||
filename: Option<String>,
|
||||
pub selected_host: usize,
|
||||
pub showing_add_popup: bool,
|
||||
pub trying_to_exit: bool,
|
||||
pub trying_to_delete: bool,
|
||||
pub add_host_cursor_position: usize,
|
||||
pub add_host_name: String,
|
||||
pub num_log_entries: u32
|
||||
}
|
||||
|
||||
/// Private Methods
|
||||
impl RatatuiApp {
|
||||
pub fn remove_selected_host(self: &mut RatatuiApp) {
|
||||
for (index, (name, data)) in self.state.clone().iter().enumerate() {
|
||||
if index == self.selected_host {
|
||||
self.state.remove(name).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_filename(mut self, new_filename: String) {
|
||||
self.filename = Some(new_filename)
|
||||
}
|
||||
|
||||
pub fn get_filename(self) -> String {
|
||||
self.filename.unwrap_or(String::new())
|
||||
}
|
||||
|
||||
pub fn add_new_host(mut self, new_target: TargetState) -> Result<()> {
|
||||
self.state.insert(new_target.name.clone(), new_target);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> {
|
||||
self.running = true;
|
||||
// start the 'manager' thread that spawns its ping threads as needed
|
||||
@ -45,22 +71,8 @@ impl RatatuiApp {
|
||||
// check for any waiting ping results...
|
||||
self.consume_waiting_results(&receiver);
|
||||
|
||||
match self.current_screen {
|
||||
RatatuiScreens::Monitoring => {
|
||||
terminal.draw(|frame| RatatuiUi::monitoring_mode(frame, &mut self)).expect("Unable to draw to screen");
|
||||
self.handle_monitoring_crossterm_events();
|
||||
}
|
||||
RatatuiScreens::Exiting => {
|
||||
terminal.draw(| frame | RatatuiUi::exiting_mode(frame));
|
||||
self.handle_exiting_crossterm_events();
|
||||
}
|
||||
RatatuiScreens::Editing => {
|
||||
terminal.draw(|frame| RatatuiUi::editing_mode(frame, &mut self.state));
|
||||
self.handle_editing_crossterm_events();
|
||||
}
|
||||
}
|
||||
|
||||
// self.handle_crossterm_events()?;
|
||||
terminal.draw(|frame| RatatuiMonitoringMode::render(frame, &mut self))?;
|
||||
RatatuiMonitoringMode::handle_crossterm_events(&mut self)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -85,6 +97,7 @@ impl RatatuiApp {
|
||||
alive: new_message.success,
|
||||
last_rtt: new_message.rtt,
|
||||
last_alive_change,
|
||||
..TargetState::default()
|
||||
};
|
||||
local_state.insert(name.clone(), new_state.clone());
|
||||
let success_message = if new_state.alive {
|
||||
@ -96,12 +109,12 @@ impl RatatuiApp {
|
||||
let current_time: DateTime<Local> = SystemTime::now().into();
|
||||
|
||||
if did_change {
|
||||
self.log_entries.push(
|
||||
format!("{:?} {} for {}",
|
||||
current_time.format("%Y-%m-%d %H:%M:%S: ").to_string(),
|
||||
success_message,
|
||||
new_state.name.clone())
|
||||
);
|
||||
self.log_entries.push(format!(
|
||||
"{} {} for {}",
|
||||
current_time.format("%Y-%m-%d %H:%M:%S:").to_string(),
|
||||
success_message,
|
||||
new_state.name.clone()
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -118,139 +131,129 @@ impl RatatuiApp {
|
||||
}
|
||||
|
||||
fn render(&mut self, frame: &mut Frame) {
|
||||
match self.current_screen {
|
||||
RatatuiScreens::Monitoring => { RatatuiUi::monitoring_mode(frame, self) }
|
||||
RatatuiScreens::Exiting => { RatatuiUi::exiting_mode(frame)}
|
||||
RatatuiScreens::Editing => { RatatuiUi::editing_mode(frame, &mut self.state) }
|
||||
}
|
||||
RatatuiMonitoringMode::render(frame, self)
|
||||
}
|
||||
|
||||
fn handle_monitoring_crossterm_events(&mut self) -> Result<()> {
|
||||
if event::poll(Duration::from_millis(100))? {
|
||||
match event::read()? {
|
||||
Event::Key(key) if key.kind == KeyEventKind::Press => {
|
||||
match (key.modifiers, key.code) {
|
||||
(_, KeyCode::Esc) => {
|
||||
self.current_screen = RatatuiScreens::Exiting;
|
||||
}
|
||||
(_, KeyCode::Char('e')) | (_, KeyCode::Char('E')) => {
|
||||
self.current_screen = RatatuiScreens::Editing
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_exiting_crossterm_events(&mut self) -> Result<()> {
|
||||
if event::poll(Duration::from_millis(100))? {
|
||||
match event::read()? {
|
||||
Event::Key(key) if key.kind == KeyEventKind::Press => {
|
||||
match key.code {
|
||||
KeyCode::Enter => { self.running = false; }
|
||||
KeyCode::Char('y') | KeyCode::Char('Y') => { self.running = false;
|
||||
}
|
||||
KeyCode::Char('n') | KeyCode::Char('N') => { self.current_screen = RatatuiScreens::Monitoring }
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_editing_crossterm_events(&mut self) -> Result<()> {
|
||||
if event::poll(Duration::from_millis(100))? {
|
||||
match event::read()? {
|
||||
Event::Key(key) if key.kind == KeyEventKind::Press => {
|
||||
match key.code {
|
||||
KeyCode::Up => { println!("UP") },
|
||||
KeyCode::Down => { println!("Down") },
|
||||
KeyCode::Char('+') => { println!("ADD HOST") },
|
||||
KeyCode::Char('-') => { println!("Delete Host") }
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_crossterm_events(&mut self) -> Result<()> {
|
||||
if event::poll(Duration::from_millis(100))? {
|
||||
match event::read()? {
|
||||
// it's important to check KeyEventKind::Press to avoid handling key release events
|
||||
Event::Key(key) if key.kind == KeyEventKind::Press => self.on_key_event(key),
|
||||
Event::Mouse(_) => {}
|
||||
Event::Resize(_, _) => {}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn quit(&mut self) { self.running = false; }
|
||||
|
||||
/// Handles the key events and updates the state of [`App`].
|
||||
fn on_key_event(&mut self, key: KeyEvent) {
|
||||
match (key.modifiers, key.code) {
|
||||
(_, KeyCode::Esc | KeyCode::Char('q'))
|
||||
| (KeyModifiers::CONTROL, KeyCode::Char('c') |
|
||||
KeyCode::Char('C')) => { self.quit() }
|
||||
// Add other key handlers here.
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
pub fn new(option: Option<PathBuf>) -> Self {
|
||||
let targets = if let Some(file) = option {
|
||||
let mut working = BTreeMap::new();
|
||||
if !&file.exists() {
|
||||
RatatuiApp::get_default_targets()
|
||||
} else {
|
||||
let real_file = File::open(file);
|
||||
let mut rdr = csv::Reader::from_reader(real_file.unwrap());
|
||||
for result in rdr.records() {
|
||||
let record = result.unwrap();
|
||||
working.insert(record[1].to_string(),
|
||||
TargetState {
|
||||
name: record[1].to_string(),
|
||||
target: Ipv4Addr::from_str(&record[0]).unwrap(),
|
||||
alive: false,
|
||||
last_alive_change: SystemTime::now(),
|
||||
last_rtt: 0
|
||||
});
|
||||
}
|
||||
working
|
||||
}
|
||||
} else { RatatuiApp::get_default_targets() };
|
||||
let mut working = Self::default();
|
||||
working.state = targets;
|
||||
working
|
||||
fn quit(&mut self) {
|
||||
self.running = false;
|
||||
}
|
||||
|
||||
fn get_default_targets() -> BTreeMap<String, TargetState> {
|
||||
let mut working = BTreeMap::new();
|
||||
|
||||
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 {
|
||||
name: "Google DNS".to_string(),
|
||||
target: Ipv4Addr::new(8,8,8,8),
|
||||
..TargetState::default()
|
||||
});
|
||||
working.insert("Test Site 1".to_string(), TargetState {
|
||||
name: "Test Site 1".to_string(),
|
||||
target: Ipv4Addr::new(216,234,202,122),
|
||||
..TargetState::default()
|
||||
});
|
||||
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 {
|
||||
name: "Google DNS".to_string(),
|
||||
target: Ipv4Addr::new(8, 8, 8, 8),
|
||||
..TargetState::default()
|
||||
},
|
||||
);
|
||||
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();
|
||||
|
||||
let the_file = File::open(file_to_load_from.clone());
|
||||
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 {
|
||||
name: record[1].to_string(),
|
||||
target: Ipv4Addr::from_str(&record[0]).unwrap(),
|
||||
..TargetState::default()
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
working
|
||||
}
|
||||
|
||||
pub fn new(option: Option<PathBuf>) -> Self {
|
||||
let mut working = Self::default();
|
||||
let targets = if let Some(file) = option {
|
||||
if file.exists() {
|
||||
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("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(file.as_str()).unwrap())
|
||||
} else {
|
||||
RatatuiApp::get_default_targets()
|
||||
}
|
||||
} else {
|
||||
// none was passed for our parameter.
|
||||
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(),
|
||||
)
|
||||
} else {
|
||||
working.filename = None;
|
||||
RatatuiApp::get_default_targets()
|
||||
}
|
||||
} else {
|
||||
RatatuiApp::get_default_targets()
|
||||
}
|
||||
};
|
||||
working.state = targets.clone();
|
||||
working.num_log_entries = LOG_ENTRIES_DEFAULT_COUNT;
|
||||
working
|
||||
}
|
||||
|
||||
pub fn set_screen(&mut self, new_screen: RatatuiScreens) {
|
||||
self.current_screen = new_screen
|
||||
}
|
||||
|
||||
pub fn set_running(&mut self, new_state: bool) {
|
||||
self.running = new_state
|
||||
}
|
||||
|
||||
pub fn clear_log(&mut self) {
|
||||
self.log_entries.clear();
|
||||
}
|
||||
pub fn log_event(&mut self, to_log: String) {
|
||||
self.log_entries.push(to_log);
|
||||
}
|
||||
|
||||
pub fn get_log_entries(&self, how_many: u32) -> Vec<String> {
|
||||
self.log_entries
|
||||
.iter()
|
||||
.rev()
|
||||
.take(how_many as usize)
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
9
src/tui/ratatui_screens.rs
Normal file
9
src/tui/ratatui_screens.rs
Normal file
@ -0,0 +1,9 @@
|
||||
|
||||
#[derive(Default)]
|
||||
pub enum RatatuiScreens {
|
||||
#[default]
|
||||
Monitoring,
|
||||
Editing,
|
||||
Deleting,
|
||||
Adding
|
||||
}
|
||||
@ -1,122 +0,0 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::time::SystemTime;
|
||||
use color_eyre::owo_colors::OwoColorize;
|
||||
use ratatui::Frame;
|
||||
use ratatui::prelude::*;
|
||||
use ratatui::widgets::{Block, Borders, List, ListItem, ListState, Paragraph};
|
||||
use crate::duration_to_string;
|
||||
use crate::target_state::TargetState;
|
||||
use crate::tui::ratatui_app::RatatuiApp;
|
||||
|
||||
pub struct RatatuiUi {}
|
||||
|
||||
impl RatatuiUi {
|
||||
pub fn exiting_mode(frame: &mut Frame) {
|
||||
let title = Line::from("Exit?")
|
||||
.bold()
|
||||
.red()
|
||||
.centered();
|
||||
let mut body = "Do you want to exit? (Y/N)";
|
||||
frame.render_widget(
|
||||
Paragraph::new(body)
|
||||
.block(Block::bordered().title(title))
|
||||
.centered(),
|
||||
frame.area()
|
||||
);
|
||||
}
|
||||
|
||||
pub fn editing_mode(frame: &mut Frame, state: &mut BTreeMap<String, TargetState>) {
|
||||
let title = Line::from("Editing Hosts")
|
||||
.bold()
|
||||
.blue()
|
||||
.centered();
|
||||
|
||||
let body_text = "This is the body text for editing hosts";
|
||||
|
||||
frame.render_widget(
|
||||
Paragraph::new(body_text)
|
||||
.centered()
|
||||
.block(Block::new()
|
||||
.title(title)
|
||||
.blue()),
|
||||
frame.area()
|
||||
);
|
||||
}
|
||||
|
||||
pub fn monitoring_mode(frame: &mut Frame, state: &mut RatatuiApp) {
|
||||
let layouts = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Fill(1),
|
||||
Constraint::Length(10),
|
||||
Constraint::Length(3)
|
||||
])
|
||||
.split(frame.area());
|
||||
|
||||
let body_layout = layouts[0];
|
||||
let logs_layout = layouts[1];
|
||||
let footer_layout = layouts[2];
|
||||
|
||||
let title = Line::from(format!("PP v{}", env!("CARGO_PKG_VERSION")))
|
||||
.bold()
|
||||
.blue()
|
||||
.centered();
|
||||
let mut working = vec![]; // Line::from("Empty");
|
||||
for (title, current) in state.state.iter() {
|
||||
let mut name_field = format!("{} ({})", current.name.clone(), current.target.clone());
|
||||
|
||||
while name_field.len() < 40 {
|
||||
name_field.push(' ');
|
||||
}
|
||||
|
||||
let name_style = if current.alive {
|
||||
Style::default().fg(Color::Green)
|
||||
} else {
|
||||
Style::default().fg(Color::Red)
|
||||
};
|
||||
|
||||
working.push(Line::from(vec![
|
||||
Span::styled(name_field, name_style),
|
||||
Span::styled(current.alive.to_string(), Style::default()),
|
||||
Span::styled(current.last_rtt.to_string(), Style::default()),
|
||||
Span::styled(
|
||||
format!("{} ago.", duration_to_string(
|
||||
SystemTime::now()
|
||||
.duration_since(current.last_alive_change)
|
||||
.unwrap()
|
||||
)), Style::default())
|
||||
]));
|
||||
}
|
||||
|
||||
let footer_text = "Press <ESC> to exit | Press e to edit hosts";
|
||||
|
||||
frame.render_widget(
|
||||
Paragraph::new(working)
|
||||
.block(Block::bordered().title(title)),
|
||||
body_layout
|
||||
);
|
||||
|
||||
let mut list_items = vec![];
|
||||
for entry in state.log_entries.clone().into_iter().rev() {
|
||||
list_items.push(ListItem::new(entry));
|
||||
}
|
||||
|
||||
let list = List::new(list_items)
|
||||
.block(Block::default().title("Hosts").borders(Borders::ALL))
|
||||
.highlight_style(Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD))
|
||||
.highlight_symbol(">> ");
|
||||
|
||||
let mut list_state = ListState::default();
|
||||
list_state.select(Some(0));
|
||||
|
||||
frame.render_stateful_widget(
|
||||
list, logs_layout, &mut list_state);
|
||||
|
||||
frame.render_widget(
|
||||
Paragraph::new(footer_text)
|
||||
.block(Block::bordered())
|
||||
.centered(),
|
||||
footer_layout
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user