diff options
author | ClementTsang <cjhtsang@uwaterloo.ca> | 2020-03-07 23:47:53 -0500 |
---|---|---|
committer | ClementTsang <cjhtsang@uwaterloo.ca> | 2020-03-07 23:47:53 -0500 |
commit | 03ec52c5b16d8968e14e1a648bd2ef4b9489380e (patch) | |
tree | 2de0ee608bbf72255be03b3fab35f87ab14fd11a /src/canvas/widgets/basic_table_arrows.rs | |
parent | 132a5a2170294f0d5c463ecba77de1aab32183db (diff) |
Split up widgets to make it a bit easier to work with.
Diffstat (limited to 'src/canvas/widgets/basic_table_arrows.rs')
-rw-r--r-- | src/canvas/widgets/basic_table_arrows.rs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/canvas/widgets/basic_table_arrows.rs b/src/canvas/widgets/basic_table_arrows.rs new file mode 100644 index 00000000..ec6b6515 --- /dev/null +++ b/src/canvas/widgets/basic_table_arrows.rs @@ -0,0 +1,74 @@ +use std::cmp::max; + +use crate::{ + app::{App, WidgetPosition}, + canvas::Painter, +}; + +use tui::{ + backend::Backend, + layout::{Constraint, Layout, Rect}, + terminal::Frame, + widgets::{Block, Paragraph, Text, Widget}, +}; + +pub trait BasicTableArrows { + fn draw_basic_table_arrows<B: Backend>( + &self, f: &mut Frame<'_, B>, app_state: &mut App, draw_loc: Rect, + ); +} + +impl BasicTableArrows for Painter { + fn draw_basic_table_arrows<B: Backend>( + &self, f: &mut Frame<'_, B>, app_state: &mut App, draw_loc: Rect, + ) { + // Effectively a paragraph with a ton of spacing + + // TODO: [MODULARITY] This is hard coded. Gross. + let (left_table, right_table) = if app_state.current_widget_selected.is_widget_table() { + match app_state.current_widget_selected { + WidgetPosition::Process | WidgetPosition::ProcessSearch => { + (WidgetPosition::Temp, WidgetPosition::Disk) + } + WidgetPosition::Disk => (WidgetPosition::Process, WidgetPosition::Temp), + WidgetPosition::Temp => (WidgetPosition::Disk, WidgetPosition::Process), + _ => (WidgetPosition::Disk, WidgetPosition::Temp), + } + } else { + match app_state.previous_basic_table_selected { + WidgetPosition::Process | WidgetPosition::ProcessSearch => { + (WidgetPosition::Temp, WidgetPosition::Disk) + } + WidgetPosition::Disk => (WidgetPosition::Process, WidgetPosition::Temp), + WidgetPosition::Temp => (WidgetPosition::Disk, WidgetPosition::Process), + _ => (WidgetPosition::Disk, WidgetPosition::Temp), + } + }; + + let left_name = left_table.get_pretty_name(); + let right_name = right_table.get_pretty_name(); + + let num_spaces = max( + 0, + draw_loc.width as i64 - 2 - 4 - (left_name.len() + right_name.len()) as i64, + ) as usize; + + let arrow_text = vec![ + Text::Styled( + format!("\n◄ {}", right_name).into(), + self.colours.text_style, + ), + Text::Raw(" ".repeat(num_spaces).into()), + Text::Styled(format!("{} ►", left_name).into(), self.colours.text_style), + ]; + + let margined_draw_loc = Layout::default() + .constraints([Constraint::Percentage(100)].as_ref()) + .horizontal_margin(1) + .split(draw_loc); + + Paragraph::new(arrow_text.iter()) + .block(Block::default()) + .render(f, margined_draw_loc[0]); + } +} |