diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2023-02-02 13:43:47 -0330 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2023-02-02 13:43:47 -0330 |
| commit | c3c5d07e70e884d68caca4a42424dba839a8ddb5 (patch) | |
| tree | 2a0bb9b11f7597bb078c568470711f70966623d2 | |
| parent | 694a2de36951a145636dede5a0cad9e2179089e8 (diff) | |
| download | pfc-c3c5d07e70e884d68caca4a42424dba839a8ddb5.zip | |
refactor ui
| -rw-r--r-- | src/ui.rs | 42 |
1 files changed, 24 insertions, 18 deletions
@@ -15,24 +15,8 @@ use crate::Calculator; impl Calculator { pub fn draw<B: Backend>(&self, f: &mut Frame<B>) { - let chunks = Layout::default() - .direction(Direction::Vertical) - .constraints( - [ - Constraint::Max(u16::MAX), - Constraint::Length(self.stack.len() as u16), - Constraint::Length(1), - ] - .as_ref(), - ) - .split(f.size()); - - let items: Vec<ListItem> = (self.stack) - .iter() - .map(|f| ListItem::new(format!("{}", f))) - .collect(); - f.render_widget(List::new(items), chunks[1]); - + let chunks = layout(self.stack.len()).split(f.size()); + f.render_widget(stack_list(&self.stack), chunks[1]); f.render_widget(Paragraph::new(self.input_buffer.as_str()), chunks[2]); } } @@ -58,3 +42,25 @@ where terminal.show_cursor()?; Ok(()) } + +fn layout(stack_size: usize) -> Layout { + Layout::default() + .direction(Direction::Vertical) + .constraints( + [ + Constraint::Max(u16::MAX), + Constraint::Length(stack_size as u16), + Constraint::Length(1), + ] + .as_ref(), + ) +} + +fn stack_list(stack: &Vec<f64>) -> List { + List::new( + stack + .iter() + .map(|f| ListItem::new(format!("{}", f))) + .collect::<Vec<ListItem>>(), + ) +} |