Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 17780c02d5 | |||
| 141d0ee899 | |||
| 022359d3fd | |||
| 21b41f5593 | |||
| 9ff76954e0 |
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -709,7 +709,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pp"
|
name = "pp"
|
||||||
version = "0.2.1"
|
version = "0.2.2-PREVIEW"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ansi_term",
|
"ansi_term",
|
||||||
"chrono",
|
"chrono",
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "pp"
|
name = "pp"
|
||||||
version = "0.2.1"
|
version = "0.2.2-PREVIEW"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
pub mod mode_editing;
|
pub mod mode_editing;
|
||||||
pub mod mode_exiting;
|
|
||||||
pub mod mode_monitoring;
|
pub mod mode_monitoring;
|
||||||
pub mod ratatui_app;
|
pub mod ratatui_app;
|
||||||
pub mod target_state_widget;
|
pub mod target_state_widget;
|
||||||
|
|||||||
@ -8,7 +8,6 @@ use ratatui::widgets::{Block, Paragraph};
|
|||||||
use crate::tui::ratatui_screens::RatatuiScreens::Monitoring;
|
use crate::tui::ratatui_screens::RatatuiScreens::Monitoring;
|
||||||
|
|
||||||
pub struct RatatuiAddingMode {}
|
pub struct RatatuiAddingMode {}
|
||||||
|
|
||||||
impl RatatuiAddingMode {
|
impl RatatuiAddingMode {
|
||||||
pub fn render(frame: &mut Frame, state: &mut RatatuiApp) {
|
pub fn render(frame: &mut Frame, state: &mut RatatuiApp) {
|
||||||
frame.render_widget(
|
frame.render_widget(
|
||||||
@ -17,7 +16,6 @@ impl RatatuiAddingMode {
|
|||||||
.centered(),
|
.centered(),
|
||||||
frame.area()
|
frame.area()
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_crossterm_events(app: &mut RatatuiApp) -> Result<()> {
|
pub fn handle_crossterm_events(app: &mut RatatuiApp) -> Result<()> {
|
||||||
|
|||||||
@ -14,7 +14,14 @@ pub struct RatatuiDeletingMode {}
|
|||||||
impl RatatuiDeletingMode {
|
impl RatatuiDeletingMode {
|
||||||
pub fn render(frame: &mut Frame, state: &mut RatatuiApp) {
|
pub fn render(frame: &mut Frame, state: &mut RatatuiApp) {
|
||||||
let title = Line::from("Delete Host").bold().red().centered();
|
let title = Line::from("Delete Host").bold().red().centered();
|
||||||
let mut body = format!("Do you really want to delete {} (Y/N)", state.selected_host);
|
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(
|
frame.render_widget(
|
||||||
Paragraph::new(body)
|
Paragraph::new(body)
|
||||||
@ -29,7 +36,13 @@ impl RatatuiDeletingMode {
|
|||||||
match event::read()? {
|
match event::read()? {
|
||||||
Event::Key(key) if key.kind == KeyEventKind::Press => match key.code {
|
Event::Key(key) if key.kind == KeyEventKind::Press => match key.code {
|
||||||
KeyCode::Enter | KeyCode::Char('y') | KeyCode::Char('Y') => {
|
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') => {
|
KeyCode::Esc | KeyCode::Char('n') | KeyCode::Char('N') => {
|
||||||
app.set_screen(Monitoring)
|
app.set_screen(Monitoring)
|
||||||
|
|||||||
@ -1,43 +0,0 @@
|
|||||||
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(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,57 +1,37 @@
|
|||||||
use crate::duration_to_string;
|
use crate::duration_to_string;
|
||||||
|
use crate::target_state::TargetState;
|
||||||
|
use crate::tui::ratatui_app::RatatuiApp;
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
use crossterm::event;
|
use crossterm::event;
|
||||||
use crossterm::event::{Event, KeyCode, KeyEventKind};
|
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 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};
|
use std::time::{Duration, SystemTime};
|
||||||
use crate::tui::ratatui_app::RatatuiApp;
|
|
||||||
use crate::tui::ratatui_screens::RatatuiScreens::{Deleting, Editing, Exiting};
|
|
||||||
|
|
||||||
pub struct RatatuiMonitoringMode {}
|
pub struct RatatuiMonitoringMode {}
|
||||||
|
|
||||||
impl RatatuiMonitoringMode {
|
impl RatatuiMonitoringMode {
|
||||||
pub fn render(frame: &mut Frame, state: &mut RatatuiApp) {
|
fn render_hosts_list(frame: &mut Frame, state: &mut RatatuiApp, area: Rect) {
|
||||||
let layouts = Layout::default()
|
// Headers
|
||||||
.direction(Direction::Vertical)
|
let headers = ["Host", "RTT", "Last Change"]
|
||||||
.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()
|
.iter()
|
||||||
.map(|h| Cell::from(*h));
|
.map(|h| Cell::from(*h));
|
||||||
let header = Row::new(headers)
|
let header = Row::new(headers)
|
||||||
.style(Style::default().fg(Color::Yellow))
|
.style(Style::default().fg(Color::Yellow).bold())
|
||||||
.bottom_margin(1);
|
.bottom_margin(1);
|
||||||
|
|
||||||
|
// Rows
|
||||||
let mut rows = vec![];
|
let mut rows = vec![];
|
||||||
|
|
||||||
for (_, current) in state.state.iter() {
|
for (_, current) in state.state.iter() {
|
||||||
let mut name_field = format!("{} ({})", current.name.clone(), current.target.clone());
|
let name_field = format!(
|
||||||
|
"{:<40}",
|
||||||
while name_field.len() < 40 {
|
format!("{} ({})", current.name.clone(), current.target.clone())
|
||||||
name_field.push(' ');
|
);
|
||||||
}
|
|
||||||
|
|
||||||
let name_style = if current.alive {
|
let name_style = if current.alive {
|
||||||
Style::default().fg(Color::Green)
|
Style::default().fg(Color::Green)
|
||||||
@ -59,105 +39,246 @@ impl RatatuiMonitoringMode {
|
|||||||
Style::default().fg(Color::Red)
|
Style::default().fg(Color::Red)
|
||||||
};
|
};
|
||||||
|
|
||||||
let to_push = vec![
|
rows.push(Row::new(vec![
|
||||||
Cell::from(name_field).style(name_style),
|
Cell::from(name_field).style(name_style),
|
||||||
Cell::from(current.alive.to_string()),
|
|
||||||
Cell::from(current.last_rtt.to_string()),
|
Cell::from(current.last_rtt.to_string()),
|
||||||
Cell::from(
|
Cell::from(format!(
|
||||||
format!("{} ago.",
|
"{} ago.",
|
||||||
duration_to_string(
|
duration_to_string(
|
||||||
SystemTime::now()
|
SystemTime::now()
|
||||||
.duration_since(current.last_alive_change)
|
.duration_since(current.last_alive_change)
|
||||||
.unwrap()
|
.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)])
|
// Table Builder
|
||||||
.header(header)
|
let table = Table::new(rows, vec![
|
||||||
.block(Block::default()
|
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"))))
|
.title(Line::from(format!("PP v{}", env!("CARGO_PKG_VERSION"))))
|
||||||
.borders(Borders::ALL))
|
.borders(Borders::ALL),
|
||||||
.widths(&[
|
)
|
||||||
Constraint::Min(30),
|
.widths(&[
|
||||||
Constraint::Min(6),
|
Constraint::Fill(2), // Host
|
||||||
Constraint::Min(4),
|
Constraint::Min(4), // RTT
|
||||||
Constraint::Min(30)
|
Constraint::Min(30), // TIme Since
|
||||||
])
|
])
|
||||||
.highlight_style(
|
.row_highlight_style(
|
||||||
Style::default()
|
Style::default()
|
||||||
.bg(Color::Blue)
|
.bg(Color::Blue)
|
||||||
.fg(Color::White)
|
.fg(Color::White)
|
||||||
.add_modifier(Modifier::BOLD),
|
.add_modifier(Modifier::BOLD),
|
||||||
)
|
)
|
||||||
.highlight_symbol(">> ");
|
.highlight_symbol(">> ");
|
||||||
|
|
||||||
// frame.render_widget(table, layouts[0]);
|
// frame.render_widget(table, layouts[0]);
|
||||||
frame.render_stateful_widget(table, layouts[0], &mut make_state(state.selected_host));
|
frame.render_stateful_widget(table, area, &mut make_hosts_list_state(state.selected_host));
|
||||||
|
}
|
||||||
|
|
||||||
// let footer_text = "Press <ESC> or q to exit";
|
fn render_logs(frame: &mut Frame, state: &mut RatatuiApp, logs_layout: Rect) {
|
||||||
let footer_text = format!("Press <ESC> or q to exit | Press d to delete host | Press a to add host - ({:?})", state.filename);
|
let list_items: Vec<ListItem> = state
|
||||||
|
.get_log_entries(10)
|
||||||
let mut list_items = vec![];
|
.iter()
|
||||||
for entry in state.get_log_entries(10) {
|
.map(|entry| ListItem::new(entry.clone()))
|
||||||
list_items.push(ListItem::new(entry));
|
.collect();
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
|
// Log
|
||||||
frame.render_widget(
|
frame.render_widget(
|
||||||
Paragraph::new(footer_text)
|
List::new(list_items)
|
||||||
.block(Block::bordered())
|
.block(Block::default().title("Logs").borders(Borders::ALL))
|
||||||
.centered(),
|
.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,
|
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<()> {
|
pub fn handle_crossterm_events(app: &mut RatatuiApp) -> Result<()> {
|
||||||
if event::poll(Duration::from_millis(100))? {
|
if event::poll(Duration::from_millis(100))? {
|
||||||
match event::read()? {
|
match event::read()? {
|
||||||
Event::Key(key) if key.kind == KeyEventKind::Press => {
|
Event::Key(key) if key.kind == KeyEventKind::Press => {
|
||||||
match (key.modifiers, key.code) {
|
// adding mode
|
||||||
(_, KeyCode::Down) => {
|
if app.showing_add_popup {
|
||||||
if app.selected_host + 1 == app.state.len() {
|
Self::handle_add_popup_inputs(app, key.code);
|
||||||
app.selected_host = 0;
|
} else if app.trying_to_exit {
|
||||||
} else {
|
Self::handle_exit_popup_inputs(app, key.code);
|
||||||
app.selected_host += 1;
|
} else if app.trying_to_delete {
|
||||||
}
|
Self::handle_delete_popup_inputs(app, key.code);
|
||||||
}
|
} else {
|
||||||
(_, KeyCode::Up) => {
|
Self::handle_monitoring_screen_inputs(app, key.code);
|
||||||
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);
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
@ -167,8 +288,8 @@ impl RatatuiMonitoringMode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn make_state(selected: usize) -> TableState {
|
pub fn make_hosts_list_state(selected: usize) -> TableState {
|
||||||
let mut state = TableState::default();
|
let mut state = TableState::default();
|
||||||
state.select(Some(selected));
|
state.select(Some(selected));
|
||||||
state
|
state
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,14 @@
|
|||||||
use crate::tui::ratatui_screens::RatatuiScreens;
|
|
||||||
use crate::manager::Manager;
|
use crate::manager::Manager;
|
||||||
use crate::ping_result::PingResult;
|
use crate::ping_result::PingResult;
|
||||||
use crate::target_state::TargetState;
|
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 chrono::{DateTime, Local};
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
|
use log::debug;
|
||||||
use ratatui::prelude::*;
|
use ratatui::prelude::*;
|
||||||
use ratatui::{DefaultTerminal, Frame};
|
use ratatui::{DefaultTerminal, Frame};
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
@ -15,14 +20,8 @@ use std::sync::mpsc;
|
|||||||
use std::sync::mpsc::Receiver;
|
use std::sync::mpsc::Receiver;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::{Duration, SystemTime};
|
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_exiting::RatatuiExitingMode;
|
|
||||||
use crate::tui::mode_monitoring::RatatuiMonitoringMode;
|
|
||||||
|
|
||||||
|
|
||||||
|
const LOG_ENTRIES_DEFAULT_COUNT: u32 = 10;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct RatatuiApp {
|
pub struct RatatuiApp {
|
||||||
@ -30,12 +29,39 @@ pub struct RatatuiApp {
|
|||||||
pub state: BTreeMap<String, TargetState>,
|
pub state: BTreeMap<String, TargetState>,
|
||||||
current_screen: RatatuiScreens,
|
current_screen: RatatuiScreens,
|
||||||
log_entries: Vec<String>,
|
log_entries: Vec<String>,
|
||||||
pub(crate) filename: Option<String>,
|
filename: Option<String>,
|
||||||
pub selected_host: usize
|
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
|
/// Private Methods
|
||||||
impl RatatuiApp {
|
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<()> {
|
pub fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> {
|
||||||
self.running = true;
|
self.running = true;
|
||||||
// start the 'manager' thread that spawns its ping threads as needed
|
// start the 'manager' thread that spawns its ping threads as needed
|
||||||
@ -45,31 +71,8 @@ impl RatatuiApp {
|
|||||||
// check for any waiting ping results...
|
// check for any waiting ping results...
|
||||||
self.consume_waiting_results(&receiver);
|
self.consume_waiting_results(&receiver);
|
||||||
|
|
||||||
match self.current_screen {
|
terminal.draw(|frame| RatatuiMonitoringMode::render(frame, &mut self))?;
|
||||||
RatatuiScreens::Deleting => {
|
RatatuiMonitoringMode::handle_crossterm_events(&mut self)?;
|
||||||
terminal
|
|
||||||
.draw(|frame| RatatuiDeletingMode::render(frame, &mut self))?;
|
|
||||||
RatatuiDeletingMode::handle_crossterm_events(&mut self)?
|
|
||||||
|
|
||||||
}
|
|
||||||
RatatuiScreens::Monitoring => {
|
|
||||||
terminal
|
|
||||||
.draw(|frame| RatatuiMonitoringMode::render(frame, &mut self))?;
|
|
||||||
RatatuiMonitoringMode::handle_crossterm_events(&mut self)?;
|
|
||||||
}
|
|
||||||
RatatuiScreens::Exiting => {
|
|
||||||
terminal.draw(|frame| RatatuiExitingMode::render(frame))?;
|
|
||||||
RatatuiExitingMode::handle_crossterm_events(&mut self)?;
|
|
||||||
}
|
|
||||||
RatatuiScreens::Editing => {
|
|
||||||
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)?
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -94,6 +97,7 @@ impl RatatuiApp {
|
|||||||
alive: new_message.success,
|
alive: new_message.success,
|
||||||
last_rtt: new_message.rtt,
|
last_rtt: new_message.rtt,
|
||||||
last_alive_change,
|
last_alive_change,
|
||||||
|
..TargetState::default()
|
||||||
};
|
};
|
||||||
local_state.insert(name.clone(), new_state.clone());
|
local_state.insert(name.clone(), new_state.clone());
|
||||||
let success_message = if new_state.alive {
|
let success_message = if new_state.alive {
|
||||||
@ -106,8 +110,8 @@ impl RatatuiApp {
|
|||||||
|
|
||||||
if did_change {
|
if did_change {
|
||||||
self.log_entries.push(format!(
|
self.log_entries.push(format!(
|
||||||
"{:?} {} for {}",
|
"{} {} for {}",
|
||||||
current_time.format("%Y-%m-%d %H:%M:%S: ").to_string(),
|
current_time.format("%Y-%m-%d %H:%M:%S:").to_string(),
|
||||||
success_message,
|
success_message,
|
||||||
new_state.name.clone()
|
new_state.name.clone()
|
||||||
));
|
));
|
||||||
@ -127,13 +131,7 @@ impl RatatuiApp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn render(&mut self, frame: &mut Frame) {
|
fn render(&mut self, frame: &mut Frame) {
|
||||||
match self.current_screen {
|
RatatuiMonitoringMode::render(frame, self)
|
||||||
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 quit(&mut self) {
|
fn quit(&mut self) {
|
||||||
@ -143,29 +141,36 @@ impl RatatuiApp {
|
|||||||
fn get_default_targets() -> BTreeMap<String, TargetState> {
|
fn get_default_targets() -> BTreeMap<String, TargetState> {
|
||||||
let mut working = BTreeMap::new();
|
let mut working = BTreeMap::new();
|
||||||
|
|
||||||
working.insert("1111 DNS".to_string(), TargetState {
|
working.insert(
|
||||||
name: "1111 DNS".to_string(),
|
"1111 DNS".to_string(),
|
||||||
target: Ipv4Addr::new(1, 1, 1, 1),
|
TargetState {
|
||||||
..TargetState::default()
|
name: "1111 DNS".to_string(),
|
||||||
});
|
target: Ipv4Addr::new(1, 1, 1, 1),
|
||||||
working.insert("Google DNS".to_string(), TargetState {
|
..TargetState::default()
|
||||||
name: "Google DNS".to_string(),
|
},
|
||||||
target: Ipv4Addr::new(8, 8, 8, 8),
|
);
|
||||||
..TargetState::default()
|
working.insert(
|
||||||
});
|
"Google DNS".to_string(),
|
||||||
working.insert("Test Site 1".to_string(), TargetState {
|
TargetState {
|
||||||
name: "Test Site 1".to_string(),
|
name: "Google DNS".to_string(),
|
||||||
target: Ipv4Addr::new(216, 234, 202, 122),
|
target: Ipv4Addr::new(8, 8, 8, 8),
|
||||||
..TargetState::default()
|
..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
|
working
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Public Methods
|
/// Public Methods
|
||||||
impl RatatuiApp {
|
impl RatatuiApp {
|
||||||
|
|
||||||
fn load_hosts_from_file(file_to_load_from: PathBuf) -> BTreeMap<String, TargetState> {
|
fn load_hosts_from_file(file_to_load_from: PathBuf) -> BTreeMap<String, TargetState> {
|
||||||
let mut working = BTreeMap::new();
|
let mut working = BTreeMap::new();
|
||||||
|
|
||||||
@ -173,13 +178,14 @@ impl RatatuiApp {
|
|||||||
let mut rdr = csv::Reader::from_reader(the_file.unwrap());
|
let mut rdr = csv::Reader::from_reader(the_file.unwrap());
|
||||||
for result in rdr.records() {
|
for result in rdr.records() {
|
||||||
let record = result.unwrap();
|
let record = result.unwrap();
|
||||||
working.insert(record[1].to_string(), TargetState {
|
working.insert(
|
||||||
name: record[1].to_string(),
|
record[1].to_string(),
|
||||||
target: Ipv4Addr::from_str(&record[0]).unwrap(),
|
TargetState {
|
||||||
alive: false,
|
name: record[1].to_string(),
|
||||||
last_alive_change: SystemTime::now(),
|
target: Ipv4Addr::from_str(&record[0]).unwrap(),
|
||||||
last_rtt: 0
|
..TargetState::default()
|
||||||
});
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
working
|
working
|
||||||
@ -192,17 +198,17 @@ impl RatatuiApp {
|
|||||||
working.filename = Some(file.as_os_str().to_string_lossy().parse().unwrap());
|
working.filename = Some(file.as_os_str().to_string_lossy().parse().unwrap());
|
||||||
} else {
|
} else {
|
||||||
// working.log_event("Passed file doesnt exist looking for hosts.txt".to_string());
|
// working.log_event("Passed file doesnt exist looking for hosts.txt".to_string());
|
||||||
if !&PathBuf::from_str("hosts.txt").unwrap().exists() {
|
if PathBuf::from_str("hosts.txt").unwrap().exists() {
|
||||||
// working.log_event("Didnt find hosts.txt".to_string());
|
|
||||||
working.filename = None;
|
|
||||||
} else {
|
|
||||||
// working.log_event("Found hosts.txt. using it as the default".to_string());
|
// working.log_event("Found hosts.txt. using it as the default".to_string());
|
||||||
working.filename = Some("hosts.txt".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() {
|
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 {
|
} else {
|
||||||
RatatuiApp::get_default_targets()
|
RatatuiApp::get_default_targets()
|
||||||
}
|
}
|
||||||
@ -211,7 +217,9 @@ impl RatatuiApp {
|
|||||||
working.filename = Some("hosts.txt".to_string());
|
working.filename = Some("hosts.txt".to_string());
|
||||||
if let Some(ref hosts_file) = working.filename {
|
if let Some(ref hosts_file) = working.filename {
|
||||||
if PathBuf::from_str(hosts_file).unwrap().exists() {
|
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 {
|
} else {
|
||||||
working.filename = None;
|
working.filename = None;
|
||||||
RatatuiApp::get_default_targets()
|
RatatuiApp::get_default_targets()
|
||||||
@ -221,6 +229,7 @@ impl RatatuiApp {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
working.state = targets.clone();
|
working.state = targets.clone();
|
||||||
|
working.num_log_entries = LOG_ENTRIES_DEFAULT_COUNT;
|
||||||
working
|
working
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -240,11 +249,11 @@ impl RatatuiApp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_log_entries(&self, how_many: u32) -> Vec<String> {
|
pub fn get_log_entries(&self, how_many: u32) -> Vec<String> {
|
||||||
let mut return_value = vec![];
|
self.log_entries
|
||||||
for current in self.log_entries.clone().into_iter().rev() {
|
.iter()
|
||||||
return_value.push(current);
|
.rev()
|
||||||
}
|
.take(how_many as usize)
|
||||||
return_value
|
.cloned()
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,7 +3,6 @@
|
|||||||
pub enum RatatuiScreens {
|
pub enum RatatuiScreens {
|
||||||
#[default]
|
#[default]
|
||||||
Monitoring,
|
Monitoring,
|
||||||
Exiting,
|
|
||||||
Editing,
|
Editing,
|
||||||
Deleting,
|
Deleting,
|
||||||
Adding
|
Adding
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user