BUGFIX: Delete works for lists of hosts using popups
This commit is contained in:
parent
022359d3fd
commit
141d0ee899
@ -1,16 +1,13 @@
|
|||||||
use std::collections::BTreeMap;
|
use crate::{duration_to_string};
|
||||||
use crate::duration_to_string;
|
|
||||||
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::prelude::*;
|
||||||
use ratatui::widgets::{Block, Borders, Cell, Clear, List, ListItem, ListState, Paragraph, Row, Table, TableState, Wrap};
|
use ratatui::widgets::{Block, Borders, Cell, Clear, List, ListItem, Paragraph, Row, Table, TableState};
|
||||||
use ratatui::Frame;
|
use ratatui::Frame;
|
||||||
use std::time::{Duration, SystemTime};
|
use std::time::{Duration, SystemTime};
|
||||||
use ratatui::layout::Flex;
|
use ratatui::layout::Flex;
|
||||||
use crate::target_state::TargetState;
|
|
||||||
use crate::tui::ratatui_app::RatatuiApp;
|
use crate::tui::ratatui_app::RatatuiApp;
|
||||||
use crate::tui::ratatui_screens::RatatuiScreens::{Adding, Deleting, Editing};
|
|
||||||
|
|
||||||
pub struct RatatuiMonitoringMode {}
|
pub struct RatatuiMonitoringMode {}
|
||||||
|
|
||||||
@ -21,26 +18,18 @@ impl RatatuiMonitoringMode {
|
|||||||
.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
|
/// 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}", format!("{} ({})", current.name.clone(), current.target.clone()));
|
||||||
|
|
||||||
while name_field.len() < 40 {
|
let name_style = if current.alive { Style::default().fg(Color::Green) } else { Style::default().fg(Color::Red) };
|
||||||
name_field.push(' ');
|
|
||||||
}
|
|
||||||
|
|
||||||
let name_style = if current.alive {
|
rows.push(Row::new(vec![
|
||||||
Style::default().fg(Color::Green)
|
|
||||||
} else {
|
|
||||||
Style::default().fg(Color::Red)
|
|
||||||
};
|
|
||||||
|
|
||||||
let to_push = vec![
|
|
||||||
Cell::from(name_field).style(name_style),
|
Cell::from(name_field).style(name_style),
|
||||||
Cell::from(current.last_rtt.to_string()),
|
Cell::from(current.last_rtt.to_string()),
|
||||||
Cell::from(
|
Cell::from(
|
||||||
@ -52,9 +41,7 @@ impl RatatuiMonitoringMode {
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
];
|
]));
|
||||||
|
|
||||||
rows.push(Row::new(to_push));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Table Builder
|
/// Table Builder
|
||||||
@ -64,9 +51,9 @@ impl RatatuiMonitoringMode {
|
|||||||
.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(&[
|
.widths(&[
|
||||||
Constraint::Fill(3),
|
Constraint::Fill(2), // Host
|
||||||
Constraint::Min(4),
|
Constraint::Min(4), // RTT
|
||||||
Constraint::Min(30)
|
Constraint::Min(30) // TIme Since
|
||||||
])
|
])
|
||||||
.row_highlight_style(
|
.row_highlight_style(
|
||||||
Style::default()
|
Style::default()
|
||||||
@ -81,42 +68,39 @@ impl RatatuiMonitoringMode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn render_logs(frame: &mut Frame, state: &mut RatatuiApp, logs_layout: Rect) {
|
fn render_logs(frame: &mut Frame, state: &mut RatatuiApp, logs_layout: Rect) {
|
||||||
let mut list_items = vec![];
|
let list_items: Vec<ListItem> = state
|
||||||
for entry in state.get_log_entries(10) {
|
.get_log_entries(10)
|
||||||
list_items.push(ListItem::new(entry));
|
.iter()
|
||||||
}
|
.map(|entry| ListItem::new(entry.clone()))
|
||||||
|
.collect();
|
||||||
|
|
||||||
let list = List::new(list_items)
|
// Log
|
||||||
.block(Block::default().title("Logs").borders(Borders::ALL))
|
frame.render_widget(List::new(list_items)
|
||||||
|
.block(Block::default()
|
||||||
|
.title("Logs")
|
||||||
|
.borders(Borders::ALL))
|
||||||
.highlight_style(
|
.highlight_style(
|
||||||
Style::default()
|
Style::default()
|
||||||
.fg(Color::Yellow)
|
.fg(Color::Yellow)
|
||||||
.add_modifier(Modifier::BOLD),
|
.add_modifier(Modifier::BOLD),
|
||||||
);
|
), logs_layout);
|
||||||
// Log
|
|
||||||
frame.render_widget(list, logs_layout);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_footer_block(frame: &mut Frame, footer_layout: Rect) {
|
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(
|
frame.render_widget(
|
||||||
Paragraph::new(footer_text)
|
Paragraph::new("Press <ESC> or q to exit | Press d to delete host | Press a to add host")
|
||||||
.block(Block::bordered())
|
.block(Block::bordered())
|
||||||
.centered(),
|
.centered(),
|
||||||
footer_layout,
|
footer_layout,
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
pub fn render(frame: &mut Frame, state: &mut RatatuiApp) {
|
pub fn render(frame: &mut Frame, state: &mut RatatuiApp) {
|
||||||
let layouts = Layout::default()
|
let layouts = Layout::default()
|
||||||
.direction(Direction::Vertical)
|
.direction(Direction::Vertical)
|
||||||
.constraints([
|
.constraints([
|
||||||
Constraint::Fill(1),
|
Constraint::Fill(1), // Top Bar
|
||||||
Constraint::Length(10),
|
Constraint::Length(10), // Hosts
|
||||||
Constraint::Length(3),
|
Constraint::Length(3), // Logs
|
||||||
])
|
])
|
||||||
.split(frame.area());
|
.split(frame.area());
|
||||||
|
|
||||||
@ -124,33 +108,31 @@ impl RatatuiMonitoringMode {
|
|||||||
Self::render_logs(frame, state, layouts[1]);
|
Self::render_logs(frame, state, layouts[1]);
|
||||||
Self::render_footer_block(frame, layouts[2]);
|
Self::render_footer_block(frame, layouts[2]);
|
||||||
|
|
||||||
Self::add_popup(state, frame);
|
Self::add_popup(frame, state);
|
||||||
Self::exit_popup(state, frame);
|
Self::exit_popup(frame, state);
|
||||||
Self::delete_popup(state, frame);
|
Self::delete_popup(frame, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_popup(state: &RatatuiApp, frame: &mut Frame) {
|
fn add_popup(frame: &mut Frame, state: &RatatuiApp) {
|
||||||
if state.showing_add_popup {
|
if state.showing_add_popup {
|
||||||
let block = Block::bordered().title("Add Host");
|
|
||||||
let area = RatatuiMonitoringMode::popup_area(frame.area(), 60, 20);
|
let area = RatatuiMonitoringMode::popup_area(frame.area(), 60, 20);
|
||||||
|
|
||||||
frame.render_widget(Clear, area);
|
frame.render_widget(Clear, area);
|
||||||
frame.render_widget(block, area);
|
frame.render_widget(Block::bordered().title("Add Host"), area);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn exit_popup(state: &RatatuiApp, frame: &mut Frame) {
|
fn exit_popup(frame: &mut Frame, state: &RatatuiApp) {
|
||||||
if state.trying_to_exit {
|
if state.trying_to_exit {
|
||||||
let block = Paragraph::new("Are you sure? (Y/N)")
|
|
||||||
.block(Block::bordered().title("Exit"));
|
|
||||||
let area = RatatuiMonitoringMode::popup_area(frame.area(), 60, 20);
|
let area = RatatuiMonitoringMode::popup_area(frame.area(), 60, 20);
|
||||||
|
|
||||||
frame.render_widget(Clear, area);
|
frame.render_widget(Clear, area);
|
||||||
frame.render_widget(block, area);
|
frame.render_widget(Paragraph::new("Are you sure? (Y/N)")
|
||||||
|
.block(Block::bordered().title("Exit")), area);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn delete_popup(state: &RatatuiApp, frame: &mut Frame) {
|
fn delete_popup(frame: &mut Frame, state: &RatatuiApp) {
|
||||||
if state.trying_to_delete {
|
if state.trying_to_delete {
|
||||||
let as_list = state.state.clone();
|
let as_list = state.state.clone();
|
||||||
let mut host_name = String::new();
|
let mut host_name = String::new();
|
||||||
@ -179,13 +161,13 @@ impl RatatuiMonitoringMode {
|
|||||||
match key {
|
match key {
|
||||||
KeyCode::Esc => {
|
KeyCode::Esc => {
|
||||||
app.showing_add_popup = false;
|
app.showing_add_popup = false;
|
||||||
},
|
}
|
||||||
KeyCode::Backspace => {
|
KeyCode::Backspace => {
|
||||||
app.add_host_name.remove(app.add_host_name.len());
|
app.add_host_name.remove(app.add_host_name.len());
|
||||||
},
|
}
|
||||||
KeyCode::Enter => {
|
KeyCode::Enter => {
|
||||||
dbg!("SAVE THE VALUE TO A NEW TARGETSTATE");
|
dbg!("SAVE THE VALUE TO A NEW TARGETSTATE");
|
||||||
},
|
}
|
||||||
_ => {
|
_ => {
|
||||||
app.add_host_name = format!("{}{}", app.add_host_name, key);
|
app.add_host_name = format!("{}{}", app.add_host_name, key);
|
||||||
}
|
}
|
||||||
@ -237,12 +219,13 @@ impl RatatuiMonitoringMode {
|
|||||||
fn handle_delete_popup_inputs(app: &mut RatatuiApp, key: KeyCode) {
|
fn handle_delete_popup_inputs(app: &mut RatatuiApp, key: KeyCode) {
|
||||||
match key {
|
match key {
|
||||||
KeyCode::Char('y') | KeyCode::Char('Y') => {
|
KeyCode::Char('y') | KeyCode::Char('Y') => {
|
||||||
// app.remove_selected_host();
|
app.remove_selected_host();
|
||||||
dbg!("Delete this item.");
|
app.trying_to_delete = false;
|
||||||
},
|
}
|
||||||
KeyCode::Char('n') | KeyCode::Char('N') => {
|
KeyCode::Char('n') | KeyCode::Char('N') => {
|
||||||
dbg!("Do not delete this item.");
|
dbg!("Do not delete this item.");
|
||||||
},
|
app.trying_to_delete = false;
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,6 +21,8 @@ use std::sync::mpsc::Receiver;
|
|||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::{Duration, SystemTime};
|
use std::time::{Duration, SystemTime};
|
||||||
|
|
||||||
|
const LOG_ENTRIES_DEFAULT_COUNT: u32 = 10;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct RatatuiApp {
|
pub struct RatatuiApp {
|
||||||
running: bool,
|
running: bool,
|
||||||
@ -34,10 +36,19 @@ pub struct RatatuiApp {
|
|||||||
pub trying_to_delete: bool,
|
pub trying_to_delete: bool,
|
||||||
pub add_host_cursor_position: usize,
|
pub add_host_cursor_position: usize,
|
||||||
pub add_host_name: String,
|
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) {
|
pub fn set_filename(mut self, new_filename: String) {
|
||||||
self.filename = Some(new_filename)
|
self.filename = Some(new_filename)
|
||||||
}
|
}
|
||||||
@ -218,6 +229,7 @@ impl RatatuiApp {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
working.state = targets.clone();
|
working.state = targets.clone();
|
||||||
|
working.num_log_entries = LOG_ENTRIES_DEFAULT_COUNT;
|
||||||
working
|
working
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user