refactor: Refactor some canvas code (#342)

This commit is contained in:
Clement Tsang 2020-12-08 22:34:21 -05:00 committed by GitHub
parent f4b8386063
commit 19cdc269fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 81 deletions

View File

@ -1,5 +1,3 @@
use futures::join;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct MemHarvest { pub struct MemHarvest {
pub mem_total_in_mb: u64, pub mem_total_in_mb: u64,
@ -56,6 +54,8 @@ pub async fn get_mem_data(
crate::utils::error::Result<Option<MemHarvest>>, crate::utils::error::Result<Option<MemHarvest>>,
crate::utils::error::Result<Option<MemHarvest>>, crate::utils::error::Result<Option<MemHarvest>>,
) { ) {
use futures::join;
if !actually_get { if !actually_get {
(Ok(None), Ok(None)) (Ok(None), Ok(None))
} else { } else {

View File

@ -95,7 +95,7 @@ pub struct Painter {
height: u16, height: u16,
width: u16, width: u16,
styled_help_text: Vec<Spans<'static>>, styled_help_text: Vec<Spans<'static>>,
is_mac_os: bool, is_mac_os: bool, // FIXME: This feels out of place...
row_constraints: Vec<Constraint>, row_constraints: Vec<Constraint>,
col_constraints: Vec<Vec<Constraint>>, col_constraints: Vec<Vec<Constraint>>,
col_row_constraints: Vec<Vec<Vec<Constraint>>>, col_row_constraints: Vec<Vec<Vec<Constraint>>>,
@ -182,7 +182,7 @@ impl Painter {
height: 0, height: 0,
width: 0, width: 0,
styled_help_text: Vec::default(), styled_help_text: Vec::default(),
is_mac_os: false, is_mac_os: cfg!(target_os = "macos"),
row_constraints, row_constraints,
col_constraints, col_constraints,
col_row_constraints, col_row_constraints,
@ -242,7 +242,6 @@ impl Painter {
/// Must be run once before drawing, but after setting colours. /// Must be run once before drawing, but after setting colours.
/// This is to set some remaining styles and text. /// This is to set some remaining styles and text.
fn complete_painter_init(&mut self) { fn complete_painter_init(&mut self) {
self.is_mac_os = cfg!(target_os = "macos");
let mut styled_help_spans = Vec::new(); let mut styled_help_spans = Vec::new();
// Init help text: // Init help text:
@ -599,61 +598,55 @@ impl Painter {
} }
if self.derived_widget_draw_locs.is_empty() || app_state.is_force_redraw { if self.derived_widget_draw_locs.is_empty() || app_state.is_force_redraw {
let row_draw_locs = Layout::default() let draw_locs = Layout::default()
.margin(0) .margin(0)
.constraints(self.row_constraints.as_ref()) .constraints(self.row_constraints.as_ref())
.direction(Direction::Vertical) .direction(Direction::Vertical)
.split(terminal_size); .split(terminal_size);
let col_draw_locs = self
.col_constraints self.derived_widget_draw_locs = izip!(
.iter() draw_locs,
.zip(&row_draw_locs) &self.col_constraints,
.map(|(col_constraint, row_draw_loc)| { &self.col_row_constraints,
&self.layout_constraints,
&self.widget_layout.rows
)
.map(
|(
draw_loc,
col_constraint,
col_row_constraint,
row_constraint_vec,
cols,
)| {
izip!(
Layout::default() Layout::default()
.constraints(col_constraint.as_ref()) .constraints(col_constraint.as_ref())
.direction(Direction::Horizontal) .direction(Direction::Horizontal)
.split(*row_draw_loc) .split(draw_loc)
}) .into_iter(),
.collect::<Vec<_>>(); col_row_constraint,
let col_row_draw_locs = self row_constraint_vec,
.col_row_constraints &cols.children
.iter()
.zip(&col_draw_locs)
.map(|(col_row_constraints, row_draw_loc)| {
col_row_constraints
.iter()
.zip(row_draw_loc)
.map(|(col_row_constraint, col_draw_loc)| {
Layout::default()
.constraints(col_row_constraint.as_ref())
.direction(Direction::Vertical)
.split(*col_draw_loc)
})
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
// Now... draw!
let mut new_derived_widget_draw_locs = Vec::new();
izip!(
&self.layout_constraints,
col_row_draw_locs,
&self.widget_layout.rows
) )
.for_each(|(row_constraint_vec, row_draw_loc, cols)| { .map(|(split_loc, constraint, col_constraint_vec, col_rows)| {
let mut derived_row_draw_locs = Vec::new(); izip!(
izip!(row_constraint_vec, row_draw_loc, &cols.children).for_each( Layout::default()
|(col_constraint_vec, col_draw_loc, col_rows)| { .constraints(constraint.as_ref())
let mut derived_col_draw_locs = Vec::new(); .direction(Direction::Vertical)
izip!(col_constraint_vec, col_draw_loc, &col_rows.children) .split(split_loc)
.for_each( .into_iter(),
|(col_row_constraint_vec, col_row_draw_loc, widgets)| { col_constraint_vec,
&col_rows.children
)
.map(|(draw_loc, col_row_constraint_vec, widgets)| {
// Note that col_row_constraint_vec CONTAINS the widget constraints // Note that col_row_constraint_vec CONTAINS the widget constraints
let widget_draw_locs = Layout::default() let widget_draw_locs = Layout::default()
.constraints(col_row_constraint_vec.as_ref()) .constraints(col_row_constraint_vec.as_ref())
.direction(Direction::Horizontal) .direction(Direction::Horizontal)
.split(col_row_draw_loc); .split(draw_loc);
// Side effect, draw here.
self.draw_widgets_with_constraints( self.draw_widgets_with_constraints(
&mut f, &mut f,
app_state, app_state,
@ -661,35 +654,30 @@ impl Painter {
&widget_draw_locs, &widget_draw_locs,
); );
derived_col_draw_locs.push(widget_draw_locs); widget_draw_locs
})
.collect()
})
.collect()
}, },
); )
derived_row_draw_locs.push(derived_col_draw_locs); .collect();
},
);
new_derived_widget_draw_locs.push(derived_row_draw_locs);
});
self.derived_widget_draw_locs = new_derived_widget_draw_locs;
} else { } else {
self.widget_layout self.widget_layout
.rows .rows
.iter() .iter()
.zip(&self.derived_widget_draw_locs) .map(|row| &row.children)
.for_each(|(cols, row_layout)| { .flatten()
cols.children.iter().zip(row_layout).for_each( .map(|col| &col.children)
|(col_rows, col_row_layout)| { .flatten()
col_rows.children.iter().zip(col_row_layout).for_each( .zip(self.derived_widget_draw_locs.iter().flatten().flatten())
|(widgets, widget_draw_locs)| { .for_each(|(widgets, widget_draw_locs)| {
self.draw_widgets_with_constraints( self.draw_widgets_with_constraints(
&mut f, &mut f,
app_state, app_state,
widgets, widgets,
&widget_draw_locs, &widget_draw_locs,
); );
},
);
},
);
}); });
} }
} }