refactor: heavily simplify the old network legend

This commit is contained in:
ClementTsang 2022-05-04 05:13:52 -04:00
parent c97126df22
commit 69ec526dc6
2 changed files with 16 additions and 56 deletions

View file

@ -333,7 +333,6 @@ mod test {
#[test] #[test]
fn test_width_calculation() { fn test_width_calculation() {
// TODO: Implement width calculation test; can reuse old ones as basis // TODO: Implement width calculation test; can reuse old ones as basis
todo!()
} }
#[test] #[test]

View file

@ -1,14 +1,10 @@
use once_cell::sync::Lazy;
use std::cmp::max;
use crate::{ use crate::{
app::{App, AxisScaling}, app::{App, AxisScaling},
canvas::{ canvas::{
components::{GraphData, TimeGraph}, components::{GraphData, TimeGraph},
drawing_utils::{get_column_widths, should_hide_x_label}, drawing_utils::should_hide_x_label,
Painter, Point, Painter, Point,
}, },
constants::*,
units::data_units::DataUnit, units::data_units::DataUnit,
utils::gen_util::*, utils::gen_util::*,
}; };
@ -21,26 +17,18 @@ use tui::{
widgets::{Block, Borders, Row, Table}, widgets::{Block, Borders, Row, Table},
}; };
const NETWORK_HEADERS: [&str; 4] = ["RX", "TX", "Total RX", "Total TX"];
static NETWORK_HEADERS_LENS: Lazy<Vec<u16>> = Lazy::new(|| {
NETWORK_HEADERS
.iter()
.map(|entry| entry.len() as u16)
.collect::<Vec<_>>()
});
impl Painter { impl Painter {
pub fn draw_network<B: Backend>( pub fn draw_network<B: Backend>(
&self, f: &mut Frame<'_, B>, app_state: &mut App, draw_loc: Rect, widget_id: u64, &self, f: &mut Frame<'_, B>, app_state: &mut App, draw_loc: Rect, widget_id: u64,
) { ) {
if app_state.app_config_fields.use_old_network_legend { if app_state.app_config_fields.use_old_network_legend {
const LEGEND_HEIGHT: u16 = 4;
let network_chunk = Layout::default() let network_chunk = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.margin(0) .margin(0)
.constraints([ .constraints([
Constraint::Length(max(draw_loc.height as i64 - 5, 0) as u16), Constraint::Length(draw_loc.height.saturating_sub(LEGEND_HEIGHT)),
Constraint::Length(5), Constraint::Length(LEGEND_HEIGHT),
]) ])
.split(draw_loc); .split(draw_loc);
@ -174,11 +162,7 @@ impl Painter {
fn draw_network_labels<B: Backend>( fn draw_network_labels<B: Backend>(
&self, f: &mut Frame<'_, B>, app_state: &mut App, draw_loc: Rect, widget_id: u64, &self, f: &mut Frame<'_, B>, app_state: &mut App, draw_loc: Rect, widget_id: u64,
) { ) {
let table_gap = if draw_loc.height < TABLE_GAP_HEIGHT_LIMIT { const NETWORK_HEADERS: [&str; 4] = ["RX", "TX", "Total RX", "Total TX"];
0
} else {
app_state.app_config_fields.table_gap
};
let rx_display = &app_state.canvas_data.rx_display; let rx_display = &app_state.canvas_data.rx_display;
let tx_display = &app_state.canvas_data.tx_display; let tx_display = &app_state.canvas_data.tx_display;
@ -186,40 +170,17 @@ impl Painter {
let total_tx_display = &app_state.canvas_data.total_tx_display; let total_tx_display = &app_state.canvas_data.total_tx_display;
// Gross but I need it to work... // Gross but I need it to work...
let total_network = vec![vec![ let total_network = vec![Row::new(vec![
Text::raw(rx_display), Text::styled(rx_display, self.colours.rx_style),
Text::raw(tx_display), Text::styled(tx_display, self.colours.tx_style),
Text::raw(total_rx_display), Text::styled(total_rx_display, self.colours.total_rx_style),
Text::raw(total_tx_display), Text::styled(total_tx_display, self.colours.total_tx_style),
]]; ])];
let mapped_network = total_network
.into_iter()
.map(|val| Row::new(val).style(self.colours.text_style));
// Calculate widths
let intrinsic_widths = get_column_widths(
draw_loc.width,
&[None, None, None, None],
&(NETWORK_HEADERS_LENS
.iter()
.map(|s| Some(*s))
.collect::<Vec<_>>()),
&[Some(0.25); 4],
&(NETWORK_HEADERS_LENS
.iter()
.map(|s| Some(*s))
.collect::<Vec<_>>()),
true,
);
// Draw // Draw
f.render_widget( f.render_widget(
Table::new(mapped_network) Table::new(total_network)
.header( .header(Row::new(NETWORK_HEADERS.to_vec()).style(self.colours.table_header_style))
Row::new(NETWORK_HEADERS.to_vec())
.style(self.colours.table_header_style)
.bottom_margin(table_gap),
)
.block(Block::default().borders(Borders::ALL).border_style( .block(Block::default().borders(Borders::ALL).border_style(
if app_state.current_widget.widget_id == widget_id { if app_state.current_widget.widget_id == widget_id {
self.colours.highlighted_border_style self.colours.highlighted_border_style
@ -229,9 +190,9 @@ impl Painter {
)) ))
.style(self.colours.text_style) .style(self.colours.text_style)
.widths( .widths(
&(intrinsic_widths &((std::iter::repeat(draw_loc.width.saturating_sub(2) / 4))
.iter() .take(4)
.map(|calculated_width| Constraint::Length(*calculated_width)) .map(Constraint::Length)
.collect::<Vec<_>>()), .collect::<Vec<_>>()),
), ),
draw_loc, draw_loc,