2020-01-05 00:20:54 +00:00
|
|
|
use crate::{
|
|
|
|
app, constants,
|
2020-01-29 02:24:52 +00:00
|
|
|
data_conversion::{ConvertedCpuData, ConvertedProcessHarvest},
|
2020-01-05 00:20:54 +00:00
|
|
|
utils::{error, gen_util::*},
|
|
|
|
};
|
2020-01-11 22:30:04 +00:00
|
|
|
use std::cmp::max;
|
2019-12-06 05:57:04 +00:00
|
|
|
use tui::{
|
2019-09-17 03:53:20 +00:00
|
|
|
backend,
|
2020-01-01 22:55:15 +00:00
|
|
|
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
2020-01-12 20:41:27 +00:00
|
|
|
style::{Color, Style},
|
2019-12-28 03:39:25 +00:00
|
|
|
terminal::Frame,
|
2020-01-01 22:55:15 +00:00
|
|
|
widgets::{Axis, Block, Borders, Chart, Dataset, Marker, Paragraph, Row, Table, Text, Widget},
|
2019-09-11 03:37:20 +00:00
|
|
|
Terminal,
|
|
|
|
};
|
2019-09-12 02:10:49 +00:00
|
|
|
|
2019-12-06 05:57:04 +00:00
|
|
|
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;
|
2020-01-12 20:41:27 +00:00
|
|
|
const TABLE_HEADER_COLOUR: Color = Color::LightBlue;
|
2019-12-27 23:19:57 +00:00
|
|
|
const GOLDEN_RATIO: f32 = 0.618_034; // Approx, good enough for use (also Clippy gets mad if it's too long)
|
2019-12-27 00:06:30 +00:00
|
|
|
|
2020-01-11 22:30:04 +00:00
|
|
|
// Headers
|
|
|
|
const CPU_LEGEND_HEADER: [&str; 2] = ["CPU", "Use%"];
|
|
|
|
const DISK_HEADERS: [&str; 7] = ["Disk", "Mount", "Used", "Free", "Total", "R/s", "W/s"];
|
|
|
|
const TEMP_HEADERS: [&str; 2] = ["Sensor", "Temp"];
|
2020-01-15 02:24:10 +00:00
|
|
|
const MEM_HEADERS: [&str; 3] = ["Mem", "Usage", "Usage%"];
|
2020-01-11 22:30:04 +00:00
|
|
|
const NON_WINDOWS_NETWORK_HEADERS: [&str; 4] = ["RX", "TX", "Total RX", "Total TX"];
|
|
|
|
const WINDOWS_NETWORK_HEADERS: [&str; 2] = ["RX", "TX"];
|
|
|
|
const FORCE_MIN_THRESHOLD: usize = 5;
|
|
|
|
|
2019-12-27 00:06:30 +00:00
|
|
|
lazy_static! {
|
2020-01-18 00:59:01 +00:00
|
|
|
static ref HELP_TEXT: [Text<'static>; 20] = [
|
2019-12-27 00:06:30 +00:00
|
|
|
Text::raw("\nGeneral Keybindings\n"),
|
2020-01-12 20:41:27 +00:00
|
|
|
Text::raw("q, Ctrl-c to quit. Note if you are currently in the search widget, `q` will not work.\n"),
|
2019-12-27 00:06:30 +00:00
|
|
|
Text::raw("Ctrl-r to reset all data.\n"),
|
|
|
|
Text::raw("f to toggle freezing and unfreezing the display.\n"),
|
2020-01-08 04:40:53 +00:00
|
|
|
Text::raw(
|
2020-01-12 19:25:47 +00:00
|
|
|
"Ctrl-Up or Ctrl-k, Ctrl-Down or Ctrl-j, Ctrl-Left or Ctrl-h, Ctrl-Right or Ctrl-l to navigate between widgets.\n"
|
2020-01-08 04:40:53 +00:00
|
|
|
),
|
2020-01-12 19:25:47 +00:00
|
|
|
Text::raw("Up or k and Down or j scrolls through a list.\n"),
|
2019-12-27 00:06:30 +00:00
|
|
|
Text::raw("Esc to close a dialog window (help or dd confirmation).\n"),
|
|
|
|
Text::raw("? to get this help screen.\n"),
|
2020-01-12 18:18:01 +00:00
|
|
|
Text::raw("\n Process Widget Keybindings\n"),
|
2019-12-27 00:06:30 +00:00
|
|
|
Text::raw("dd to kill the selected process.\n"),
|
|
|
|
Text::raw("c to sort by CPU usage.\n"),
|
|
|
|
Text::raw("m to sort by memory usage.\n"),
|
|
|
|
Text::raw("p to sort by PID.\n"),
|
|
|
|
Text::raw("n to sort by process name.\n"),
|
2020-01-12 19:25:47 +00:00
|
|
|
Text::raw("Tab to group together processes with the same name.\n"),
|
2020-01-18 00:59:01 +00:00
|
|
|
Text::raw("Ctrl-f to toggle searching for a process. / to just open it.\n"),
|
|
|
|
Text::raw("Use Ctrl-p and Ctrl-n to toggle between searching for PID and name.\n"),
|
|
|
|
Text::raw("Use Ctrl-a and Ctrl-e to set the cursor to the start and end of the bar respectively.\n"),
|
|
|
|
Text::raw("Use Ctrl-s to toggle between simple and regex search.\n"),
|
2020-01-12 19:25:47 +00:00
|
|
|
Text::raw("\nFor startup flags, type in \"btm -h\".")
|
2019-12-27 00:06:30 +00:00
|
|
|
];
|
|
|
|
static ref COLOUR_LIST: Vec<Color> = gen_n_colours(constants::NUM_COLOURS);
|
2019-12-28 03:39:25 +00:00
|
|
|
static ref CANVAS_BORDER_STYLE: Style = Style::default().fg(BORDER_STYLE_COLOUR);
|
2020-01-08 04:40:53 +00:00
|
|
|
static ref CANVAS_HIGHLIGHTED_BORDER_STYLE: Style =
|
|
|
|
Style::default().fg(HIGHLIGHTED_BORDER_STYLE_COLOUR);
|
2020-01-11 22:30:04 +00:00
|
|
|
static ref DISK_HEADERS_LENS: Vec<usize> = DISK_HEADERS
|
|
|
|
.iter()
|
|
|
|
.map(|entry| max(FORCE_MIN_THRESHOLD, entry.len()))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
static ref CPU_LEGEND_HEADER_LENS: Vec<usize> = CPU_LEGEND_HEADER
|
|
|
|
.iter()
|
|
|
|
.map(|entry| max(FORCE_MIN_THRESHOLD, entry.len()))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
static ref TEMP_HEADERS_LENS: Vec<usize> = TEMP_HEADERS
|
|
|
|
.iter()
|
|
|
|
.map(|entry| max(FORCE_MIN_THRESHOLD, entry.len()))
|
|
|
|
.collect::<Vec<_>>();
|
2020-01-14 03:06:44 +00:00
|
|
|
static ref MEM_HEADERS_LENS: Vec<usize> = MEM_HEADERS
|
|
|
|
.iter()
|
|
|
|
.map(|entry| max(FORCE_MIN_THRESHOLD, entry.len()))
|
|
|
|
.collect::<Vec<_>>();
|
2020-01-11 22:30:04 +00:00
|
|
|
static ref NON_WINDOWS_NETWORK_HEADERS_LENS: Vec<usize> = NON_WINDOWS_NETWORK_HEADERS
|
|
|
|
.iter()
|
|
|
|
.map(|entry| max(FORCE_MIN_THRESHOLD, entry.len()))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
static ref WINDOWS_NETWORK_HEADERS_LENS: Vec<usize> = WINDOWS_NETWORK_HEADERS
|
|
|
|
.iter()
|
|
|
|
.map(|entry| max(FORCE_MIN_THRESHOLD, entry.len()))
|
|
|
|
.collect::<Vec<_>>();
|
2019-12-27 00:06:30 +00:00
|
|
|
}
|
2019-09-12 02:10:49 +00:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct CanvasData {
|
2019-12-06 05:57:04 +00:00
|
|
|
pub rx_display: String,
|
|
|
|
pub tx_display: String,
|
2019-12-27 23:30:35 +00:00
|
|
|
pub total_rx_display: String,
|
|
|
|
pub total_tx_display: String,
|
2019-12-06 05:57:04 +00:00
|
|
|
pub network_data_rx: Vec<(f64, f64)>,
|
|
|
|
pub network_data_tx: Vec<(f64, f64)>,
|
|
|
|
pub disk_data: Vec<Vec<String>>,
|
|
|
|
pub temp_sensor_data: Vec<Vec<String>>,
|
2020-01-29 02:24:52 +00:00
|
|
|
pub process_data: Vec<ConvertedProcessHarvest>,
|
|
|
|
pub grouped_process_data: Vec<ConvertedProcessHarvest>,
|
2020-01-26 21:44:24 +00:00
|
|
|
pub mem_label: String,
|
|
|
|
pub swap_label: String,
|
2019-12-06 05:57:04 +00:00
|
|
|
pub mem_data: Vec<(f64, f64)>,
|
|
|
|
pub swap_data: Vec<(f64, f64)>,
|
2020-01-05 00:20:54 +00:00
|
|
|
pub cpu_data: Vec<ConvertedCpuData>,
|
2019-09-12 02:10:49 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 00:06:30 +00:00
|
|
|
/// Generates random colours.
|
|
|
|
/// Strategy found from https://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/
|
|
|
|
fn gen_n_colours(num_to_gen: i32) -> Vec<Color> {
|
2019-12-27 21:47:04 +00:00
|
|
|
fn gen_hsv(h: f32) -> f32 {
|
|
|
|
let new_val = h + GOLDEN_RATIO;
|
|
|
|
if new_val > 1.0 {
|
|
|
|
new_val.fract()
|
|
|
|
} else {
|
|
|
|
new_val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// This takes in an h, s, and v value of range [0, 1]
|
|
|
|
/// For explanation of what this does, see
|
|
|
|
/// https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative
|
|
|
|
fn hsv_to_rgb(hue: f32, saturation: f32, value: f32) -> (u8, u8, u8) {
|
|
|
|
fn hsv_helper(num: u32, hu: f32, sat: f32, val: f32) -> f32 {
|
|
|
|
let k = (num as f32 + hu * 6.0) % 6.0;
|
|
|
|
val - val * sat * float_max(float_min(k, float_min(4.1 - k, 1.1)), 0.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
(
|
|
|
|
(hsv_helper(5, hue, saturation, value) * 255.0) as u8,
|
|
|
|
(hsv_helper(3, hue, saturation, value) * 255.0) as u8,
|
|
|
|
(hsv_helper(1, hue, saturation, value) * 255.0) as u8,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate colours
|
2020-01-08 04:40:53 +00:00
|
|
|
let mut colour_vec: Vec<Color> = vec![
|
|
|
|
Color::Red,
|
2020-01-22 03:59:42 +00:00
|
|
|
Color::LightYellow,
|
2020-01-08 04:40:53 +00:00
|
|
|
Color::LightMagenta,
|
2020-01-22 03:59:42 +00:00
|
|
|
Color::LightCyan,
|
|
|
|
Color::Green,
|
2020-01-08 04:40:53 +00:00
|
|
|
];
|
2019-12-27 00:06:30 +00:00
|
|
|
|
|
|
|
let mut h: f32 = 0.4; // We don't need random colours... right?
|
|
|
|
for _i in 0..num_to_gen {
|
|
|
|
h = gen_hsv(h);
|
|
|
|
let result = hsv_to_rgb(h, 0.5, 0.95);
|
|
|
|
colour_vec.push(Color::Rgb(result.0, result.1, result.2));
|
|
|
|
}
|
|
|
|
|
|
|
|
colour_vec
|
|
|
|
}
|
|
|
|
|
2020-01-15 03:28:11 +00:00
|
|
|
#[allow(unused_variables)]
|
2020-01-08 04:40:53 +00:00
|
|
|
pub fn draw_data<B: backend::Backend>(
|
|
|
|
terminal: &mut Terminal<B>, app_state: &mut app::App,
|
|
|
|
) -> error::Result<()> {
|
2019-10-09 23:19:39 +00:00
|
|
|
terminal.autoresize()?;
|
2019-09-12 02:10:49 +00:00
|
|
|
terminal.draw(|mut f| {
|
2019-12-07 05:58:52 +00:00
|
|
|
if app_state.show_help {
|
2020-01-02 04:39:47 +00:00
|
|
|
// Only for the help
|
2019-12-07 05:58:52 +00:00
|
|
|
let vertical_dialog_chunk = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.margin(1)
|
2020-01-08 04:40:53 +00:00
|
|
|
.constraints(
|
|
|
|
[
|
2020-01-18 00:59:01 +00:00
|
|
|
Constraint::Percentage(22),
|
|
|
|
Constraint::Percentage(60),
|
|
|
|
Constraint::Percentage(18),
|
2020-01-08 04:40:53 +00:00
|
|
|
]
|
|
|
|
.as_ref(),
|
|
|
|
)
|
2019-12-07 05:58:52 +00:00
|
|
|
.split(f.size());
|
2019-09-12 03:15:25 +00:00
|
|
|
|
2019-12-07 05:58:52 +00:00
|
|
|
let middle_dialog_chunk = Layout::default()
|
|
|
|
.direction(Direction::Horizontal)
|
|
|
|
.margin(0)
|
2020-01-08 04:40:53 +00:00
|
|
|
.constraints(
|
|
|
|
[
|
2020-01-18 00:59:01 +00:00
|
|
|
Constraint::Percentage(20),
|
|
|
|
Constraint::Percentage(60),
|
|
|
|
Constraint::Percentage(20),
|
2020-01-08 04:40:53 +00:00
|
|
|
]
|
|
|
|
.as_ref(),
|
|
|
|
)
|
2019-12-07 05:58:52 +00:00
|
|
|
.split(vertical_dialog_chunk[1]);
|
2019-10-10 02:00:10 +00:00
|
|
|
|
2019-12-27 00:06:30 +00:00
|
|
|
Paragraph::new(HELP_TEXT.iter())
|
2020-01-08 04:40:53 +00:00
|
|
|
.block(
|
|
|
|
Block::default()
|
|
|
|
.title("Help (Press Esc to close)")
|
|
|
|
.borders(Borders::ALL),
|
|
|
|
)
|
2019-10-10 02:00:10 +00:00
|
|
|
.style(Style::default().fg(Color::Gray))
|
|
|
|
.alignment(Alignment::Left)
|
|
|
|
.wrap(true)
|
|
|
|
.render(&mut f, middle_dialog_chunk[1]);
|
2020-01-02 04:39:47 +00:00
|
|
|
} else if app_state.show_dd {
|
|
|
|
let vertical_dialog_chunk = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.margin(1)
|
2020-01-08 04:40:53 +00:00
|
|
|
.constraints(
|
|
|
|
[
|
|
|
|
Constraint::Percentage(40),
|
|
|
|
Constraint::Percentage(20),
|
|
|
|
Constraint::Percentage(40),
|
|
|
|
]
|
|
|
|
.as_ref(),
|
|
|
|
)
|
2020-01-02 04:39:47 +00:00
|
|
|
.split(f.size());
|
|
|
|
|
|
|
|
let middle_dialog_chunk = Layout::default()
|
|
|
|
.direction(Direction::Horizontal)
|
|
|
|
.margin(0)
|
2020-01-08 04:40:53 +00:00
|
|
|
.constraints(
|
|
|
|
[
|
|
|
|
Constraint::Percentage(30),
|
|
|
|
Constraint::Percentage(40),
|
|
|
|
Constraint::Percentage(30),
|
|
|
|
]
|
|
|
|
.as_ref(),
|
|
|
|
)
|
2020-01-02 04:39:47 +00:00
|
|
|
.split(vertical_dialog_chunk[1]);
|
|
|
|
|
|
|
|
if let Some(dd_err) = app_state.dd_err.clone() {
|
2020-01-08 04:40:53 +00:00
|
|
|
let dd_text = [Text::raw(format!(
|
|
|
|
"\nFailure to properly kill the process - {}",
|
|
|
|
dd_err
|
|
|
|
))];
|
2020-01-02 04:39:47 +00:00
|
|
|
|
|
|
|
Paragraph::new(dd_text.iter())
|
2020-01-08 04:40:53 +00:00
|
|
|
.block(
|
|
|
|
Block::default()
|
|
|
|
.title("Kill Process Error (Press Esc to close)")
|
|
|
|
.borders(Borders::ALL),
|
|
|
|
)
|
2020-01-02 04:39:47 +00:00
|
|
|
.style(Style::default().fg(Color::Gray))
|
|
|
|
.alignment(Alignment::Center)
|
|
|
|
.wrap(true)
|
|
|
|
.render(&mut f, middle_dialog_chunk[1]);
|
2020-01-09 03:36:36 +00:00
|
|
|
} else if let Some(process_list) = app_state.get_current_highlighted_process_list() {
|
|
|
|
if let Some(process) = process_list.first() {
|
|
|
|
let dd_text = [
|
|
|
|
if app_state.is_grouped() {
|
|
|
|
Text::raw(format!(
|
|
|
|
"\nAre you sure you want to kill {} process(es) with name {}?",
|
|
|
|
process_list.len(), process.name
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Text::raw(format!(
|
|
|
|
"\nAre you sure you want to kill process {} with PID {}?",
|
|
|
|
process.name, process.pid
|
|
|
|
))
|
|
|
|
},
|
2020-01-02 04:39:47 +00:00
|
|
|
Text::raw("\n\nPress ENTER to proceed, ESC to exit."),
|
|
|
|
Text::raw("\nNote that if bottom is frozen, it must be unfrozen for changes to be shown."),
|
|
|
|
];
|
|
|
|
|
2020-01-09 03:36:36 +00:00
|
|
|
Paragraph::new(dd_text.iter())
|
|
|
|
.block(
|
|
|
|
Block::default()
|
|
|
|
.title("Kill Process Confirmation (Press Esc to close)")
|
|
|
|
.borders(Borders::ALL),
|
|
|
|
)
|
|
|
|
.style(Style::default().fg(Color::Gray))
|
|
|
|
.alignment(Alignment::Center)
|
|
|
|
.wrap(true)
|
|
|
|
.render(&mut f, middle_dialog_chunk[1]);
|
|
|
|
} else {
|
|
|
|
app_state.show_dd = false;
|
|
|
|
}
|
2020-01-02 04:39:47 +00:00
|
|
|
} else {
|
|
|
|
// This is a bit nasty, but it works well... I guess.
|
|
|
|
app_state.show_dd = false;
|
|
|
|
}
|
2019-12-06 05:57:04 +00:00
|
|
|
} else {
|
2019-10-10 02:00:10 +00:00
|
|
|
let vertical_chunks = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.margin(1)
|
2020-01-08 04:40:53 +00:00
|
|
|
.constraints(
|
|
|
|
[
|
2020-01-29 03:38:36 +00:00
|
|
|
Constraint::Percentage(30),
|
|
|
|
Constraint::Percentage(36),
|
2020-01-08 04:40:53 +00:00
|
|
|
Constraint::Percentage(34),
|
|
|
|
]
|
|
|
|
.as_ref(),
|
|
|
|
)
|
2019-10-10 02:00:10 +00:00
|
|
|
.split(f.size());
|
|
|
|
|
|
|
|
let middle_chunks = Layout::default()
|
|
|
|
.direction(Direction::Horizontal)
|
|
|
|
.margin(0)
|
|
|
|
.constraints([Constraint::Percentage(60), Constraint::Percentage(40)].as_ref())
|
|
|
|
.split(vertical_chunks[1]);
|
|
|
|
|
|
|
|
let middle_divided_chunk_2 = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.margin(0)
|
|
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
|
|
|
.split(middle_chunks[1]);
|
|
|
|
|
2020-01-14 03:06:44 +00:00
|
|
|
let middle_divide_chunk_3 = Layout::default()
|
|
|
|
.direction(Direction::Horizontal)
|
|
|
|
.margin(0)
|
|
|
|
.constraints([Constraint::Percentage(40), Constraint::Percentage(60)].as_ref())
|
|
|
|
.split(middle_divided_chunk_2[0]);
|
|
|
|
|
2019-10-10 02:00:10 +00:00
|
|
|
let bottom_chunks = Layout::default()
|
|
|
|
.direction(Direction::Horizontal)
|
|
|
|
.margin(0)
|
|
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
|
|
|
.split(vertical_chunks[2]);
|
|
|
|
|
2019-12-11 06:01:24 +00:00
|
|
|
// Component specific chunks
|
2019-12-28 03:39:25 +00:00
|
|
|
let cpu_chunk = Layout::default()
|
2019-12-11 06:01:24 +00:00
|
|
|
.direction(Direction::Horizontal)
|
|
|
|
.margin(0)
|
2019-12-28 03:39:25 +00:00
|
|
|
.constraints(
|
|
|
|
if app_state.left_legend {
|
2020-01-01 22:56:28 +00:00
|
|
|
[Constraint::Percentage(15), Constraint::Percentage(85)]
|
2019-12-28 03:39:25 +00:00
|
|
|
} else {
|
2020-01-01 22:56:28 +00:00
|
|
|
[Constraint::Percentage(85), Constraint::Percentage(15)]
|
2019-12-28 03:39:25 +00:00
|
|
|
}
|
|
|
|
.as_ref(),
|
|
|
|
)
|
2019-12-11 06:01:24 +00:00
|
|
|
.split(vertical_chunks[0]);
|
|
|
|
|
2019-12-28 03:39:25 +00:00
|
|
|
let network_chunk = Layout::default()
|
2019-12-28 06:20:05 +00:00
|
|
|
.direction(Direction::Vertical)
|
2019-12-11 06:01:24 +00:00
|
|
|
.margin(0)
|
2020-01-15 05:38:43 +00:00
|
|
|
.constraints(
|
|
|
|
if (bottom_chunks[0].height as f64 * 0.25) as u16 >= 4 {
|
|
|
|
[Constraint::Percentage(75), Constraint::Percentage(25)]
|
|
|
|
} else {
|
|
|
|
let required = if bottom_chunks[0].height < 10 {
|
|
|
|
bottom_chunks[0].height / 2
|
|
|
|
} else {
|
|
|
|
5
|
|
|
|
};
|
|
|
|
let remaining = bottom_chunks[0].height - required;
|
|
|
|
[Constraint::Length(remaining), Constraint::Length(required)]
|
|
|
|
}
|
|
|
|
.as_ref(),
|
|
|
|
)
|
2019-12-11 06:01:24 +00:00
|
|
|
.split(bottom_chunks[0]);
|
|
|
|
|
2019-12-28 03:39:25 +00:00
|
|
|
// Default chunk index based on left or right legend setting
|
|
|
|
let legend_index = if app_state.left_legend { 0 } else { 1 };
|
|
|
|
let graph_index = if app_state.left_legend { 1 } else { 0 };
|
2019-10-10 02:00:10 +00:00
|
|
|
|
2019-12-28 03:39:25 +00:00
|
|
|
// Set up blocks and their components
|
|
|
|
// CPU graph
|
2020-01-02 04:39:47 +00:00
|
|
|
draw_cpu_graph(&mut f, &app_state, cpu_chunk[graph_index]);
|
2019-09-15 05:29:49 +00:00
|
|
|
|
2020-01-01 22:55:15 +00:00
|
|
|
// CPU legend
|
2020-01-02 04:39:47 +00:00
|
|
|
draw_cpu_legend(&mut f, app_state, cpu_chunk[legend_index]);
|
2019-09-15 05:29:49 +00:00
|
|
|
|
2019-10-10 02:00:10 +00:00
|
|
|
//Memory usage graph
|
2020-01-15 03:28:11 +00:00
|
|
|
draw_memory_graph(&mut f, &app_state, middle_chunks[0]);
|
2019-10-10 02:00:10 +00:00
|
|
|
|
|
|
|
// Network graph
|
2020-01-02 23:34:09 +00:00
|
|
|
draw_network_graph(&mut f, &app_state, network_chunk[0]);
|
2019-10-10 02:00:10 +00:00
|
|
|
|
2020-01-02 04:39:47 +00:00
|
|
|
draw_network_labels(&mut f, app_state, network_chunk[1]);
|
2019-12-28 06:20:05 +00:00
|
|
|
|
|
|
|
// Temperature table
|
2020-01-15 03:28:11 +00:00
|
|
|
draw_temp_table(&mut f, app_state, middle_divided_chunk_2[0]);
|
2019-12-28 06:20:05 +00:00
|
|
|
|
|
|
|
// Disk usage table
|
2020-01-02 04:39:47 +00:00
|
|
|
draw_disk_table(&mut f, app_state, middle_divided_chunk_2[1]);
|
2019-10-10 02:00:10 +00:00
|
|
|
|
2019-12-28 03:39:25 +00:00
|
|
|
// Processes table
|
2020-01-12 19:25:47 +00:00
|
|
|
if app_state.is_searching() {
|
|
|
|
let processes_chunk = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.margin(0)
|
|
|
|
.constraints([Constraint::Percentage(25), Constraint::Percentage(75)].as_ref())
|
|
|
|
.split(bottom_chunks[1]);
|
|
|
|
draw_search_field(&mut f, app_state, processes_chunk[0]);
|
|
|
|
draw_processes_table(&mut f, app_state, processes_chunk[1]);
|
|
|
|
} else {
|
|
|
|
draw_processes_table(&mut f, app_state, bottom_chunks[1]);
|
|
|
|
}
|
2019-09-14 20:46:14 +00:00
|
|
|
}
|
2019-09-12 02:10:49 +00:00
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-09-25 20:43:13 +00:00
|
|
|
|
2020-01-02 04:39:47 +00:00
|
|
|
fn draw_cpu_graph<B: backend::Backend>(f: &mut Frame<B>, app_state: &app::App, draw_loc: Rect) {
|
2020-01-05 00:20:54 +00:00
|
|
|
let cpu_data: &[ConvertedCpuData] = &app_state.canvas_data.cpu_data;
|
2019-12-28 03:39:25 +00:00
|
|
|
|
2020-01-02 04:39:47 +00:00
|
|
|
// CPU usage graph
|
2019-12-28 03:39:25 +00:00
|
|
|
let x_axis: Axis<String> = Axis::default()
|
|
|
|
.style(Style::default().fg(GRAPH_COLOUR))
|
2020-01-27 01:14:14 +00:00
|
|
|
.bounds([0.0, constants::TIME_STARTS_FROM as f64]);
|
2019-12-28 03:39:25 +00:00
|
|
|
let y_axis = Axis::default()
|
|
|
|
.style(Style::default().fg(GRAPH_COLOUR))
|
|
|
|
.bounds([-0.5, 100.5])
|
|
|
|
.labels(&["0%", "100%"]);
|
|
|
|
|
|
|
|
let mut dataset_vector: Vec<Dataset> = Vec::new();
|
2020-01-05 00:20:54 +00:00
|
|
|
let mut cpu_entries_vec: Vec<(Style, Vec<(f64, f64)>)> = Vec::new();
|
2019-12-28 03:39:25 +00:00
|
|
|
|
|
|
|
for (i, cpu) in cpu_data.iter().enumerate() {
|
|
|
|
let mut avg_cpu_exist_offset = 0;
|
|
|
|
if app_state.show_average_cpu {
|
|
|
|
if i == 0 {
|
|
|
|
// Skip, we want to render the average cpu last!
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
avg_cpu_exist_offset = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 00:20:54 +00:00
|
|
|
cpu_entries_vec.push((
|
|
|
|
Style::default().fg(COLOUR_LIST[(i - avg_cpu_exist_offset) % COLOUR_LIST.len()]),
|
2020-01-08 04:40:53 +00:00
|
|
|
cpu.cpu_data
|
|
|
|
.iter()
|
|
|
|
.map(<(f64, f64)>::from)
|
|
|
|
.collect::<Vec<_>>(),
|
2020-01-05 00:20:54 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
if app_state.show_average_cpu {
|
|
|
|
if let Some(avg_cpu_entry) = cpu_data.first() {
|
|
|
|
cpu_entries_vec.push((
|
|
|
|
Style::default().fg(COLOUR_LIST[(cpu_data.len() - 1) % COLOUR_LIST.len()]),
|
2020-01-08 04:40:53 +00:00
|
|
|
avg_cpu_entry
|
|
|
|
.cpu_data
|
|
|
|
.iter()
|
|
|
|
.map(<(f64, f64)>::from)
|
|
|
|
.collect::<Vec<_>>(),
|
2020-01-05 00:20:54 +00:00
|
|
|
));
|
|
|
|
}
|
2019-12-28 03:39:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-05 00:20:54 +00:00
|
|
|
for cpu_entry in &cpu_entries_vec {
|
2019-12-28 03:39:25 +00:00
|
|
|
dataset_vector.push(
|
|
|
|
Dataset::default()
|
2020-01-08 04:40:53 +00:00
|
|
|
.marker(if app_state.use_dot {
|
|
|
|
Marker::Dot
|
|
|
|
} else {
|
|
|
|
Marker::Braille
|
|
|
|
})
|
2020-01-05 00:20:54 +00:00
|
|
|
.style(cpu_entry.0)
|
|
|
|
.data(&(cpu_entry.1)),
|
2019-12-28 03:39:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Chart::default()
|
|
|
|
.block(
|
|
|
|
Block::default()
|
2019-12-28 06:20:05 +00:00
|
|
|
.title("CPU")
|
2019-12-28 03:39:25 +00:00
|
|
|
.borders(Borders::ALL)
|
|
|
|
.border_style(match app_state.current_application_position {
|
2019-12-28 06:20:05 +00:00
|
|
|
app::ApplicationPosition::Cpu => *CANVAS_HIGHLIGHTED_BORDER_STYLE,
|
2019-12-28 03:39:25 +00:00
|
|
|
_ => *CANVAS_BORDER_STYLE,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.x_axis(x_axis)
|
|
|
|
.y_axis(y_axis)
|
|
|
|
.datasets(&dataset_vector)
|
|
|
|
.render(f, draw_loc);
|
|
|
|
}
|
|
|
|
|
2020-01-08 04:40:53 +00:00
|
|
|
fn draw_cpu_legend<B: backend::Backend>(
|
|
|
|
f: &mut Frame<B>, app_state: &mut app::App, draw_loc: Rect,
|
|
|
|
) {
|
2020-01-05 00:20:54 +00:00
|
|
|
let cpu_data: &[ConvertedCpuData] = &(app_state.canvas_data.cpu_data);
|
2020-01-02 04:39:47 +00:00
|
|
|
|
2020-01-11 21:28:21 +00:00
|
|
|
let num_rows = i64::from(draw_loc.height) - 5;
|
2020-01-01 22:55:15 +00:00
|
|
|
let start_position = get_start_position(
|
|
|
|
num_rows,
|
|
|
|
&(app_state.scroll_direction),
|
|
|
|
&mut app_state.previous_cpu_table_position,
|
2020-01-11 21:28:21 +00:00
|
|
|
app_state.currently_selected_cpu_table_position,
|
2020-01-01 22:55:15 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let sliced_cpu_data = (&cpu_data[start_position as usize..]).to_vec();
|
|
|
|
let mut stringified_cpu_data: Vec<Vec<String>> = Vec::new();
|
|
|
|
|
|
|
|
for cpu in sliced_cpu_data {
|
2020-01-05 00:20:54 +00:00
|
|
|
if let Some(cpu_data) = cpu.cpu_data.last() {
|
2020-01-08 04:40:53 +00:00
|
|
|
stringified_cpu_data.push(vec![
|
|
|
|
cpu.cpu_name.clone(),
|
|
|
|
format!("{:.0}%", cpu_data.usage.round()),
|
|
|
|
]);
|
2020-01-01 22:55:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut cpu_row_counter = 0;
|
|
|
|
|
2020-01-08 04:40:53 +00:00
|
|
|
let cpu_rows = stringified_cpu_data
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(itx, cpu_string_row)| {
|
|
|
|
Row::StyledData(
|
|
|
|
cpu_string_row.iter(),
|
|
|
|
match app_state.current_application_position {
|
|
|
|
app::ApplicationPosition::Cpu => {
|
|
|
|
if cpu_row_counter
|
|
|
|
== app_state.currently_selected_cpu_table_position - start_position
|
|
|
|
{
|
|
|
|
cpu_row_counter = -1;
|
|
|
|
Style::default().fg(Color::Black).bg(Color::Cyan)
|
|
|
|
} else {
|
|
|
|
if cpu_row_counter >= 0 {
|
|
|
|
cpu_row_counter += 1;
|
|
|
|
}
|
|
|
|
Style::default().fg(COLOUR_LIST[itx % COLOUR_LIST.len()])
|
2020-01-03 00:07:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-08 04:40:53 +00:00
|
|
|
_ => Style::default().fg(COLOUR_LIST[itx % COLOUR_LIST.len()]),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
});
|
2019-12-28 03:39:25 +00:00
|
|
|
|
2020-01-11 04:41:16 +00:00
|
|
|
// Calculate widths
|
|
|
|
let width = f64::from(draw_loc.width);
|
|
|
|
let width_ratios = vec![0.5, 0.5];
|
2020-01-11 22:30:04 +00:00
|
|
|
let variable_intrinsic_results =
|
|
|
|
get_variable_intrinsic_widths(width as u16, &width_ratios, &CPU_LEGEND_HEADER_LENS);
|
2020-01-11 04:41:16 +00:00
|
|
|
let intrinsic_widths: Vec<u16> =
|
|
|
|
((variable_intrinsic_results.0)[0..variable_intrinsic_results.1]).to_vec();
|
|
|
|
|
|
|
|
// Draw
|
2020-01-11 22:30:04 +00:00
|
|
|
Table::new(CPU_LEGEND_HEADER.iter(), cpu_rows)
|
2020-01-08 04:40:53 +00:00
|
|
|
.block(Block::default().borders(Borders::ALL).border_style(
|
|
|
|
match app_state.current_application_position {
|
|
|
|
app::ApplicationPosition::Cpu => *CANVAS_HIGHLIGHTED_BORDER_STYLE,
|
|
|
|
_ => *CANVAS_BORDER_STYLE,
|
|
|
|
},
|
|
|
|
))
|
2020-01-12 20:41:27 +00:00
|
|
|
.header_style(Style::default().fg(TABLE_HEADER_COLOUR))
|
2020-01-11 04:41:16 +00:00
|
|
|
.widths(
|
|
|
|
&(intrinsic_widths
|
|
|
|
.into_iter()
|
|
|
|
.map(|calculated_width| Constraint::Length(calculated_width as u16))
|
|
|
|
.collect::<Vec<_>>()),
|
|
|
|
)
|
2019-12-28 06:20:05 +00:00
|
|
|
.render(f, draw_loc);
|
|
|
|
}
|
|
|
|
|
2020-01-15 03:28:11 +00:00
|
|
|
#[allow(dead_code)]
|
2020-01-14 03:06:44 +00:00
|
|
|
fn draw_memory_table<B: backend::Backend>(
|
|
|
|
f: &mut Frame<B>, app_state: &app::App, memory_entry: Vec<String>, swap_entry: Vec<String>,
|
|
|
|
draw_loc: Rect,
|
2020-01-08 04:40:53 +00:00
|
|
|
) {
|
2020-01-14 03:06:44 +00:00
|
|
|
// Calculate widths
|
|
|
|
let width = f64::from(draw_loc.width);
|
2020-01-15 02:24:10 +00:00
|
|
|
let width_ratios = [0.2, 0.4, 0.4];
|
2020-01-14 03:06:44 +00:00
|
|
|
let variable_intrinsic_results =
|
|
|
|
get_variable_intrinsic_widths(width as u16, &width_ratios, &MEM_HEADERS_LENS);
|
|
|
|
let intrinsic_widths: Vec<u16> =
|
|
|
|
((variable_intrinsic_results.0)[0..variable_intrinsic_results.1]).to_vec();
|
|
|
|
|
|
|
|
let mem_rows = vec![memory_entry, swap_entry];
|
|
|
|
let mapped_mem_rows = mem_rows.iter().enumerate().map(|(itx, val)| {
|
|
|
|
Row::StyledData(
|
|
|
|
val.iter(),
|
|
|
|
Style::default().fg(COLOUR_LIST[itx % COLOUR_LIST.len()]),
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
// Draw
|
|
|
|
Table::new(MEM_HEADERS.iter(), mapped_mem_rows)
|
|
|
|
.block(Block::default().borders(Borders::ALL).border_style(
|
|
|
|
match app_state.current_application_position {
|
|
|
|
app::ApplicationPosition::Mem => *CANVAS_HIGHLIGHTED_BORDER_STYLE,
|
|
|
|
_ => *CANVAS_BORDER_STYLE,
|
|
|
|
},
|
|
|
|
))
|
|
|
|
.header_style(Style::default().fg(TABLE_HEADER_COLOUR))
|
|
|
|
.widths(
|
|
|
|
&(intrinsic_widths
|
|
|
|
.into_iter()
|
|
|
|
.map(|calculated_width| Constraint::Length(calculated_width as u16))
|
|
|
|
.collect::<Vec<_>>()),
|
|
|
|
)
|
|
|
|
.render(f, draw_loc);
|
2020-01-07 04:07:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-15 03:28:11 +00:00
|
|
|
fn draw_memory_graph<B: backend::Backend>(f: &mut Frame<B>, app_state: &app::App, draw_loc: Rect) {
|
2020-01-02 04:39:47 +00:00
|
|
|
let mem_data: &[(f64, f64)] = &(app_state.canvas_data.mem_data);
|
|
|
|
let swap_data: &[(f64, f64)] = &(app_state.canvas_data.swap_data);
|
|
|
|
|
2019-12-28 03:39:25 +00:00
|
|
|
let x_axis: Axis<String> = Axis::default()
|
|
|
|
.style(Style::default().fg(GRAPH_COLOUR))
|
2020-01-26 06:51:54 +00:00
|
|
|
.bounds([0.0, constants::TIME_STARTS_FROM as f64]);
|
2019-12-28 03:39:25 +00:00
|
|
|
let y_axis = Axis::default()
|
|
|
|
.style(Style::default().fg(GRAPH_COLOUR))
|
|
|
|
.bounds([-0.5, 100.5]) // Offset as the zero value isn't drawn otherwise...
|
|
|
|
.labels(&["0%", "100%"]);
|
|
|
|
|
|
|
|
let mut mem_canvas_vec: Vec<Dataset> = vec![Dataset::default()
|
2020-01-26 21:44:24 +00:00
|
|
|
.name(&app_state.canvas_data.mem_label)
|
2020-01-08 04:40:53 +00:00
|
|
|
.marker(if app_state.use_dot {
|
|
|
|
Marker::Dot
|
|
|
|
} else {
|
|
|
|
Marker::Braille
|
|
|
|
})
|
2019-12-28 03:39:25 +00:00
|
|
|
.style(Style::default().fg(COLOUR_LIST[0]))
|
|
|
|
.data(&mem_data)];
|
|
|
|
|
|
|
|
if !(&swap_data).is_empty() {
|
2020-01-26 21:44:24 +00:00
|
|
|
mem_canvas_vec.push(
|
|
|
|
Dataset::default()
|
|
|
|
.name(&app_state.canvas_data.swap_label)
|
|
|
|
.marker(if app_state.use_dot {
|
|
|
|
Marker::Dot
|
|
|
|
} else {
|
|
|
|
Marker::Braille
|
|
|
|
})
|
|
|
|
.style(Style::default().fg(COLOUR_LIST[1]))
|
|
|
|
.data(&swap_data),
|
|
|
|
);
|
2019-12-28 03:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Chart::default()
|
|
|
|
.block(
|
|
|
|
Block::default()
|
2019-12-28 06:20:05 +00:00
|
|
|
.title("Memory")
|
2019-12-28 03:39:25 +00:00
|
|
|
.borders(Borders::ALL)
|
|
|
|
.border_style(match app_state.current_application_position {
|
2019-12-28 06:20:05 +00:00
|
|
|
app::ApplicationPosition::Mem => *CANVAS_HIGHLIGHTED_BORDER_STYLE,
|
2019-12-28 03:39:25 +00:00
|
|
|
_ => *CANVAS_BORDER_STYLE,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.x_axis(x_axis)
|
|
|
|
.y_axis(y_axis)
|
|
|
|
.datasets(&mem_canvas_vec)
|
|
|
|
.render(f, draw_loc);
|
|
|
|
}
|
|
|
|
|
2020-01-02 04:39:47 +00:00
|
|
|
fn draw_network_graph<B: backend::Backend>(f: &mut Frame<B>, app_state: &app::App, draw_loc: Rect) {
|
|
|
|
let network_data_rx: &[(f64, f64)] = &(app_state.canvas_data.network_data_rx);
|
|
|
|
let network_data_tx: &[(f64, f64)] = &(app_state.canvas_data.network_data_tx);
|
|
|
|
|
2020-01-08 04:40:53 +00:00
|
|
|
let x_axis: Axis<String> = Axis::default()
|
|
|
|
.style(Style::default().fg(GRAPH_COLOUR))
|
2020-01-25 21:36:14 +00:00
|
|
|
.bounds([0.0, 60_000.0]);
|
2019-12-28 03:39:25 +00:00
|
|
|
let y_axis = Axis::default()
|
|
|
|
.style(Style::default().fg(GRAPH_COLOUR))
|
|
|
|
.bounds([-0.5, 30_f64])
|
|
|
|
.labels(&["0B", "1KiB", "1MiB", "1GiB"]);
|
|
|
|
Chart::default()
|
|
|
|
.block(
|
|
|
|
Block::default()
|
|
|
|
.title("Network")
|
|
|
|
.borders(Borders::ALL)
|
|
|
|
.border_style(match app_state.current_application_position {
|
2019-12-28 06:20:05 +00:00
|
|
|
app::ApplicationPosition::Network => *CANVAS_HIGHLIGHTED_BORDER_STYLE,
|
2019-12-28 03:39:25 +00:00
|
|
|
_ => *CANVAS_BORDER_STYLE,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.x_axis(x_axis)
|
|
|
|
.y_axis(y_axis)
|
|
|
|
.datasets(&[
|
|
|
|
Dataset::default()
|
2020-01-08 04:40:53 +00:00
|
|
|
.marker(if app_state.use_dot {
|
|
|
|
Marker::Dot
|
|
|
|
} else {
|
|
|
|
Marker::Braille
|
|
|
|
})
|
2019-12-28 03:39:25 +00:00
|
|
|
.style(Style::default().fg(COLOUR_LIST[0]))
|
|
|
|
.data(&network_data_rx),
|
|
|
|
Dataset::default()
|
2020-01-08 04:40:53 +00:00
|
|
|
.marker(if app_state.use_dot {
|
|
|
|
Marker::Dot
|
|
|
|
} else {
|
|
|
|
Marker::Braille
|
|
|
|
})
|
2019-12-28 03:39:25 +00:00
|
|
|
.style(Style::default().fg(COLOUR_LIST[1]))
|
|
|
|
.data(&network_data_tx),
|
|
|
|
])
|
|
|
|
.render(f, draw_loc);
|
|
|
|
}
|
|
|
|
|
2020-01-08 04:40:53 +00:00
|
|
|
fn draw_network_labels<B: backend::Backend>(
|
|
|
|
f: &mut Frame<B>, app_state: &mut app::App, draw_loc: Rect,
|
|
|
|
) {
|
2020-01-02 04:39:47 +00:00
|
|
|
let rx_display: String = app_state.canvas_data.rx_display.clone();
|
|
|
|
let tx_display: String = app_state.canvas_data.tx_display.clone();
|
|
|
|
let total_rx_display: String = app_state.canvas_data.total_rx_display.clone();
|
|
|
|
let total_tx_display: String = app_state.canvas_data.total_tx_display.clone();
|
|
|
|
|
2019-12-28 06:20:05 +00:00
|
|
|
// Gross but I need it to work...
|
2020-01-02 23:34:09 +00:00
|
|
|
let total_network = if cfg!(not(target_os = "windows")) {
|
2020-01-08 04:40:53 +00:00
|
|
|
vec![vec![
|
|
|
|
rx_display,
|
|
|
|
tx_display,
|
|
|
|
total_rx_display,
|
|
|
|
total_tx_display,
|
|
|
|
]]
|
2020-01-02 23:34:09 +00:00
|
|
|
} else {
|
|
|
|
vec![vec![rx_display, tx_display]]
|
|
|
|
};
|
2019-12-28 06:20:05 +00:00
|
|
|
let mapped_network = total_network.iter().map(|val| Row::Data(val.iter()));
|
|
|
|
|
2020-01-11 04:41:16 +00:00
|
|
|
// Calculate widths
|
2020-01-11 22:30:04 +00:00
|
|
|
let width_ratios: Vec<f64>;
|
|
|
|
let lens: &Vec<usize>;
|
2020-01-11 04:41:16 +00:00
|
|
|
let width = f64::from(draw_loc.width);
|
2020-01-11 22:30:04 +00:00
|
|
|
|
|
|
|
if cfg!(not(target_os = "windows")) {
|
|
|
|
width_ratios = vec![0.25, 0.25, 0.25, 0.25];
|
|
|
|
lens = &NON_WINDOWS_NETWORK_HEADERS_LENS;
|
2020-01-11 04:41:16 +00:00
|
|
|
} else {
|
2020-01-11 22:30:04 +00:00
|
|
|
width_ratios = vec![0.25, 0.25];
|
|
|
|
lens = &WINDOWS_NETWORK_HEADERS_LENS;
|
|
|
|
}
|
|
|
|
let variable_intrinsic_results =
|
|
|
|
get_variable_intrinsic_widths(width as u16, &width_ratios, lens);
|
2020-01-11 04:41:16 +00:00
|
|
|
let intrinsic_widths: Vec<u16> =
|
|
|
|
((variable_intrinsic_results.0)[0..variable_intrinsic_results.1]).to_vec();
|
|
|
|
|
|
|
|
// Draw
|
2020-01-02 23:34:09 +00:00
|
|
|
Table::new(
|
2020-01-11 22:30:04 +00:00
|
|
|
// TODO: [OPT] Feels like I can optimize this and avoid multiple to_vec calls?
|
2020-01-02 23:34:09 +00:00
|
|
|
if cfg!(not(target_os = "windows")) {
|
2020-01-11 22:30:04 +00:00
|
|
|
NON_WINDOWS_NETWORK_HEADERS.to_vec()
|
2020-01-02 23:34:09 +00:00
|
|
|
} else {
|
2020-01-11 22:30:04 +00:00
|
|
|
WINDOWS_NETWORK_HEADERS.to_vec()
|
2020-01-02 23:34:09 +00:00
|
|
|
}
|
|
|
|
.iter(),
|
|
|
|
mapped_network,
|
|
|
|
)
|
2020-01-08 04:40:53 +00:00
|
|
|
.block(Block::default().borders(Borders::ALL).border_style(
|
|
|
|
match app_state.current_application_position {
|
|
|
|
app::ApplicationPosition::Network => *CANVAS_HIGHLIGHTED_BORDER_STYLE,
|
|
|
|
_ => *CANVAS_BORDER_STYLE,
|
|
|
|
},
|
|
|
|
))
|
2020-01-12 20:41:27 +00:00
|
|
|
.header_style(Style::default().fg(TABLE_HEADER_COLOUR))
|
2020-01-11 04:41:16 +00:00
|
|
|
.widths(
|
|
|
|
&(intrinsic_widths
|
|
|
|
.into_iter()
|
|
|
|
.map(|calculated_width| Constraint::Length(calculated_width as u16))
|
|
|
|
.collect::<Vec<_>>()),
|
|
|
|
)
|
2020-01-02 23:34:09 +00:00
|
|
|
.render(f, draw_loc);
|
2019-12-28 06:20:05 +00:00
|
|
|
}
|
2019-12-28 03:39:25 +00:00
|
|
|
|
2020-01-08 04:40:53 +00:00
|
|
|
fn draw_temp_table<B: backend::Backend>(
|
|
|
|
f: &mut Frame<B>, app_state: &mut app::App, draw_loc: Rect,
|
|
|
|
) {
|
2020-01-02 04:39:47 +00:00
|
|
|
let temp_sensor_data: &[Vec<String>] = &(app_state.canvas_data.temp_sensor_data);
|
|
|
|
|
2020-01-11 21:28:21 +00:00
|
|
|
let num_rows = i64::from(draw_loc.height) - 5;
|
2019-12-28 03:39:25 +00:00
|
|
|
let start_position = get_start_position(
|
|
|
|
num_rows,
|
|
|
|
&(app_state.scroll_direction),
|
|
|
|
&mut app_state.previous_temp_position,
|
2020-01-11 21:28:21 +00:00
|
|
|
app_state.currently_selected_temperature_position,
|
2019-12-28 03:39:25 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let sliced_vec: Vec<Vec<String>> = (&temp_sensor_data[start_position as usize..]).to_vec();
|
2020-01-01 22:55:15 +00:00
|
|
|
let mut temp_row_counter = 0;
|
2019-12-28 03:39:25 +00:00
|
|
|
|
2020-01-01 22:55:15 +00:00
|
|
|
let temperature_rows = sliced_vec.iter().map(|temp_row| {
|
2019-12-28 03:39:25 +00:00
|
|
|
Row::StyledData(
|
2020-01-01 22:55:15 +00:00
|
|
|
temp_row.iter(),
|
2020-01-03 00:07:53 +00:00
|
|
|
match app_state.current_application_position {
|
|
|
|
app::ApplicationPosition::Temp => {
|
2020-01-08 04:40:53 +00:00
|
|
|
if temp_row_counter
|
|
|
|
== app_state.currently_selected_temperature_position - start_position
|
|
|
|
{
|
2020-01-03 00:07:53 +00:00
|
|
|
temp_row_counter = -1;
|
|
|
|
Style::default().fg(Color::Black).bg(Color::Cyan)
|
|
|
|
} else {
|
|
|
|
if temp_row_counter >= 0 {
|
|
|
|
temp_row_counter += 1;
|
|
|
|
}
|
|
|
|
Style::default().fg(TEXT_COLOUR)
|
|
|
|
}
|
2019-12-28 03:39:25 +00:00
|
|
|
}
|
2020-01-03 00:07:53 +00:00
|
|
|
_ => Style::default().fg(TEXT_COLOUR),
|
2019-12-28 03:39:25 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
});
|
2020-01-11 04:41:16 +00:00
|
|
|
|
|
|
|
// Calculate widths
|
|
|
|
let width = f64::from(draw_loc.width);
|
|
|
|
let width_ratios = [0.5, 0.5];
|
2020-01-11 22:30:04 +00:00
|
|
|
let variable_intrinsic_results =
|
|
|
|
get_variable_intrinsic_widths(width as u16, &width_ratios, &TEMP_HEADERS_LENS);
|
2020-01-11 04:41:16 +00:00
|
|
|
let intrinsic_widths: Vec<u16> =
|
|
|
|
((variable_intrinsic_results.0)[0..variable_intrinsic_results.1]).to_vec();
|
|
|
|
|
|
|
|
// Draw
|
2020-01-11 22:30:04 +00:00
|
|
|
Table::new(TEMP_HEADERS.iter(), temperature_rows)
|
2019-12-28 03:39:25 +00:00
|
|
|
.block(
|
|
|
|
Block::default()
|
|
|
|
.title("Temperatures")
|
|
|
|
.borders(Borders::ALL)
|
|
|
|
.border_style(match app_state.current_application_position {
|
2019-12-28 06:20:05 +00:00
|
|
|
app::ApplicationPosition::Temp => *CANVAS_HIGHLIGHTED_BORDER_STYLE,
|
2019-12-28 03:39:25 +00:00
|
|
|
_ => *CANVAS_BORDER_STYLE,
|
|
|
|
}),
|
|
|
|
)
|
2020-01-12 20:41:27 +00:00
|
|
|
.header_style(Style::default().fg(TABLE_HEADER_COLOUR))
|
2020-01-11 04:41:16 +00:00
|
|
|
.widths(
|
|
|
|
&(intrinsic_widths
|
|
|
|
.into_iter()
|
|
|
|
.map(|calculated_width| Constraint::Length(calculated_width as u16))
|
|
|
|
.collect::<Vec<_>>()),
|
|
|
|
)
|
2019-12-28 03:39:25 +00:00
|
|
|
.render(f, draw_loc);
|
|
|
|
}
|
|
|
|
|
2020-01-08 04:40:53 +00:00
|
|
|
fn draw_disk_table<B: backend::Backend>(
|
|
|
|
f: &mut Frame<B>, app_state: &mut app::App, draw_loc: Rect,
|
|
|
|
) {
|
2020-01-02 04:39:47 +00:00
|
|
|
let disk_data: &[Vec<String>] = &(app_state.canvas_data.disk_data);
|
2020-01-11 21:28:21 +00:00
|
|
|
let num_rows = i64::from(draw_loc.height) - 5;
|
2019-12-28 03:39:25 +00:00
|
|
|
let start_position = get_start_position(
|
|
|
|
num_rows,
|
|
|
|
&(app_state.scroll_direction),
|
|
|
|
&mut app_state.previous_disk_position,
|
2020-01-11 21:28:21 +00:00
|
|
|
app_state.currently_selected_disk_position,
|
2019-12-28 03:39:25 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let sliced_vec: Vec<Vec<String>> = (&disk_data[start_position as usize..]).to_vec();
|
|
|
|
let mut disk_counter = 0;
|
|
|
|
|
|
|
|
let disk_rows = sliced_vec.iter().map(|disk| {
|
|
|
|
Row::StyledData(
|
|
|
|
disk.iter(),
|
2020-01-03 00:07:53 +00:00
|
|
|
match app_state.current_application_position {
|
|
|
|
app::ApplicationPosition::Disk => {
|
|
|
|
if disk_counter == app_state.currently_selected_disk_position - start_position {
|
|
|
|
disk_counter = -1;
|
|
|
|
Style::default().fg(Color::Black).bg(Color::Cyan)
|
|
|
|
} else {
|
|
|
|
if disk_counter >= 0 {
|
|
|
|
disk_counter += 1;
|
|
|
|
}
|
|
|
|
Style::default().fg(TEXT_COLOUR)
|
|
|
|
}
|
2019-12-28 03:39:25 +00:00
|
|
|
}
|
2020-01-03 00:07:53 +00:00
|
|
|
_ => Style::default().fg(TEXT_COLOUR),
|
2019-12-28 03:39:25 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
2020-01-11 04:41:16 +00:00
|
|
|
// Calculate widths
|
2020-01-11 22:30:04 +00:00
|
|
|
// TODO: Ellipsis on strings?
|
2020-01-11 00:00:55 +00:00
|
|
|
let width = f64::from(draw_loc.width);
|
2020-01-11 04:55:21 +00:00
|
|
|
let width_ratios = [0.2, 0.15, 0.13, 0.13, 0.13, 0.13, 0.13];
|
2020-01-11 22:30:04 +00:00
|
|
|
let variable_intrinsic_results =
|
|
|
|
get_variable_intrinsic_widths(width as u16, &width_ratios, &DISK_HEADERS_LENS);
|
2020-01-11 04:41:16 +00:00
|
|
|
let intrinsic_widths: Vec<u16> =
|
|
|
|
((variable_intrinsic_results.0)[0..variable_intrinsic_results.1]).to_vec();
|
|
|
|
|
|
|
|
// Draw!
|
2020-01-11 22:30:04 +00:00
|
|
|
Table::new(DISK_HEADERS.iter(), disk_rows)
|
2020-01-11 04:41:16 +00:00
|
|
|
.block(
|
|
|
|
Block::default()
|
|
|
|
.title("Disk")
|
|
|
|
.borders(Borders::ALL)
|
|
|
|
.border_style(match app_state.current_application_position {
|
|
|
|
app::ApplicationPosition::Disk => *CANVAS_HIGHLIGHTED_BORDER_STYLE,
|
|
|
|
_ => *CANVAS_BORDER_STYLE,
|
|
|
|
}),
|
|
|
|
)
|
2020-01-12 20:41:27 +00:00
|
|
|
.header_style(Style::default().fg(TABLE_HEADER_COLOUR))
|
2020-01-11 04:41:16 +00:00
|
|
|
.widths(
|
|
|
|
&(intrinsic_widths
|
|
|
|
.into_iter()
|
|
|
|
.map(|calculated_width| Constraint::Length(calculated_width as u16))
|
|
|
|
.collect::<Vec<_>>()),
|
|
|
|
)
|
|
|
|
.render(f, draw_loc);
|
2019-12-28 03:39:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-12 19:25:47 +00:00
|
|
|
fn draw_search_field<B: backend::Backend>(
|
|
|
|
f: &mut Frame<B>, app_state: &mut app::App, draw_loc: Rect,
|
|
|
|
) {
|
2020-01-17 02:33:06 +00:00
|
|
|
let width = draw_loc.width - 18; // TODO [SEARCH] this is hard-coded... ew
|
2020-01-12 23:45:11 +00:00
|
|
|
let query = app_state.get_current_search_query();
|
|
|
|
let shrunk_query = if query.len() < width as usize {
|
|
|
|
query
|
|
|
|
} else {
|
|
|
|
&query[(query.len() - width as usize)..]
|
|
|
|
};
|
|
|
|
|
2020-01-17 02:33:06 +00:00
|
|
|
// TODO: [SEARCH] Consider making this look prettier
|
|
|
|
let cursor_position = app_state.get_cursor_position();
|
|
|
|
|
2020-01-18 19:57:54 +00:00
|
|
|
// TODO: [SEARCH] This can be optimized... if the cursor is at the very end or not focused we can skip this
|
2020-01-17 02:33:06 +00:00
|
|
|
let mut query_with_cursor: Vec<Text> = shrunk_query
|
|
|
|
.chars()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(itx, c)| {
|
2020-01-18 19:57:54 +00:00
|
|
|
if let app::ApplicationPosition::ProcessSearch = app_state.current_application_position
|
|
|
|
{
|
|
|
|
if itx == cursor_position {
|
|
|
|
return Text::styled(
|
|
|
|
c.to_string(),
|
|
|
|
Style::default().fg(TEXT_COLOUR).bg(TABLE_HEADER_COLOUR),
|
|
|
|
);
|
|
|
|
}
|
2020-01-17 02:33:06 +00:00
|
|
|
}
|
2020-01-18 19:57:54 +00:00
|
|
|
Text::styled(c.to_string(), Style::default().fg(TEXT_COLOUR))
|
2020-01-17 02:33:06 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2020-01-18 19:57:54 +00:00
|
|
|
if let app::ApplicationPosition::ProcessSearch = app_state.current_application_position {
|
|
|
|
if cursor_position >= query.len() {
|
|
|
|
query_with_cursor.push(Text::styled(
|
|
|
|
" ".to_string(),
|
|
|
|
Style::default().fg(TEXT_COLOUR).bg(TABLE_HEADER_COLOUR),
|
|
|
|
))
|
|
|
|
}
|
2020-01-17 02:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut search_text = vec![
|
2020-01-12 20:41:27 +00:00
|
|
|
if app_state.is_searching_with_pid() {
|
2020-01-16 04:03:23 +00:00
|
|
|
Text::styled("\nPID", Style::default().fg(TABLE_HEADER_COLOUR))
|
2020-01-12 20:41:27 +00:00
|
|
|
} else {
|
2020-01-16 04:03:23 +00:00
|
|
|
Text::styled("\nName", Style::default().fg(TABLE_HEADER_COLOUR))
|
|
|
|
},
|
|
|
|
if app_state.use_simple {
|
|
|
|
Text::styled(" (Simple): ", Style::default().fg(TABLE_HEADER_COLOUR))
|
|
|
|
} else {
|
|
|
|
Text::styled(" (Regex): ", Style::default().fg(TABLE_HEADER_COLOUR))
|
2020-01-12 20:41:27 +00:00
|
|
|
},
|
|
|
|
];
|
2020-01-17 02:33:06 +00:00
|
|
|
|
|
|
|
search_text.extend(query_with_cursor);
|
|
|
|
|
2020-01-16 04:03:23 +00:00
|
|
|
// TODO: [SEARCH] Gotta make this easier to understand... it's pretty ugly cramming controls like this
|
2020-01-12 20:41:27 +00:00
|
|
|
Paragraph::new(search_text.iter())
|
|
|
|
.block(
|
|
|
|
Block::default()
|
2020-01-16 04:03:23 +00:00
|
|
|
.title("Search (Esc or Ctrl-f to close)")
|
2020-01-12 20:41:27 +00:00
|
|
|
.borders(Borders::ALL)
|
2020-01-12 23:45:11 +00:00
|
|
|
.border_style(if app_state.get_current_regex_matcher().is_err() {
|
|
|
|
Style::default().fg(Color::Red)
|
|
|
|
} else {
|
|
|
|
match app_state.current_application_position {
|
|
|
|
app::ApplicationPosition::ProcessSearch => *CANVAS_HIGHLIGHTED_BORDER_STYLE,
|
|
|
|
_ => *CANVAS_BORDER_STYLE,
|
|
|
|
}
|
2020-01-12 20:41:27 +00:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
.style(Style::default().fg(Color::Gray))
|
|
|
|
.alignment(Alignment::Left)
|
2020-01-12 23:45:11 +00:00
|
|
|
.wrap(false)
|
2020-01-12 20:41:27 +00:00
|
|
|
.render(f, draw_loc);
|
2020-01-12 19:25:47 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 04:40:53 +00:00
|
|
|
fn draw_processes_table<B: backend::Backend>(
|
|
|
|
f: &mut Frame<B>, app_state: &mut app::App, draw_loc: Rect,
|
|
|
|
) {
|
2020-01-29 02:24:52 +00:00
|
|
|
let process_data: &[ConvertedProcessHarvest] = if app_state.is_grouped() {
|
2020-01-08 05:28:11 +00:00
|
|
|
&app_state.canvas_data.grouped_process_data
|
|
|
|
} else {
|
|
|
|
&app_state.canvas_data.process_data
|
|
|
|
};
|
2019-12-28 03:39:25 +00:00
|
|
|
|
|
|
|
// Admittedly this is kinda a hack... but we need to:
|
|
|
|
// * Scroll
|
|
|
|
// * Show/hide elements based on scroll position
|
|
|
|
// As such, we use a process_counter to know when we've hit the process we've currently scrolled to. We also need to move the list - we can
|
|
|
|
// do so by hiding some elements!
|
2020-01-11 21:28:21 +00:00
|
|
|
let num_rows = i64::from(draw_loc.height) - 5;
|
2019-12-28 03:39:25 +00:00
|
|
|
|
|
|
|
let start_position = get_start_position(
|
|
|
|
num_rows,
|
|
|
|
&(app_state.scroll_direction),
|
|
|
|
&mut app_state.previous_process_position,
|
2020-01-11 21:28:21 +00:00
|
|
|
app_state.currently_selected_process_position,
|
2019-12-28 03:39:25 +00:00
|
|
|
);
|
|
|
|
|
2020-01-29 03:38:36 +00:00
|
|
|
let sliced_vec: Vec<ConvertedProcessHarvest> =
|
|
|
|
(&process_data[start_position as usize..]).to_vec();
|
2019-12-28 03:39:25 +00:00
|
|
|
let mut process_counter = 0;
|
|
|
|
|
2020-01-11 04:41:16 +00:00
|
|
|
// Draw!
|
2019-12-28 03:39:25 +00:00
|
|
|
let process_rows = sliced_vec.iter().map(|process| {
|
2020-01-02 04:39:47 +00:00
|
|
|
let stringified_process_vec: Vec<String> = vec![
|
2020-01-08 04:39:52 +00:00
|
|
|
if app_state.is_grouped() {
|
2020-01-09 03:36:36 +00:00
|
|
|
process.group.len().to_string()
|
2020-01-08 04:39:52 +00:00
|
|
|
} else {
|
|
|
|
process.pid.to_string()
|
|
|
|
},
|
2020-01-02 04:39:47 +00:00
|
|
|
process.name.clone(),
|
|
|
|
process.cpu_usage.clone(),
|
|
|
|
process.mem_usage.clone(),
|
|
|
|
];
|
2019-12-28 03:39:25 +00:00
|
|
|
Row::StyledData(
|
2020-01-02 04:39:47 +00:00
|
|
|
stringified_process_vec.into_iter(),
|
2020-01-03 00:07:53 +00:00
|
|
|
match app_state.current_application_position {
|
|
|
|
app::ApplicationPosition::Process => {
|
2020-01-08 04:40:53 +00:00
|
|
|
if process_counter
|
|
|
|
== app_state.currently_selected_process_position - start_position
|
|
|
|
{
|
2020-01-03 00:07:53 +00:00
|
|
|
process_counter = -1;
|
|
|
|
Style::default().fg(Color::Black).bg(Color::Cyan)
|
|
|
|
} else {
|
|
|
|
if process_counter >= 0 {
|
|
|
|
process_counter += 1;
|
|
|
|
}
|
|
|
|
Style::default().fg(TEXT_COLOUR)
|
|
|
|
}
|
2019-12-28 03:39:25 +00:00
|
|
|
}
|
2020-01-03 00:07:53 +00:00
|
|
|
_ => Style::default().fg(TEXT_COLOUR),
|
2019-12-28 03:39:25 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
2020-01-25 21:36:14 +00:00
|
|
|
use app::data_harvester::processes::ProcessSorting;
|
2020-01-11 22:30:04 +00:00
|
|
|
let mut pid_or_name = if app_state.is_grouped() {
|
|
|
|
"Count"
|
|
|
|
} else {
|
|
|
|
"PID(p)"
|
|
|
|
}
|
|
|
|
.to_string();
|
|
|
|
let mut name = "Name(n)".to_string();
|
|
|
|
let mut cpu = "CPU%(c)".to_string();
|
|
|
|
let mut mem = "Mem%(m)".to_string();
|
2019-12-28 03:39:25 +00:00
|
|
|
|
2020-01-11 22:30:04 +00:00
|
|
|
let direction_val = if app_state.process_sorting_reverse {
|
|
|
|
"⯆".to_string()
|
|
|
|
} else {
|
|
|
|
"⯅".to_string()
|
|
|
|
};
|
2019-12-28 03:39:25 +00:00
|
|
|
|
2020-01-11 22:30:04 +00:00
|
|
|
match app_state.process_sorting_type {
|
|
|
|
ProcessSorting::CPU => cpu += &direction_val,
|
|
|
|
ProcessSorting::MEM => mem += &direction_val,
|
|
|
|
ProcessSorting::PID => pid_or_name += &direction_val,
|
|
|
|
ProcessSorting::NAME => name += &direction_val,
|
|
|
|
};
|
2019-12-28 03:39:25 +00:00
|
|
|
|
2020-01-11 22:30:04 +00:00
|
|
|
// TODO: [OPT] Reuse calculation to save time?
|
|
|
|
let process_headers = [pid_or_name, name, cpu, mem];
|
|
|
|
let process_headers_lens: Vec<usize> = process_headers
|
|
|
|
.iter()
|
|
|
|
.map(|entry| entry.len())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
// Calculate widths
|
|
|
|
let width = f64::from(draw_loc.width);
|
|
|
|
let width_ratios = [0.2, 0.4, 0.2, 0.2];
|
|
|
|
let variable_intrinsic_results =
|
|
|
|
get_variable_intrinsic_widths(width as u16, &width_ratios, &process_headers_lens);
|
|
|
|
let intrinsic_widths: Vec<u16> =
|
|
|
|
((variable_intrinsic_results.0)[0..variable_intrinsic_results.1]).to_vec();
|
|
|
|
|
|
|
|
Table::new(process_headers.iter(), process_rows)
|
|
|
|
.block(
|
|
|
|
Block::default()
|
|
|
|
.title("Processes")
|
|
|
|
.borders(Borders::ALL)
|
|
|
|
.border_style(match app_state.current_application_position {
|
|
|
|
app::ApplicationPosition::Process => *CANVAS_HIGHLIGHTED_BORDER_STYLE,
|
|
|
|
_ => *CANVAS_BORDER_STYLE,
|
|
|
|
}),
|
|
|
|
)
|
2020-01-12 20:41:27 +00:00
|
|
|
.header_style(Style::default().fg(TABLE_HEADER_COLOUR))
|
2020-01-11 22:30:04 +00:00
|
|
|
.widths(
|
|
|
|
&(intrinsic_widths
|
|
|
|
.into_iter()
|
|
|
|
.map(|calculated_width| Constraint::Length(calculated_width as u16))
|
|
|
|
.collect::<Vec<_>>()),
|
|
|
|
)
|
|
|
|
.render(f, draw_loc);
|
2019-12-28 03:39:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-11 04:41:16 +00:00
|
|
|
/// A somewhat jury-rigged solution to simulate a variable intrinsic layout for
|
|
|
|
/// table widths. Note that this will do one main pass to try to properly
|
|
|
|
/// allocate widths. This will thus potentially cut off latter elements
|
|
|
|
/// (return size of 0) if it is too small (threshold), but will try its best.
|
|
|
|
///
|
2020-01-11 22:30:04 +00:00
|
|
|
/// `width thresholds` and `desired_widths_ratio` should be the same length.
|
|
|
|
/// Otherwise bad things happen.
|
2020-01-11 04:41:16 +00:00
|
|
|
fn get_variable_intrinsic_widths(
|
2020-01-11 22:30:04 +00:00
|
|
|
total_width: u16, desired_widths_ratio: &[f64], width_thresholds: &[usize],
|
2020-01-11 04:41:16 +00:00
|
|
|
) -> (Vec<u16>, usize) {
|
|
|
|
let num_widths = desired_widths_ratio.len();
|
|
|
|
let mut resulting_widths: Vec<u16> = vec![0; num_widths];
|
|
|
|
let mut last_index = 0;
|
|
|
|
|
|
|
|
let mut remaining_width = (total_width - (num_widths as u16 - 1)) as i32; // Required for spaces...
|
|
|
|
let desired_widths = desired_widths_ratio
|
|
|
|
.iter()
|
|
|
|
.map(|&desired_width_ratio| (desired_width_ratio * total_width as f64) as i32)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
for (itx, desired_width) in desired_widths.into_iter().enumerate() {
|
2020-01-11 22:30:04 +00:00
|
|
|
resulting_widths[itx] = if desired_width < width_thresholds[itx] as i32 {
|
2020-01-11 04:41:16 +00:00
|
|
|
// Try to take threshold, else, 0
|
2020-01-11 22:30:04 +00:00
|
|
|
if remaining_width < width_thresholds[itx] as i32 {
|
2020-01-11 04:41:16 +00:00
|
|
|
0
|
|
|
|
} else {
|
2020-01-11 22:30:04 +00:00
|
|
|
remaining_width -= width_thresholds[itx] as i32;
|
|
|
|
width_thresholds[itx] as u16
|
2020-01-11 04:41:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Take as large as possible
|
2020-01-11 04:55:21 +00:00
|
|
|
if remaining_width < desired_width {
|
2020-01-11 04:41:16 +00:00
|
|
|
// Check the biggest chunk possible
|
2020-01-11 22:30:04 +00:00
|
|
|
if remaining_width < width_thresholds[itx] as i32 {
|
2020-01-11 04:41:16 +00:00
|
|
|
0
|
|
|
|
} else {
|
|
|
|
let temp_width = remaining_width;
|
|
|
|
remaining_width = 0;
|
|
|
|
temp_width as u16
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
remaining_width -= desired_width;
|
|
|
|
desired_width as u16
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if resulting_widths[itx] == 0 {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
last_index += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-11 22:30:04 +00:00
|
|
|
// Simple redistribution tactic - if there's any space left, split it evenly amongst all members
|
|
|
|
if last_index < num_widths {
|
|
|
|
let for_all_widths = (remaining_width / last_index as i32) as u16;
|
|
|
|
let mut remainder = remaining_width % last_index as i32;
|
|
|
|
|
|
|
|
for resulting_width in &mut resulting_widths {
|
|
|
|
*resulting_width += for_all_widths;
|
|
|
|
if remainder > 0 {
|
|
|
|
*resulting_width += 1;
|
|
|
|
remainder -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-11 04:41:16 +00:00
|
|
|
|
|
|
|
(resulting_widths, last_index)
|
|
|
|
}
|
|
|
|
|
2019-09-25 20:43:13 +00:00
|
|
|
fn get_start_position(
|
2020-01-11 21:28:21 +00:00
|
|
|
num_rows: i64, scroll_direction: &app::ScrollDirection, previously_scrolled_position: &mut i64,
|
|
|
|
currently_selected_position: i64,
|
2019-09-25 20:43:13 +00:00
|
|
|
) -> i64 {
|
|
|
|
match scroll_direction {
|
|
|
|
app::ScrollDirection::DOWN => {
|
2020-01-11 21:28:21 +00:00
|
|
|
if currently_selected_position < *previously_scrolled_position + num_rows {
|
2020-01-11 22:30:04 +00:00
|
|
|
// If, using previous_scrolled_position, we can see the element
|
|
|
|
// (so within that and + num_rows) just reuse the current previously scrolled position
|
2020-01-11 21:28:21 +00:00
|
|
|
*previously_scrolled_position
|
|
|
|
} else if currently_selected_position >= num_rows {
|
2020-01-11 22:30:04 +00:00
|
|
|
// Else if the current position past the last element visible in the list, omit
|
|
|
|
// until we can see that element
|
2020-01-11 21:28:21 +00:00
|
|
|
*previously_scrolled_position = currently_selected_position - num_rows;
|
|
|
|
currently_selected_position - num_rows
|
2019-12-06 05:57:04 +00:00
|
|
|
} else {
|
2020-01-11 22:30:04 +00:00
|
|
|
// Else, if it is not past the last element visible, do not omit anything
|
2020-01-11 21:28:21 +00:00
|
|
|
0
|
2019-09-25 20:43:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
app::ScrollDirection::UP => {
|
2020-01-11 21:28:21 +00:00
|
|
|
if currently_selected_position <= *previously_scrolled_position {
|
2020-01-11 22:30:04 +00:00
|
|
|
// If it's past the first element, then show from that element downwards
|
2020-01-11 21:28:21 +00:00
|
|
|
*previously_scrolled_position = currently_selected_position;
|
|
|
|
currently_selected_position
|
2019-12-06 05:57:04 +00:00
|
|
|
} else {
|
2020-01-11 22:30:04 +00:00
|
|
|
// Else, don't change what our start position is from whatever it is set to!
|
2020-01-11 21:28:21 +00:00
|
|
|
*previously_scrolled_position
|
2019-09-25 20:43:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|