Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f5fa9877b3 | |||
| 7d830c4e3b | |||
| 6d55d96ca7 |
Generated
+1
-1
@@ -709,7 +709,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "pp"
|
||||
version = "0.2.0-PREVIEW"
|
||||
version = "0.2.0"
|
||||
dependencies = [
|
||||
"ansi_term",
|
||||
"chrono",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "pp"
|
||||
version = "0.2.0-PREVIEW"
|
||||
version = "0.2.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
|
||||
+40
-1
@@ -11,7 +11,7 @@ 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::SECONDS_BETWEEN_DISPLAY;
|
||||
use pp::target_state::TargetState;
|
||||
use std::{env, error::Error, ffi::OsString, process};
|
||||
use color_eyre::owo_colors::OwoColorize;
|
||||
@@ -19,6 +19,45 @@ use crossterm::style::Stylize;
|
||||
use log::debug;
|
||||
use pp::app_settings::AppSettings;
|
||||
|
||||
const SECONDS_IN_MINUTE: u32 = 60;
|
||||
const SECONDS_IN_HOUR: u32 = SECONDS_IN_MINUTE * 60;
|
||||
const SECONDS_IN_DAY: u32 = SECONDS_IN_HOUR * 24;
|
||||
|
||||
pub fn duration_to_string(to_convert: Duration) -> String {
|
||||
let mut total_seconds = to_convert.as_secs() as u32;
|
||||
let mut working_string = String::new();
|
||||
|
||||
if total_seconds > 86400 {
|
||||
// days
|
||||
let num_days = (total_seconds / SECONDS_IN_DAY) as u32;
|
||||
working_string = format!("{} days", num_days);
|
||||
total_seconds = total_seconds - (num_days * SECONDS_IN_DAY);
|
||||
}
|
||||
|
||||
if total_seconds > 3600 {
|
||||
// hours
|
||||
let num_hours = (total_seconds / SECONDS_IN_HOUR) as u32;
|
||||
if num_hours > 0 {
|
||||
working_string = format!("{} {} hours", working_string, num_hours);
|
||||
total_seconds = total_seconds - (num_hours * SECONDS_IN_HOUR);
|
||||
}
|
||||
}
|
||||
if total_seconds > 60 {
|
||||
let num_minutes = (total_seconds / SECONDS_IN_MINUTE) as u32;
|
||||
if num_minutes > 0 {
|
||||
working_string = format!("{} {} minutes", working_string, num_minutes);
|
||||
total_seconds = total_seconds - (num_minutes * SECONDS_IN_MINUTE);
|
||||
}
|
||||
// minutes
|
||||
}
|
||||
|
||||
|
||||
working_string = format!("{} {} seconds", working_string, total_seconds);
|
||||
|
||||
working_string
|
||||
}
|
||||
|
||||
|
||||
struct PPState {}
|
||||
|
||||
impl PPState {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use clap::Parser;
|
||||
use ratatui::widgets::TableState;
|
||||
use pp::app_settings::AppSettings;
|
||||
use pp::tui::ratatui_app::RatatuiApp;
|
||||
fn main() -> color_eyre::Result<()> {
|
||||
@@ -11,3 +12,4 @@ fn main() -> color_eyre::Result<()> {
|
||||
ratatui::restore();
|
||||
result
|
||||
}
|
||||
|
||||
|
||||
+7
-2
@@ -1,3 +1,8 @@
|
||||
pub mod target_state_widget;
|
||||
pub mod mode_editing;
|
||||
pub mod mode_exiting;
|
||||
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;
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
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(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
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 mut body = format!("Do you really want to delete {} (Y/N)", state.selected_host);
|
||||
|
||||
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') => {
|
||||
println!("TiME TO DELETE SELECTED");
|
||||
}
|
||||
KeyCode::Esc | KeyCode::Char('n') | KeyCode::Char('N') => {
|
||||
app.set_screen(Monitoring)
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
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 color_eyre::Result;
|
||||
use crate::tui::ratatui_screens::RatatuiScreens::Monitoring;
|
||||
|
||||
pub struct RatatuiExitingMode {}
|
||||
|
||||
impl RatatuiExitingMode {
|
||||
pub fn render(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 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') => {
|
||||
app.set_running(false);
|
||||
}
|
||||
KeyCode::Char('n') | KeyCode::Char('N') => {
|
||||
app.set_screen(Monitoring);
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
use crate::duration_to_string;
|
||||
use color_eyre::Result;
|
||||
use crossterm::event;
|
||||
use crossterm::event::{Event, KeyCode, KeyEventKind};
|
||||
use ratatui::prelude::*;
|
||||
use ratatui::widgets::{Block, Borders, Cell, List, ListItem, ListState, Paragraph, Row, Table, TableState};
|
||||
use ratatui::Frame;
|
||||
use std::time::{Duration, SystemTime};
|
||||
use ratatui::layout::Direction::Vertical;
|
||||
use crate::tui::ratatui_app::RatatuiApp;
|
||||
use crate::tui::ratatui_screens::RatatuiScreens::{Deleting, Editing, Exiting};
|
||||
|
||||
pub struct RatatuiMonitoringMode {}
|
||||
|
||||
impl RatatuiMonitoringMode {
|
||||
pub fn render(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 table_size = layouts[0].area();
|
||||
// let columns = Layout::default()
|
||||
// .direction(Vertical)
|
||||
// .constraints([Constraint::Min(30),
|
||||
// Constraint::Min(6),
|
||||
// Constraint::Min(4),
|
||||
// Constraint::Min(30)]);
|
||||
|
||||
let headers = ["Host", "Alive", "RTT", "Last Change"]
|
||||
.iter()
|
||||
.map(|h| Cell::from(*h));
|
||||
let header = Row::new(headers)
|
||||
.style(Style::default().fg(Color::Yellow))
|
||||
.bottom_margin(1);
|
||||
let mut rows = vec![];
|
||||
|
||||
for (_, 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)
|
||||
};
|
||||
|
||||
let to_push = vec![
|
||||
Cell::from(name_field).style(name_style),
|
||||
Cell::from(current.alive.to_string()),
|
||||
Cell::from(current.last_rtt.to_string()),
|
||||
Cell::from(
|
||||
format!("{} ago.",
|
||||
duration_to_string(
|
||||
SystemTime::now()
|
||||
.duration_since(current.last_alive_change)
|
||||
.unwrap()
|
||||
)
|
||||
)
|
||||
)
|
||||
];
|
||||
|
||||
rows.push(Row::new(to_push));
|
||||
}
|
||||
|
||||
let table = Table::new(rows, vec![Constraint::Min(30), Constraint::Min(6), Constraint::Min(5), Constraint::Min(30)])
|
||||
.header(header)
|
||||
.block(Block::default()
|
||||
.title("Hosts")
|
||||
.borders(Borders::ALL))
|
||||
.widths(&[
|
||||
Constraint::Min(30),
|
||||
Constraint::Min(6),
|
||||
Constraint::Min(4),
|
||||
Constraint::Min(30)
|
||||
])
|
||||
.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, layouts[0], &mut make_state(state.selected_host));
|
||||
|
||||
let footer_text = "Press <ESC> or q to exit";
|
||||
// let footer_text = "Press <ESC> or q to exit | Press d to delete host | Press a to add host";
|
||||
|
||||
let mut list_items = vec![];
|
||||
for entry in state.get_log_entries(10) {
|
||||
list_items.push(ListItem::new(entry));
|
||||
}
|
||||
|
||||
let list = List::new(list_items)
|
||||
.block(Block::default().title("Logs").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,
|
||||
);
|
||||
}
|
||||
|
||||
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.modifiers, key.code) {
|
||||
(_, 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.set_screen(Exiting);
|
||||
}
|
||||
// (_, KeyCode::Char('e')) | (_, KeyCode::Char('E')) => {
|
||||
// app.set_screen(Editing);
|
||||
// }
|
||||
// (_, KeyCode::Char('d')) | (_, KeyCode::Char('D')) => {
|
||||
// app.set_screen(Deleting);
|
||||
// }
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn make_state(selected: usize) -> TableState {
|
||||
let mut state = TableState::default();
|
||||
state.select(Some(selected));
|
||||
state
|
||||
}
|
||||
+108
-144
@@ -1,4 +1,11 @@
|
||||
use crate::tui::ratatui_screens::RatatuiScreens;
|
||||
use crate::manager::Manager;
|
||||
use crate::ping_result::PingResult;
|
||||
use crate::target_state::TargetState;
|
||||
use chrono::{DateTime, Local};
|
||||
use color_eyre::Result;
|
||||
use ratatui::prelude::*;
|
||||
use ratatui::{DefaultTerminal, Frame};
|
||||
use std::collections::BTreeMap;
|
||||
use std::fs::File;
|
||||
use std::net::Ipv4Addr;
|
||||
@@ -7,34 +14,25 @@ use std::str::FromStr;
|
||||
use std::sync::mpsc;
|
||||
use std::sync::mpsc::Receiver;
|
||||
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;
|
||||
use crate::tui::mode_adding::RatatuiAddingMode;
|
||||
use crate::tui::mode_deleting::RatatuiDeletingMode;
|
||||
use crate::tui::mode_editing::RatatuiEditingMode;
|
||||
use crate::tui::mode_exiting::RatatuiExitingMode;
|
||||
use crate::tui::mode_monitoring::RatatuiMonitoringMode;
|
||||
|
||||
|
||||
#[derive(Default)]
|
||||
pub enum RatatuiScreens {
|
||||
#[default]
|
||||
Monitoring,
|
||||
Exiting,
|
||||
Editing
|
||||
}
|
||||
|
||||
#[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
|
||||
}
|
||||
|
||||
/// Private Methods
|
||||
impl RatatuiApp {
|
||||
pub fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> {
|
||||
self.running = true;
|
||||
@@ -46,21 +44,30 @@ impl RatatuiApp {
|
||||
self.consume_waiting_results(&receiver);
|
||||
|
||||
match self.current_screen {
|
||||
RatatuiScreens::Deleting => {
|
||||
terminal
|
||||
.draw(|frame| RatatuiDeletingMode::render(frame, &mut self))?;
|
||||
RatatuiDeletingMode::handle_crossterm_events(&mut self)?
|
||||
|
||||
}
|
||||
RatatuiScreens::Monitoring => {
|
||||
terminal.draw(|frame| RatatuiUi::monitoring_mode(frame, &mut self)).expect("Unable to draw to screen");
|
||||
self.handle_monitoring_crossterm_events();
|
||||
terminal
|
||||
.draw(|frame| RatatuiMonitoringMode::render(frame, &mut self))?;
|
||||
RatatuiMonitoringMode::handle_crossterm_events(&mut self)?;
|
||||
}
|
||||
RatatuiScreens::Exiting => {
|
||||
terminal.draw(| frame | RatatuiUi::exiting_mode(frame));
|
||||
self.handle_exiting_crossterm_events();
|
||||
terminal.draw(|frame| RatatuiExitingMode::render(frame))?;
|
||||
RatatuiExitingMode::handle_crossterm_events(&mut self)?;
|
||||
}
|
||||
RatatuiScreens::Editing => {
|
||||
terminal.draw(|frame| RatatuiUi::editing_mode(frame, &mut self.state));
|
||||
self.handle_editing_crossterm_events();
|
||||
terminal.draw(|frame| RatatuiEditingMode::render(frame, &mut self.state))?;
|
||||
RatatuiEditingMode::handle_crossterm_events(&mut self)?;
|
||||
}
|
||||
RatatuiScreens::Adding => {
|
||||
terminal.draw(|frame| RatatuiAddingMode::render(frame, &mut self))?;
|
||||
RatatuiAddingMode::handle_crossterm_events(&mut self)?
|
||||
}
|
||||
}
|
||||
|
||||
// self.handle_crossterm_events()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -96,12 +103,12 @@ impl RatatuiApp {
|
||||
let current_time: DateTime<Local> = SystemTime::now().into();
|
||||
|
||||
if did_change {
|
||||
self.log_entries.push(
|
||||
format!("{:?} {} for {}",
|
||||
self.log_entries.push(format!(
|
||||
"{:?} {} for {}",
|
||||
current_time.format("%Y-%m-%d %H:%M:%S: ").to_string(),
|
||||
success_message,
|
||||
new_state.name.clone())
|
||||
);
|
||||
new_state.name.clone()
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -119,118 +126,16 @@ 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) }
|
||||
RatatuiScreens::Monitoring => RatatuiMonitoringMode::render(frame, self),
|
||||
RatatuiScreens::Exiting => RatatuiExitingMode::render(frame),
|
||||
RatatuiScreens::Editing => RatatuiEditingMode::render(frame, &mut self.state),
|
||||
RatatuiScreens::Deleting => RatatuiDeletingMode::render(frame,self),
|
||||
RatatuiScreens::Adding => RatatuiAddingMode::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> {
|
||||
@@ -238,19 +143,78 @@ impl RatatuiApp {
|
||||
|
||||
working.insert("1111 DNS".to_string(), TargetState {
|
||||
name: "1111 DNS".to_string(),
|
||||
target: Ipv4Addr::new(1,1,1,1),
|
||||
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),
|
||||
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),
|
||||
target: Ipv4Addr::new(216, 234, 202, 122),
|
||||
..TargetState::default()
|
||||
});
|
||||
working
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Public Methods
|
||||
impl RatatuiApp {
|
||||
pub fn new(option: Option<PathBuf>) -> Self {
|
||||
|
||||
let mut working = Self::default();
|
||||
let targets = if let Some(file) = option {
|
||||
let mut working_btree = BTreeMap::new();
|
||||
if !&file.exists() {
|
||||
RatatuiApp::get_default_targets();
|
||||
working.filename = Some("hosts.txt".to_string());
|
||||
} else {
|
||||
let real_file = File::open(file.clone());
|
||||
working.filename = Some(file.as_os_str().to_string_lossy().parse().unwrap());
|
||||
let mut rdr = csv::Reader::from_reader(real_file.unwrap());
|
||||
for result in rdr.records() {
|
||||
let record = result.unwrap();
|
||||
working_btree.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_btree
|
||||
} else {
|
||||
RatatuiApp::get_default_targets()
|
||||
};
|
||||
working.state = targets;
|
||||
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> {
|
||||
let mut return_value = vec![];
|
||||
for current in self.log_entries.clone().into_iter().rev() {
|
||||
return_value.push(current);
|
||||
}
|
||||
return_value
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
|
||||
#[derive(Default)]
|
||||
pub enum RatatuiScreens {
|
||||
#[default]
|
||||
Monitoring,
|
||||
Exiting,
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user