Compare commits

..

No commits in common. "17780c02d5338ca368c7a35bc3467ccd0f58e3de" and "022359d3fdacc83c1c99b7b1aa3ad56702a9607c" have entirely different histories.

2 changed files with 97 additions and 128 deletions

View File

@ -1,37 +1,38 @@
use std::collections::BTreeMap;
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 ratatui::widgets::{Block, Borders, Cell, Clear, List, ListItem, ListState, Paragraph, Row, Table, TableState, Wrap};
use ratatui::Frame;
use std::time::{Duration, SystemTime};
use ratatui::layout::Flex;
use crate::target_state::TargetState;
use crate::tui::ratatui_app::RatatuiApp;
use crate::tui::ratatui_screens::RatatuiScreens::{Adding, Deleting, Editing};
pub struct RatatuiMonitoringMode {}
impl RatatuiMonitoringMode {
fn render_hosts_list(frame: &mut Frame, state: &mut RatatuiApp, area: Rect) {
// Headers
/// 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())
.style(Style::default().fg(Color::Yellow))
.bottom_margin(1);
// Rows
/// Rows
let mut rows = vec![];
for (_, current) in state.state.iter() {
let name_field = format!(
"{:<40}",
format!("{} ({})", current.name.clone(), current.target.clone())
);
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)
@ -39,127 +40,118 @@ impl RatatuiMonitoringMode {
Style::default().fg(Color::Red)
};
rows.push(Row::new(vec![
let to_push = 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()
Cell::from(
format!("{} ago.",
duration_to_string(
SystemTime::now()
.duration_since(current.last_alive_change)
.unwrap()
)
)
)),
]));
)
];
rows.push(Row::new(to_push));
}
// 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()
/// 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(">> ");
.borders(Borders::ALL))
.widths(&[
Constraint::Fill(3),
Constraint::Min(4),
Constraint::Min(30)
])
.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));
frame.render_stateful_widget(table, area, &mut make_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();
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),
);
// 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,
);
frame.render_widget(list, logs_layout);
}
fn render_footer_block(frame: &mut Frame, footer_layout: Rect) {
// 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";
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(),
Paragraph::new(footer_text)
.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
Constraint::Fill(1),
Constraint::Length(10),
Constraint::Length(3),
])
.split(frame.area());
Self::render_hosts_list(frame, state, layouts[0]);
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);
Self::add_popup(state, frame);
Self::exit_popup(state, frame);
Self::delete_popup(state, frame);
}
fn add_popup(frame: &mut Frame, state: &RatatuiApp) {
fn add_popup(state: &RatatuiApp, frame: &mut Frame) {
if state.showing_add_popup {
let area = RatatuiMonitoringMode::popup_area(frame.area(), 10, 40);
let block = Block::bordered().title("Add Host");
let area = RatatuiMonitoringMode::popup_area(frame.area(), 60, 20);
frame.render_widget(Clear, area);
frame.render_widget(
Paragraph::new(format!("Host Address : {}", state.add_host_name))
.block(Block::bordered().title("Label")),
area,
);
frame.render_widget(block, area);
}
}
fn exit_popup(frame: &mut Frame, state: &RatatuiApp) {
fn exit_popup(state: &RatatuiApp, frame: &mut Frame) {
if state.trying_to_exit {
let area = RatatuiMonitoringMode::popup_area(frame.area(), 5, 20);
let block = Paragraph::new("Are you sure? (Y/N)")
.block(Block::bordered().title("Exit"));
let area = RatatuiMonitoringMode::popup_area(frame.area(), 60, 20);
frame.render_widget(Clear, area);
frame.render_widget(
Paragraph::new("Are you sure? (Y/N)").block(Block::bordered().title("Exit")),
area,
);
frame.render_widget(block, area);
}
}
fn delete_popup(frame: &mut Frame, state: &RatatuiApp) {
if state.trying_to_delete {
fn delete_popup(state: &RatatuiApp, frame: &mut Frame) {
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() {
@ -187,25 +179,15 @@ impl RatatuiMonitoringMode {
match key {
KeyCode::Esc => {
app.showing_add_popup = false;
}
},
KeyCode::Backspace => {
app.add_host_name.remove(app.add_host_name.len() - 1);
}
app.add_host_name.remove(app.add_host_name.len());
},
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");
}
dbg!("SAVE THE VALUE TO A NEW TARGETSTATE");
},
_ => {
app.add_host_name = format!("{}{}", app.add_host_name, key);
//dbg!(format!("ADD_HOST_NAME: {}", app.add_host_name.clone()));
}
}
}
@ -242,11 +224,11 @@ impl RatatuiMonitoringMode {
KeyCode::Esc | KeyCode::Char('q') | KeyCode::Char('Q') => {
app.trying_to_exit = true;
}
KeyCode::Char('a') | KeyCode::Char('A') => {
KeyCode::Char('a') | KeyCode::Char('A') => {
app.showing_add_popup = true;
}
KeyCode::Char('d') | KeyCode::Char('D') => {
app.trying_to_delete = true;
KeyCode::Char('d') | KeyCode::Char('D') => {
app.trying_to_delete = true;
}
_ => {}
}
@ -255,13 +237,12 @@ impl RatatuiMonitoringMode {
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;
}
// app.remove_selected_host();
dbg!("Delete this item.");
},
KeyCode::Char('n') | KeyCode::Char('N') => {
dbg!("Do not delete this item.");
app.trying_to_delete = false;
}
},
_ => {}
}
}
@ -288,8 +269,8 @@ impl RatatuiMonitoringMode {
}
}
pub fn make_hosts_list_state(selected: usize) -> TableState {
pub fn make_state(selected: usize) -> TableState {
let mut state = TableState::default();
state.select(Some(selected));
state
}
}

View File

@ -21,8 +21,6 @@ use std::sync::mpsc::Receiver;
use std::thread;
use std::time::{Duration, SystemTime};
const LOG_ENTRIES_DEFAULT_COUNT: u32 = 10;
#[derive(Default)]
pub struct RatatuiApp {
running: bool,
@ -36,19 +34,10 @@ pub struct RatatuiApp {
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)
}
@ -229,7 +218,6 @@ impl RatatuiApp {
}
};
working.state = targets.clone();
working.num_log_entries = LOG_ENTRIES_DEFAULT_COUNT;
working
}