diff --git a/src/main.rs b/src/main.rs index eb56906..44ebbf4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +// I am not yet worthy for this yet: clippy::restriction #![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)] // This is intended behavior #![allow(clippy::cast_possible_truncation)] @@ -8,6 +9,8 @@ mod calc; mod event; mod format; +const BORDER_SIZE: u16 = 2; + use calc::{ errors::CalculatorResult, types::{CalculatorAlignment, CalculatorDisplayMode, CalculatorState, RegisterState}, @@ -24,7 +27,7 @@ use event::{Event, Events}; use std::{cmp, convert::TryFrom, error::Error, io, io::Write}; use tui::{ backend::CrosstermBackend, - layout::{Constraint, Direction, Layout}, + layout::{Constraint, Direction, Layout, Rect}, style::{Modifier, Style}, terminal::Frame, text::{Span, Spans, Text}, @@ -178,21 +181,7 @@ impl App { } } - pub fn terminal_draw_func(&mut self, f: &mut Frame>) { - let chunks = Layout::default() - .direction(Direction::Vertical) - .margin(2) - .constraints( - [ - Constraint::Length(1), - Constraint::Min(1), - Constraint::Length(3), - ] - .as_ref(), - ) - .split(f.size()); - - // Help message + fn draw_status_line(&mut self, f: &mut Frame>, chunk: Rect) { let msg = match (&self.error_msg, &self.state) { (Some(e), _) => vec![ Span::raw("Error: "), @@ -215,14 +204,27 @@ impl App { }; let text = Text::from(Spans::from(msg)); let help_message = Paragraph::new(text); - f.render_widget(help_message, chunks[0]); + f.render_widget(help_message, chunk); + } - // Stack + fn draw_input_box(&mut self, f: &mut Frame>, chunk: Rect) { + let input = Paragraph::new(self.calculator.get_l().as_ref()) + .style(Style::default()) + .block(Block::default().borders(Borders::ALL).title("Input")); + f.render_widget(input, chunk); + + f.set_cursor( + chunk.x + self.calculator.get_l().len() as u16 + 1, + chunk.y + 1, + ); + } + + fn draw_stack(&mut self, f: &mut Frame>, chunk: Rect) { let mut stack: Vec = self .calculator .stack .iter() - .take((chunks[1].height as usize).saturating_sub(2)) + .take(chunk.height.saturating_sub(BORDER_SIZE) as usize) .enumerate() .rev() .map(|(i, m)| { @@ -245,11 +247,11 @@ impl App { CalculatorAlignment::Left => format!("{:>2}: {}", i, number), CalculatorAlignment::Right => { let ret = format!("{} :{:>2}", number, i); - if ret.len() < (chunks[1].width as usize).saturating_sub(2) { + if ret.len() < chunk.width.saturating_sub(BORDER_SIZE) as usize { format!( "{: >width$}", ret, - width = (chunks[1].width as usize).saturating_sub(2) + width = chunk.width.saturating_sub(BORDER_SIZE) as usize ) } else { ret @@ -260,10 +262,10 @@ impl App { }) .collect(); - for _ in 0..(chunks[1] + for _ in 0..(chunk .height .saturating_sub(stack.len() as u16) - .saturating_sub(2)) + .saturating_sub(BORDER_SIZE)) { stack.insert( 0, @@ -272,27 +274,35 @@ impl App { CalculatorAlignment::Right => format!( "{: >width$}", "~", - width = (chunks[1].width as usize).saturating_sub(2) + width = chunk.width.saturating_sub(BORDER_SIZE) as usize ), })), ); } let stack = List::new(stack).block(Block::default().borders(Borders::ALL).title("Stack")); - f.render_widget(stack, chunks[1]); + f.render_widget(stack, chunk); + } - // Input box - let input = Paragraph::new(self.calculator.get_l().as_ref()) - .style(Style::default()) - .block(Block::default().borders(Borders::ALL).title("Input")); - f.render_widget(input, chunks[2]); + pub fn terminal_draw_func(&mut self, f: &mut Frame>) { + let chunks = Layout::default() + .direction(Direction::Vertical) + .margin(BORDER_SIZE) + .constraints( + [ + Constraint::Length(1), + Constraint::Min(1), + Constraint::Length(3), + ] + .as_ref(), + ) + .split(f.size()); - f.set_cursor( - chunks[2].x + self.calculator.get_l().len() as u16 + 1, - chunks[2].y + 1, - ); + self.draw_stack(f, chunks[1]); + self.draw_input_box(f, chunks[2]); + // Draw the status line last in case something above has an error + self.draw_status_line(f, chunks[0]); - // Help text self.draw_clippy_dialogs(f); } } @@ -473,14 +483,14 @@ impl ClippyRectangle<'_> { } fn draw_clippy_rect(c: &ClippyRectangle, f: &mut Frame>) { - // let block = Block::default().title(c.title).borders(Borders::ALL); let dimensions = c.size(); let popup_layout = Layout::default() .direction(Direction::Vertical) .constraints( [ + // Padding Constraint::Min(1), - Constraint::Length(dimensions.height + 2), + Constraint::Length(dimensions.height + BORDER_SIZE), ] .as_ref(), ) @@ -489,8 +499,9 @@ fn draw_clippy_rect(c: &ClippyRectangle, f: &mut Frame