diff options
author | ClementTsang <clementjhtsang@gmail.com> | 2019-09-16 22:39:57 -0400 |
---|---|---|
committer | ClementTsang <clementjhtsang@gmail.com> | 2019-09-16 22:39:57 -0400 |
commit | 1ebe04ecb2bd886bc0860933bb07ad5bc0a8b193 (patch) | |
tree | f9c10959af8a70e758fc954a266780a6b1093161 /src | |
parent | 0550402698e7f89f3da2f7a80a3c7c2ac2847a4d (diff) |
Added vim + keyboard bindings.
Diffstat (limited to 'src')
-rw-r--r-- | src/app.rs | 42 | ||||
-rw-r--r-- | src/canvas.rs | 65 | ||||
-rw-r--r-- | src/main.rs | 16 |
3 files changed, 101 insertions, 22 deletions
@@ -3,8 +3,7 @@ use data_collection::{processes, temperature}; mod process_killer; -#[allow(dead_code)] -// Probably only use the list elements... +#[derive(Clone, Copy)] pub enum ApplicationPosition { CPU, MEM, @@ -74,10 +73,6 @@ impl App { self.awaiting_d = true; } } - 'h' => self.on_right(), - 'j' => self.on_down(), - 'k' => self.on_up(), - 'l' => self.on_left(), 'c' => { // TODO: This should depend on what tile you're on! match self.process_sorting_type { @@ -137,16 +132,51 @@ impl App { Ok(()) } + // For now, these are hard coded --- in the future, they shouldn't be! + // + // General idea for now: + // CPU -(down)> MEM + // MEM -(down)> Network, -(right)> TEMP + // TEMP -(down)> Disk, -(left)> MEM, -(up)> CPU + // Disk -(down)> Processes, -(left)> MEM, -(up)> TEMP + // Network -(up)> MEM, -(right)> PROC + // PROC -(up)> Disk, -(left)> Network pub fn on_left(&mut self) { + self.current_application_position = match self.current_application_position { + ApplicationPosition::PROCESS => ApplicationPosition::NETWORK, + ApplicationPosition::DISK => ApplicationPosition::MEM, + ApplicationPosition::TEMP => ApplicationPosition::MEM, + _ => self.current_application_position, + }; } pub fn on_right(&mut self) { + self.current_application_position = match self.current_application_position { + ApplicationPosition::MEM => ApplicationPosition::TEMP, + ApplicationPosition::NETWORK => ApplicationPosition::PROCESS, + _ => self.current_application_position, + }; } pub fn on_up(&mut self) { + self.current_application_position = match self.current_application_position { + ApplicationPosition::MEM => ApplicationPosition::CPU, + ApplicationPosition::NETWORK => ApplicationPosition::MEM, + ApplicationPosition::PROCESS => ApplicationPosition::DISK, + ApplicationPosition::TEMP => ApplicationPosition::CPU, + ApplicationPosition::DISK => ApplicationPosition::TEMP, + _ => self.current_application_position, + }; } pub fn on_down(&mut self) { + self.current_application_position = match self.current_application_position { + ApplicationPosition::CPU => ApplicationPosition::MEM, + ApplicationPosition::MEM => ApplicationPosition::NETWORK, + ApplicationPosition::TEMP => ApplicationPosition::DISK, + ApplicationPosition::DISK => ApplicationPosition::PROCESS, + _ => self.current_application_position, + }; } pub fn decrement_position_count(&mut self) { diff --git a/src/canvas.rs b/src/canvas.rs index afec5ba0..6cdf276e 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -11,6 +11,7 @@ const COLOUR_LIST : [Color; 6] = [Color::Red, Color::Green, Color::LightYellow, const TEXT_COLOUR : Color = Color::Gray; const GRAPH_COLOUR : Color = Color::Gray; const BORDER_STYLE_COLOUR : Color = Color::Gray; +const HIGHLIGHTED_BORDER_STYLE_COLOUR : Color = Color::LightBlue; const GRAPH_MARKER : Marker = Marker::Braille; #[derive(Default)] @@ -29,6 +30,7 @@ pub struct CanvasData { pub fn draw_data<B : tui::backend::Backend>(terminal : &mut Terminal<B>, app_state : &mut app::App, canvas_data : &CanvasData) -> error::Result<()> { let border_style : Style = Style::default().fg(BORDER_STYLE_COLOUR); + let highlighted_border_style : Style = Style::default().fg(HIGHLIGHTED_BORDER_STYLE_COLOUR); let temperature_rows = canvas_data .temp_sensor_data @@ -102,7 +104,15 @@ pub fn draw_data<B : tui::backend::Backend>(terminal : &mut Terminal<B>, app_sta } Chart::default() - .block(Block::default().title("CPU Usage").borders(Borders::ALL).border_style(border_style)) + .block( + Block::default() + .title("CPU Usage") + .borders(Borders::ALL) + .border_style(match app_state.current_application_position { + app::ApplicationPosition::CPU => highlighted_border_style, + _ => border_style, + }), + ) .x_axis(x_axis) .y_axis(y_axis) .datasets(&dataset_vector) @@ -114,7 +124,15 @@ pub fn draw_data<B : tui::backend::Backend>(terminal : &mut Terminal<B>, app_sta let x_axis : Axis<String> = Axis::default().style(Style::default().fg(GRAPH_COLOUR)).bounds([0.0, 600_000.0]); let y_axis = Axis::default().style(Style::default().fg(GRAPH_COLOUR)).bounds([-0.5, 100.5]).labels(&["0%", "100%"]); // Offset as the zero value isn't drawn otherwise... Chart::default() - .block(Block::default().title("Memory Usage").borders(Borders::ALL).border_style(border_style)) + .block( + Block::default() + .title("Memory Usage") + .borders(Borders::ALL) + .border_style(match app_state.current_application_position { + app::ApplicationPosition::MEM => highlighted_border_style, + _ => border_style, + }), + ) .x_axis(x_axis) .y_axis(y_axis) .datasets(&[ @@ -136,7 +154,15 @@ pub fn draw_data<B : tui::backend::Backend>(terminal : &mut Terminal<B>, app_sta { let width = f64::from(middle_divided_chunk_2[0].width); Table::new(["Sensor", "Temp"].iter(), temperature_rows) - .block(Block::default().title("Temperatures").borders(Borders::ALL).border_style(border_style)) + .block( + Block::default() + .title("Temperatures") + .borders(Borders::ALL) + .border_style(match app_state.current_application_position { + app::ApplicationPosition::TEMP => highlighted_border_style, + _ => border_style, + }), + ) .header_style(Style::default().fg(Color::LightBlue)) .widths(&[(width * 0.45) as u16, (width * 0.4) as u16]) .render(&mut f, middle_divided_chunk_2[0]); @@ -144,10 +170,18 @@ pub fn draw_data<B : tui::backend::Backend>(terminal : &mut Terminal<B>, app_sta // Disk usage table { - // TODO: We have to dynamically remove some of these table elements based on size... + // TODO: We may have to dynamically remove some of these table elements based on size... let width = f64::from(middle_divided_chunk_2[1].width); Table::new(["Disk", "Mount", "Used", "Total", "Free", "R/s", "W/s"].iter(), disk_rows) - .block(Block::default().title("Disk Usage").borders(Borders::ALL).border_style(border_style)) + .block( + Block::default() + .title("Disk Usage") + .borders(Borders::ALL) + .border_style(match app_state.current_application_position { + app::ApplicationPosition::DISK => highlighted_border_style, + _ => border_style, + }), + ) .header_style(Style::default().fg(Color::LightBlue).modifier(Modifier::BOLD)) .widths(&[ (width * 0.18).floor() as u16, @@ -166,7 +200,15 @@ pub fn draw_data<B : tui::backend::Backend>(terminal : &mut Terminal<B>, app_sta let x_axis : Axis<String> = Axis::default().style(Style::default().fg(GRAPH_COLOUR)).bounds([0.0, 600_000.0]); let y_axis = Axis::default().style(Style::default().fg(GRAPH_COLOUR)).bounds([-0.5, 1_000_000.5]).labels(&["0GB", "1GB"]); Chart::default() - .block(Block::default().title("Network").borders(Borders::ALL).border_style(border_style)) + .block( + Block::default() + .title("Network") + .borders(Borders::ALL) + .border_style(match app_state.current_application_position { + app::ApplicationPosition::NETWORK => highlighted_border_style, + _ => border_style, + }), + ) .x_axis(x_axis) .y_axis(y_axis) .datasets(&[ @@ -196,7 +238,6 @@ pub fn draw_data<B : tui::backend::Backend>(terminal : &mut Terminal<B>, app_sta let num_rows = i64::from(bottom_chunks[1].height) - 3; let mut process_counter = 0; - //TODO: Fix this! let start_position = match app_state.scroll_direction { app::ScrollDirection::DOWN => { if app_state.currently_selected_process_position < num_rows { @@ -251,7 +292,15 @@ pub fn draw_data<B : tui::backend::Backend>(terminal : &mut Terminal<B>, app_sta }); Table::new(["PID", "Name", "CPU%", "Mem%"].iter(), process_rows) - .block(Block::default().title("Processes").borders(Borders::ALL).border_style(border_style)) + .block( + Block::default() + .title("Processes") + .borders(Borders::ALL) + .border_style(match app_state.current_application_position { + app::ApplicationPosition::PROCESS => highlighted_border_style, + _ => border_style, + }), + ) .header_style(Style::default().fg(Color::LightBlue)) .widths(&[(width * 0.2) as u16, (width * 0.35) as u16, (width * 0.2) as u16, (width * 0.2) as u16]) .render(&mut f, bottom_chunks[1]); diff --git a/src/main.rs b/src/main.rs index 40b66018..fc753d7f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,7 +34,7 @@ enum Event<I, J> { } fn main() -> error::Result<()> { - let _log = utils::logging::init_logger(); // TODO: Error handling + let _log = utils::logging::init_logger(); // TODO: Note this could fail and we wouldn't know... consider whether it is worth dealing with // Parse command line options let matches = clap_app!(app => @@ -42,9 +42,9 @@ fn main() -> error::Result<()> { (version: crate_version!()) (author: "Clement Tsang <clementjhtsang@gmail.com>") (about: "A graphical top clone.") - (@arg THEME: -t --theme +takes_value "Sets a colour theme.") + //(@arg THEME: -t --theme +takes_value "Sets a colour theme.") (@arg AVG_CPU: -a --avgcpu "Enables showing the average CPU usage.") - (@arg DEBUG: -d --debug "Enables debug mode.") // TODO: This isn't done yet! + //(@arg DEBUG: -d --debug "Enables debug mode.") // TODO: This isn't done yet! (@group TEMPERATURE_TYPE => (@arg CELSIUS : -c --celsius "Sets the temperature type to Celsius. This is the default option.") (@arg FAHRENHEIT : -f --fahrenheit "Sets the temperature type to Fahrenheit.") @@ -52,7 +52,7 @@ fn main() -> error::Result<()> { ) (@arg RATE: -r --rate +takes_value "Sets a refresh rate in milliseconds, min is 250ms, defaults to 1000ms. Higher values may take more resources.") ) - .after_help("Themes:") + //.after_help("Themes:") // TODO: This and others disabled for now .get_matches(); let update_rate_in_milliseconds : u128 = matches.value_of("RATE").unwrap_or("1000").parse::<u128>()?; @@ -149,11 +149,11 @@ fn main() -> error::Result<()> { // debug!("Keyboard event fired!"); match event { KeyEvent::Ctrl('c') | KeyEvent::Esc => break, + KeyEvent::Char('h') | KeyEvent::Left => app.on_left(), + KeyEvent::Char('l') | KeyEvent::Right => app.on_right(), + KeyEvent::Char('k') | KeyEvent::Up => app.on_up(), + KeyEvent::Char('j') | KeyEvent::Down => app.on_down(), KeyEvent::Char(c) => app.on_key(c), // TODO: We can remove the 'q' event and just move it to the quit? - KeyEvent::Left => app.on_left(), - KeyEvent::Right => app.on_right(), - KeyEvent::Up => app.on_up(), - KeyEvent::Down => app.on_down(), _ => {} } |