feat: show id column

This commit is contained in:
Jack Wills
2022-04-29 01:03:06 +00:00
parent 1017850a6c
commit b10f927481
5 changed files with 87 additions and 42 deletions
+9 -7
View File
@@ -398,12 +398,13 @@ impl ContainerItem {
/// Container information panel headings + widths, for nice pretty formatting /// Container information panel headings + widths, for nice pretty formatting
#[derive(Debug)] #[derive(Debug)]
pub struct Columns { pub struct Columns {
pub cpu: (String, usize),
pub image: (String, usize),
pub name: (String, usize),
pub state: (String, usize), pub state: (String, usize),
pub status: (String, usize), pub status: (String, usize),
pub cpu: (String, usize),
pub mem: (String, usize), pub mem: (String, usize),
pub id: (String, usize),
pub name: (String, usize),
pub image: (String, usize),
pub net_rx: (String, usize), pub net_rx: (String, usize),
pub net_tx: (String, usize), pub net_tx: (String, usize),
} }
@@ -411,13 +412,14 @@ pub struct Columns {
impl Columns { impl Columns {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
// 7 to allow for 100.00%
cpu: (String::from("cpu"), 7),
image: (String::from("image"), 5),
name: (String::from("name"), 4),
state: (String::from("state"), 11), state: (String::from("state"), 11),
status: (String::from("status"), 16), status: (String::from("status"), 16),
// 7 to allow for "100.00%"
cpu: (String::from("cpu"), 7),
mem: (String::from("memory/limit"), 12), mem: (String::from("memory/limit"), 12),
id: (String::from("id"), 8),
name: (String::from("name"), 4),
image: (String::from("image"), 5),
net_rx: (String::from("↓ rx"), 5), net_rx: (String::from("↓ rx"), 5),
net_tx: (String::from("↑ tx"), 5), net_tx: (String::from("↑ tx"), 5),
} }
+16 -5
View File
@@ -11,7 +11,7 @@ use crossterm::{
execute, execute,
}; };
use parking_lot::Mutex; use parking_lot::Mutex;
use tokio::sync::broadcast::Receiver; use tokio::{sync::broadcast::Receiver, task::JoinHandle};
use tui::layout::Rect; use tui::layout::Rect;
mod message; mod message;
@@ -31,6 +31,7 @@ pub struct InputHandler {
is_running: Arc<AtomicBool>, is_running: Arc<AtomicBool>,
rec: Receiver<InputMessages>, rec: Receiver<InputMessages>,
mouse_capture: bool, mouse_capture: bool,
info_sleep: Option<JoinHandle<()>>,
} }
impl InputHandler { impl InputHandler {
@@ -49,6 +50,7 @@ impl InputHandler {
is_running, is_running,
rec, rec,
mouse_capture: true, mouse_capture: true,
info_sleep: None,
}; };
inner.start().await; inner.start().await;
} }
@@ -112,7 +114,7 @@ impl InputHandler {
Ok(_) => self Ok(_) => self
.gui_state .gui_state
.lock() .lock()
.set_info_box("Mouse capture disabled".to_owned()), .set_info_box("✖ mouse capture disabled".to_owned()),
Err(_) => self Err(_) => self
.app_data .app_data
.lock() .lock()
@@ -123,12 +125,21 @@ impl InputHandler {
Ok(_) => self Ok(_) => self
.gui_state .gui_state
.lock() .lock()
.set_info_box("Mouse capture enabled".to_owned()), .set_info_box("✓ mouse capture enabled".to_owned()),
Err(_) => self.app_data.lock().set_error(AppError::MouseCapture(true)), Err(_) => self.app_data.lock().set_error(AppError::MouseCapture(true)),
} }
todo!("tokio spawn for x seconds and then reset, probably need to take in an arc clone for self.gui_state")
// execute!(stdout, EnableMouseCapture).unwrap();
}; };
let gui_state = Arc::clone(&self.gui_state);
if self.info_sleep.is_some() {
self.info_sleep.as_ref().unwrap().abort()
}
self.info_sleep = Some(tokio::spawn(async move {
tokio::time::sleep(std::time::Duration::from_millis(4000)).await;
gui_state.lock().reset_info_box()
}));
self.mouse_capture = !self.mouse_capture; self.mouse_capture = !self.mouse_capture;
} }
KeyCode::Tab => self.gui_state.lock().next_panel(), KeyCode::Tab => self.gui_state.lock().next_panel(),
+49 -22
View File
@@ -45,7 +45,7 @@ fn generate_block<'a>(
app_data: &Arc<Mutex<AppData>>, app_data: &Arc<Mutex<AppData>>,
selected_panel: &SelectablePanel, selected_panel: &SelectablePanel,
) -> Block<'a> { ) -> Block<'a> {
let mut block = Block::default().borders(Borders::ALL); let mut block = Block::default().borders(Borders::ALL).border_type(BorderType::Rounded);
if let Some(panel) = selectable_panel { if let Some(panel) = selectable_panel {
let title = match panel { let title = match panel {
@@ -63,11 +63,12 @@ fn generate_block<'a>(
}; };
block = block.title(title); block = block.title(title);
if selected_panel == &panel { if selected_panel == &panel {
let selected_style = Style::default().fg(Color::LightCyan); // let selected_style = Style::default().fg(Color::LightCyan);
let selected_border = BorderType::Plain; // let selected_border = BorderType::Plain;
// let selected_border = BorderType::Rounded;
block = block block = block
.border_style(selected_style) .border_style(Style::default().fg(Color::LightCyan));
.border_type(selected_border); // .border_type(BorderType::Rounded);
} }
} }
block block
@@ -170,6 +171,10 @@ pub fn draw_containers<B: Backend>(
Span::styled( Span::styled(
format!("{}{:>width$}", MARGIN, mems, width = widths.mem.1), format!("{}{:>width$}", MARGIN, mems, width = widths.mem.1),
state_style, state_style,
),
Span::styled(
format!("{}{:>width$}", MARGIN, i.id.chars().take(8).collect::<String>(), width = widths.id.1),
blue,
), ),
Span::styled( Span::styled(
format!("{}{:>width$}", MARGIN, i.name, width = widths.name.1), format!("{}{:>width$}", MARGIN, i.name, width = widths.name.1),
@@ -331,7 +336,8 @@ fn make_chart<T: Stats + Display>(
.add_modifier(Modifier::BOLD), .add_modifier(Modifier::BOLD),
)) ))
.borders(Borders::ALL) .borders(Borders::ALL)
.border_type(BorderType::Plain), // .border_type(BorderType::Plain),
.border_type(BorderType::Rounded),
) )
.x_axis( .x_axis(
Axis::default() Axis::default()
@@ -355,7 +361,7 @@ fn make_chart<T: Stats + Display>(
} }
/// Show error popup over whole screen /// Show error popup over whole screen
pub fn draw_info_bar<B: Backend>( pub fn draw_heading_bar<B: Backend>(
area: Rect, area: Rect,
columns: &Columns, columns: &Columns,
f: &mut Frame<'_, B>, f: &mut Frame<'_, B>,
@@ -380,6 +386,15 @@ pub fn draw_info_bar<B: Backend>(
.push_str(format!("{}{:>width$}", MARGIN, columns.cpu.0, width = columns.cpu.1).as_str()); .push_str(format!("{}{:>width$}", MARGIN, columns.cpu.0, width = columns.cpu.1).as_str());
column_headings column_headings
.push_str(format!("{}{:>width$}", MARGIN, columns.mem.0, width = columns.mem.1).as_str()); .push_str(format!("{}{:>width$}", MARGIN, columns.mem.0, width = columns.mem.1).as_str());
column_headings.push_str(
format!(
"{}{:>width$}",
MARGIN,
columns.id.0,
width = columns.id.1
)
.as_str(),
);
column_headings.push_str( column_headings.push_str(
format!( format!(
"{}{:>width$}", "{}{:>width$}",
@@ -498,7 +513,12 @@ pub fn draw_help_box<B: Backend>(f: &mut Frame<'_, B>) {
.border_type(BorderType::Rounded) .border_type(BorderType::Rounded)
.border_style(Style::default().fg(Color::Black)); .border_style(Style::default().fg(Color::Black));
let area = draw_popup(lines as u16, max_line_width as u16, f.size(), BoxLocation::MiddleCentre); let area = draw_popup(
lines as u16,
max_line_width as u16,
f.size(),
BoxLocation::MiddleCentre,
);
let split_popup = Layout::default() let split_popup = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
@@ -561,7 +581,12 @@ pub fn draw_error<B: Backend>(f: &mut Frame<'_, B>, error: AppError, seconds: Op
.block(block) .block(block)
.alignment(Alignment::Center); .alignment(Alignment::Center);
let area = draw_popup(lines as u16, max_line_width as u16, f.size(), BoxLocation::MiddleCentre); let area = draw_popup(
lines as u16,
max_line_width as u16,
f.size(),
BoxLocation::MiddleCentre,
);
f.render_widget(Clear, area); f.render_widget(Clear, area);
f.render_widget(paragraph, area); f.render_widget(paragraph, area);
} }
@@ -573,7 +598,7 @@ pub fn draw_info<B: Backend>(f: &mut Frame<'_, B>, text: String) {
.title_alignment(Alignment::Center) .title_alignment(Alignment::Center)
.borders(Borders::NONE); .borders(Borders::NONE);
// Add a blank line, so that the text is verticall centered // Add a blank line, so that the text is verticall centered
let text = format!("\n{}", text); let text = format!("\n{}", text);
let mut max_line_width = 0; let mut max_line_width = 0;
@@ -595,33 +620,35 @@ pub fn draw_info<B: Backend>(f: &mut Frame<'_, B>, text: String) {
.block(block) .block(block)
.alignment(Alignment::Center); .alignment(Alignment::Center);
let area = draw_popup(lines as u16, max_line_width as u16, f.size(), BoxLocation::BottomRight); let area = draw_popup(
lines as u16,
max_line_width as u16,
f.size(),
BoxLocation::BottomRight,
);
f.render_widget(Clear, area); f.render_widget(Clear, area);
f.render_widget(paragraph, area); f.render_widget(paragraph, area);
} }
/// draw a box in the center of the screen, based on max line width + number of lines /// draw a box in the center of the screen, based on max line width + number of lines
fn draw_popup(text_lines: u16, text_width: u16, r: Rect, box_location:BoxLocation) -> Rect { fn draw_popup(text_lines: u16, text_width: u16, r: Rect, box_location: BoxLocation) -> Rect {
// This can panic if number_lines or max_line_width is larger than r.height or r.width // This can panic if number_lines or max_line_width is larger than r.height or r.width
let blank_vertical = (r.height - text_lines) / 2; let blank_vertical = (r.height - text_lines) / 2;
let blank_horizontal = (r.width - text_width) / 2; let blank_horizontal = (r.width - text_width) / 2;
let vertical_constraints = box_location.get_vertical_constraints(blank_vertical, text_lines); let vertical_constraints = box_location.get_vertical_constraints(blank_vertical, text_lines);
let horizontal_constraints = box_location.get_horizontal_constraints(blank_horizontal, text_width); let horizontal_constraints =
box_location.get_horizontal_constraints(blank_horizontal, text_width);
let indexes = box_location.get_indexes(); let indexes = box_location.get_indexes();
let popup_layout = Layout::default() let popup_layout = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.constraints( .constraints(vertical_constraints)
vertical_constraints
)
.split(r); .split(r);
Layout::default() Layout::default()
.direction(Direction::Horizontal) .direction(Direction::Horizontal)
.constraints( .constraints(horizontal_constraints)
horizontal_constraints
)
.split(popup_layout[indexes.0])[indexes.1] .split(popup_layout[indexes.0])[indexes.1]
} }
+11 -4
View File
@@ -36,8 +36,12 @@ impl BoxLocation {
} }
} }
// Should combine and just return a tupple? // Should combine and just return a tupple?
pub fn get_horizontal_constraints(&self, blank_vertical: u16, text_width: u16) -> [Constraint; 3] { pub fn get_horizontal_constraints(
&self,
blank_vertical: u16,
text_width: u16,
) -> [Constraint; 3] {
match self { match self {
Self::TopLeft | Self::MiddleLeft | Self::BottomLeft => [ Self::TopLeft | Self::MiddleLeft | Self::BottomLeft => [
Constraint::Max(text_width), Constraint::Max(text_width),
@@ -56,7 +60,11 @@ impl BoxLocation {
], ],
} }
} }
pub fn get_vertical_constraints(&self, blank_vertical: u16, number_lines: u16) -> [Constraint; 3] { pub fn get_vertical_constraints(
&self,
blank_vertical: u16,
number_lines: u16,
) -> [Constraint; 3] {
match self { match self {
Self::TopLeft | Self::TopCentre | Self::TopRight => [ Self::TopLeft | Self::TopCentre | Self::TopRight => [
Constraint::Max(number_lines), Constraint::Max(number_lines),
@@ -75,7 +83,6 @@ impl BoxLocation {
], ],
} }
} }
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
+2 -4
View File
@@ -192,7 +192,7 @@ fn ui<B: Backend>(
&selected_panel, &selected_panel,
); );
draw_info_bar( draw_heading_bar(
whole_layout[0], whole_layout[0],
&column_widths, &column_widths,
f, f,
@@ -200,14 +200,12 @@ fn ui<B: Backend>(
show_help, show_help,
); );
// only draw charts if there are containers // only draw charts if there are containers
if has_containers { if has_containers {
draw_chart(f, lower_main[1], app_data, log_index); draw_chart(f, lower_main[1], app_data, log_index);
} }
if let Some(info) = info_text { if let Some(info) = info_text {
draw_info(f, info); draw_info(f, info);
} }