From a83e6805217d925f0bf8f6e27295e8bdbf622402 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Wed, 19 May 2021 18:58:07 -0400 Subject: [PATCH] Add calculator alignment feature --- src/calc.rs | 11 +++++++---- src/calc/constants.rs | 21 ++++++++++----------- src/main.rs | 32 ++++++++++++++++++++++++-------- 3 files changed, 41 insertions(+), 23 deletions(-) diff --git a/src/calc.rs b/src/calc.rs index 6dc04b6..f0ceb23 100644 --- a/src/calc.rs +++ b/src/calc.rs @@ -4,8 +4,9 @@ pub mod operations; use confy::{load, store}; use constants::{ - CalculatorAngleMode, CalculatorConstant, CalculatorConstants, CalculatorDisplayMode, - CalculatorMacro, CalculatorMacros, CalculatorRegisters, CalculatorState, RegisterState, + CalculatorAlignment, CalculatorAngleMode, CalculatorConstant, CalculatorConstants, + CalculatorDisplayMode, CalculatorMacro, CalculatorMacros, CalculatorRegisters, CalculatorState, + RegisterState, }; use errors::{CalculatorError, CalculatorResult}; use operations::{CalculatorOperation, CalculatorStateChange, MacroState, OpArgs}; @@ -36,6 +37,7 @@ pub struct Calculator { #[serde(skip)] l: String, save_on_close: bool, + pub calculator_alignment: CalculatorAlignment, pub stack: VecDeque, #[serde(serialize_with = "ordered_char_map")] pub macros: CalculatorMacros, @@ -53,7 +55,6 @@ pub struct Calculator { pub state: CalculatorState, pub angle_mode: CalculatorAngleMode, pub display_mode: CalculatorDisplayMode, - // calculator_alignment: CalculatorAlignment, } fn ordered_char_map(value: &HashMap, serializer: S) -> Result @@ -127,7 +128,7 @@ impl Default for Calculator { .collect(), angle_mode: CalculatorAngleMode::default(), display_mode: CalculatorDisplayMode::default(), - // calculator_alignment: CalculatorAlignment::default(), + calculator_alignment: CalculatorAlignment::default(), } } } @@ -313,6 +314,8 @@ impl Calculator { } 'w' => self.save_on_close = false, 'W' => self.save_on_close = true, + 'L' => self.calculator_alignment = CalculatorAlignment::Left, + 'R' => self.calculator_alignment = CalculatorAlignment::Right, _ => return Err(CalculatorError::NoSuchSetting(c)), }; self.state = CalculatorState::Normal; diff --git a/src/calc/constants.rs b/src/calc/constants.rs index dffd2d0..0f7280a 100644 --- a/src/calc/constants.rs +++ b/src/calc/constants.rs @@ -96,15 +96,14 @@ impl Default for CalculatorDisplayMode { } } -// #[derive(Clone, Debug, Serialize, Deserialize)] -// #[serde(tag = "calculator_alignment")] -// pub enum CalculatorAlignment { -// Right, -// Left, -// } +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum CalculatorAlignment { + Right, + Left, +} -// impl Default for CalculatorAlignment { -// fn default() -> Self { -// CalculatorAlignment::Left -// } -// } +impl Default for CalculatorAlignment { + fn default() -> Self { + CalculatorAlignment::Left + } +} diff --git a/src/main.rs b/src/main.rs index 3a0dcdd..c3e0944 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ mod event; mod format; use calc::{ - constants::{CalculatorDisplayMode, CalculatorState, RegisterState}, + constants::{CalculatorAlignment, CalculatorDisplayMode, CalculatorState, RegisterState}, errors::CalculatorResult, Calculator, }; @@ -85,9 +85,9 @@ impl App { L => Ln e => *10^\n\ => Swap => Edit\n\ uU => Undo/Redo ` => Constants\n\ - m => Macros rR => Registers\n\ - ^s => Save Config ^l => Load Config\n\ - @ => Settings\ + r => Load Register R => Save Register\n\ + m => Macros @ => Settings\n\ + ^s => Save Config ^l => Load Config\ ", }, f, @@ -161,7 +161,9 @@ impl App { f => Fixed\n\ F => Fixed (stack precision)\n\ w => Do not write settings and stack on quit (default)\n\ - W => Write stack and settings on quit\ + W => Write stack and settings on quit\n\ + L => Left align\n\ + R => Right align\ ", }, f, @@ -215,7 +217,7 @@ impl App { .calculator .stack .iter() - .take(chunks[1].height as usize - 2) + .take((chunks[1].height as usize).saturating_sub(2)) .enumerate() .rev() .map(|(i, m)| { @@ -234,8 +236,22 @@ impl App { format!("{:0>.precision$}", m, precision = precision) } }; - let content = vec![Spans::from(Span::raw(format!("{:>2}: {}", i, number)))]; - ListItem::new(content) + let content = match self.calculator.calculator_alignment { + 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) { + format!( + "{: >width$}", + ret, + width = (chunks[1].width as usize).saturating_sub(2) + ) + } else { + ret + } + } + }; + ListItem::new(Spans::from(Span::raw(content))) }) .collect();