From 757103c54fe05afcd6e3cfeba09da2a8806e8086 Mon Sep 17 00:00:00 2001 From: Jack Wills <32690432+mrjackwills@users.noreply.github.com> Date: Fri, 29 Apr 2022 01:39:21 +0000 Subject: [PATCH] refactor: help box description from TOML --- src/app_data/container_state.rs | 4 +- src/input_handler/mod.rs | 64 +++++++++++++-------------- src/ui/draw_blocks.rs | 77 +++++++++++++++------------------ 3 files changed, 70 insertions(+), 75 deletions(-) diff --git a/src/app_data/container_state.rs b/src/app_data/container_state.rs index 3852e4c..aca3744 100644 --- a/src/app_data/container_state.rs +++ b/src/app_data/container_state.rs @@ -402,7 +402,7 @@ pub struct Columns { pub status: (String, usize), pub cpu: (String, usize), pub mem: (String, usize), - pub id: (String, usize), + pub id: (String, usize), pub name: (String, usize), pub image: (String, usize), pub net_rx: (String, usize), @@ -417,7 +417,7 @@ impl Columns { // 7 to allow for "100.00%" cpu: (String::from("cpu"), 7), mem: (String::from("memory/limit"), 12), - id: (String::from("id"), 8), + id: (String::from("id"), 8), name: (String::from("name"), 4), image: (String::from("image"), 5), net_rx: (String::from("↓ rx"), 5), diff --git a/src/input_handler/mod.rs b/src/input_handler/mod.rs index af2f63c..9baadc7 100644 --- a/src/input_handler/mod.rs +++ b/src/input_handler/mod.rs @@ -74,40 +74,40 @@ impl InputHandler { } } - fn m_button(&mut self) { - if self.mouse_capture { - match execute!(std::io::stdout(), DisableMouseCapture) { - Ok(_) => self - .gui_state - .lock() - .set_info_box("✖ mouse capture disabled".to_owned()), - Err(_) => self - .app_data - .lock() - .set_error(AppError::MouseCapture(false)), - } - } else { - match execute!(std::io::stdout(), EnableMouseCapture) { - Ok(_) => self - .gui_state - .lock() - .set_info_box("✓ mouse capture enabled".to_owned()), - Err(_) => self.app_data.lock().set_error(AppError::MouseCapture(true)), - } - }; + fn m_button(&mut self) { + if self.mouse_capture { + match execute!(std::io::stdout(), DisableMouseCapture) { + Ok(_) => self + .gui_state + .lock() + .set_info_box("✖ mouse capture disabled".to_owned()), + Err(_) => self + .app_data + .lock() + .set_error(AppError::MouseCapture(false)), + } + } else { + match execute!(std::io::stdout(), EnableMouseCapture) { + Ok(_) => self + .gui_state + .lock() + .set_info_box("✓ mouse capture enabled".to_owned()), + Err(_) => self.app_data.lock().set_error(AppError::MouseCapture(true)), + } + }; - let gui_state = Arc::clone(&self.gui_state); + 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() - })); + 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; + } /// Handle any keyboard button events async fn button_press(&mut self, key_code: KeyCode) { @@ -129,7 +129,7 @@ impl InputHandler { match key_code { KeyCode::Char('q') => self.is_running.store(false, Ordering::SeqCst), KeyCode::Char('h') => self.gui_state.lock().show_help = false, - KeyCode::Char('m') => self.m_button(), + KeyCode::Char('m') => self.m_button(), _ => (), } } else { diff --git a/src/ui/draw_blocks.rs b/src/ui/draw_blocks.rs index 3e85ab7..9f97085 100644 --- a/src/ui/draw_blocks.rs +++ b/src/ui/draw_blocks.rs @@ -35,6 +35,7 @@ a8" "8a `Y8, ,8P' 88 ,a8" a8P_____88 88P' "Y8 const NAME: &str = env!("CARGO_PKG_NAME"); const VERSION: &str = env!("CARGO_PKG_VERSION"); const REPO: &str = env!("CARGO_PKG_REPOSITORY"); +const DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION"); const ORANGE: Color = Color::Rgb(255, 178, 36); const MARGIN: &str = " "; @@ -45,7 +46,9 @@ fn generate_block<'a>( app_data: &Arc>, selected_panel: &SelectablePanel, ) -> Block<'a> { - let mut block = Block::default().borders(Borders::ALL).border_type(BorderType::Rounded); + let mut block = Block::default() + .borders(Borders::ALL) + .border_type(BorderType::Rounded); if let Some(panel) = selectable_panel { let title = match panel { @@ -65,10 +68,9 @@ fn generate_block<'a>( if selected_panel == &panel { // let selected_style = Style::default().fg(Color::LightCyan); // let selected_border = BorderType::Plain; - // let selected_border = BorderType::Rounded; - block = block - .border_style(Style::default().fg(Color::LightCyan)); - // .border_type(BorderType::Rounded); + // let selected_border = BorderType::Rounded; + block = block.border_style(Style::default().fg(Color::LightCyan)); + // .border_type(BorderType::Rounded); } } block @@ -172,8 +174,13 @@ pub fn draw_containers( format!("{}{:>width$}", MARGIN, mems, width = widths.mem.1), state_style, ), - Span::styled( - format!("{}{:>width$}", MARGIN, i.id.chars().take(8).collect::(), width = widths.id.1), + Span::styled( + format!( + "{}{:>width$}", + MARGIN, + i.id.chars().take(8).collect::(), + width = widths.id.1 + ), blue, ), Span::styled( @@ -337,7 +344,7 @@ fn make_chart( )) .borders(Borders::ALL) // .border_type(BorderType::Plain), - .border_type(BorderType::Rounded), + .border_type(BorderType::Rounded), ) .x_axis( Axis::default() @@ -386,15 +393,8 @@ pub fn draw_heading_bar( .push_str(format!("{}{:>width$}", MARGIN, columns.cpu.0, width = columns.cpu.1).as_str()); column_headings .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(format!("{}{:>width$}", MARGIN, columns.id.0, width = columns.id.1).as_str()); column_headings.push_str( format!( "{}{:>width$}", @@ -471,23 +471,20 @@ pub fn draw_heading_bar( pub fn draw_help_box(f: &mut Frame<'_, B>) { let title = format!(" {} ", VERSION); - let description_text = String::from("\n A basic docker container information viewer and controller"); + let description_text = format!("\n{}", DESCRIPTION); let mut help_text = String::from("\n ( tab ) or ( alt+tab ) to change panels"); - help_text.push_str("\n ( ↑ ↓ ← → ) mto change selected line"); - help_text.push_str("\n ( enter ) to send docker container commands"); + help_text.push_str("\n ( ↑ ↓ ← → ) mto change selected line"); + help_text.push_str("\n ( enter ) to send docker container commands"); help_text.push_str("\n ( h ) to toggle this help information"); - help_text.push_str("\n ( m ) to toggle mouse capture - if disabled, text on screen can be selected & copied, but mouse clicks get disabled"); - help_text.push_str("\n ( q ) to quit at any time"); + help_text.push_str("\n ( m ) to toggle mouse capture - if disabled, text on screen can be selected & copied, but mouse clicks get disabled"); + help_text.push_str("\n ( q ) to quit at any time"); help_text.push_str("\n mouse scrolling & clicking also available"); - help_text - .push_str("\n\n currenty an early work in progress, all and any input appreciated"); + help_text.push_str("\n\n currenty an early work in progress, all and any input appreciated"); help_text.push_str(format!("\n {}", REPO.trim()).as_str()); let mut max_line_width = 0; - - let all_text = format!("{}{}{}", NAME_TEXT, description_text, help_text); all_text.lines().into_iter().for_each(|line| { @@ -508,8 +505,7 @@ pub fn draw_help_box(f: &mut Frame<'_, B>) { .block(Block::default()) .alignment(Alignment::Center); - - let description_paragrpah = Paragraph::new(description_text.as_str()) + let description_paragrpah = Paragraph::new(description_text.as_str()) .style(Style::default().bg(Color::Magenta).fg(Color::Black)) .block(Block::default()) .alignment(Alignment::Center); @@ -537,7 +533,7 @@ pub fn draw_help_box(f: &mut Frame<'_, B>) { .constraints( [ Constraint::Max(NAME_TEXT.lines().count() as u16), - Constraint::Max(description_text.lines().count() as u16), + Constraint::Max(description_text.lines().count() as u16), Constraint::Max(help_text.lines().count() as u16), ] .as_ref(), @@ -547,7 +543,7 @@ pub fn draw_help_box(f: &mut Frame<'_, B>) { // Order is important here f.render_widget(Clear, area); f.render_widget(name_paragraph, split_popup[0]); - f.render_widget(description_paragrpah, split_popup[1]); + f.render_widget(description_paragrpah, split_popup[1]); f.render_widget(help_paragraph, split_popup[2]); f.render_widget(block, area); } @@ -646,18 +642,17 @@ pub fn draw_info(f: &mut Frame<'_, B>, text: String) { /// 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 { - - // Make sure blank_space can't be an negative, as will crash + // Make sure blank_space can't be an negative, as will crash let blank_vertical = if r.height > text_lines { - (r.height - text_lines) / 2 - } else { - 1 - }; - let blank_horizontal = if r.width > text_width { - (r.width - text_width) / 2 - }else { - 1 - }; + (r.height - text_lines) / 2 + } else { + 1 + }; + let blank_horizontal = if r.width > text_width { + (r.width - text_width) / 2 + } else { + 1 + }; let vertical_constraints = box_location.get_vertical_constraints(blank_vertical, text_lines); let horizontal_constraints =