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,96 +598,85 @@ 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
.iter()
.zip(&row_draw_locs)
.map(|(col_constraint, row_draw_loc)| {
Layout::default()
.constraints(col_constraint.as_ref())
.direction(Direction::Horizontal)
.split(*row_draw_loc)
})
.collect::<Vec<_>>();
let col_row_draw_locs = self
.col_row_constraints
.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! self.derived_widget_draw_locs = izip!(
let mut new_derived_widget_draw_locs = Vec::new(); draw_locs,
izip!( &self.col_constraints,
&self.col_row_constraints,
&self.layout_constraints, &self.layout_constraints,
col_row_draw_locs,
&self.widget_layout.rows &self.widget_layout.rows
) )
.for_each(|(row_constraint_vec, row_draw_loc, cols)| { .map(
let mut derived_row_draw_locs = Vec::new(); |(
izip!(row_constraint_vec, row_draw_loc, &cols.children).for_each( draw_loc,
|(col_constraint_vec, col_draw_loc, col_rows)| { col_constraint,
let mut derived_col_draw_locs = Vec::new(); col_row_constraint,
izip!(col_constraint_vec, col_draw_loc, &col_rows.children) row_constraint_vec,
.for_each( cols,
|(col_row_constraint_vec, col_row_draw_loc, widgets)| { )| {
// Note that col_row_constraint_vec CONTAINS the widget constraints izip!(
let widget_draw_locs = Layout::default() Layout::default()
.constraints(col_row_constraint_vec.as_ref()) .constraints(col_constraint.as_ref())
.direction(Direction::Horizontal) .direction(Direction::Horizontal)
.split(col_row_draw_loc); .split(draw_loc)
.into_iter(),
col_row_constraint,
row_constraint_vec,
&cols.children
)
.map(|(split_loc, constraint, col_constraint_vec, col_rows)| {
izip!(
Layout::default()
.constraints(constraint.as_ref())
.direction(Direction::Vertical)
.split(split_loc)
.into_iter(),
col_constraint_vec,
&col_rows.children
)
.map(|(draw_loc, col_row_constraint_vec, widgets)| {
// Note that col_row_constraint_vec CONTAINS the widget constraints
let widget_draw_locs = Layout::default()
.constraints(col_row_constraint_vec.as_ref())
.direction(Direction::Horizontal)
.split(draw_loc);
self.draw_widgets_with_constraints( // Side effect, draw here.
&mut f, self.draw_widgets_with_constraints(
app_state, &mut f,
widgets, app_state,
&widget_draw_locs, widgets,
); &widget_draw_locs,
derived_col_draw_locs.push(widget_draw_locs);
},
); );
derived_row_draw_locs.push(derived_col_draw_locs);
}, widget_draw_locs
); })
new_derived_widget_draw_locs.push(derived_row_draw_locs); .collect()
}); })
self.derived_widget_draw_locs = new_derived_widget_draw_locs; .collect()
},
)
.collect();
} 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,
);
},
);
},
); );
}); });
} }