Add calculator alignment feature

This commit is contained in:
Austen Adler 2021-05-19 18:58:07 -04:00
parent 4f4af9c5ef
commit a83e680521
3 changed files with 41 additions and 23 deletions

View File

@ -4,8 +4,9 @@ pub mod operations;
use confy::{load, store}; use confy::{load, store};
use constants::{ use constants::{
CalculatorAngleMode, CalculatorConstant, CalculatorConstants, CalculatorDisplayMode, CalculatorAlignment, CalculatorAngleMode, CalculatorConstant, CalculatorConstants,
CalculatorMacro, CalculatorMacros, CalculatorRegisters, CalculatorState, RegisterState, CalculatorDisplayMode, CalculatorMacro, CalculatorMacros, CalculatorRegisters, CalculatorState,
RegisterState,
}; };
use errors::{CalculatorError, CalculatorResult}; use errors::{CalculatorError, CalculatorResult};
use operations::{CalculatorOperation, CalculatorStateChange, MacroState, OpArgs}; use operations::{CalculatorOperation, CalculatorStateChange, MacroState, OpArgs};
@ -36,6 +37,7 @@ pub struct Calculator {
#[serde(skip)] #[serde(skip)]
l: String, l: String,
save_on_close: bool, save_on_close: bool,
pub calculator_alignment: CalculatorAlignment,
pub stack: VecDeque<f64>, pub stack: VecDeque<f64>,
#[serde(serialize_with = "ordered_char_map")] #[serde(serialize_with = "ordered_char_map")]
pub macros: CalculatorMacros, pub macros: CalculatorMacros,
@ -53,7 +55,6 @@ pub struct Calculator {
pub state: CalculatorState, pub state: CalculatorState,
pub angle_mode: CalculatorAngleMode, pub angle_mode: CalculatorAngleMode,
pub display_mode: CalculatorDisplayMode, pub display_mode: CalculatorDisplayMode,
// calculator_alignment: CalculatorAlignment,
} }
fn ordered_char_map<S, T>(value: &HashMap<char, T>, serializer: S) -> Result<S::Ok, S::Error> fn ordered_char_map<S, T>(value: &HashMap<char, T>, serializer: S) -> Result<S::Ok, S::Error>
@ -127,7 +128,7 @@ impl Default for Calculator {
.collect(), .collect(),
angle_mode: CalculatorAngleMode::default(), angle_mode: CalculatorAngleMode::default(),
display_mode: CalculatorDisplayMode::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 = false,
'W' => self.save_on_close = true, 'W' => self.save_on_close = true,
'L' => self.calculator_alignment = CalculatorAlignment::Left,
'R' => self.calculator_alignment = CalculatorAlignment::Right,
_ => return Err(CalculatorError::NoSuchSetting(c)), _ => return Err(CalculatorError::NoSuchSetting(c)),
}; };
self.state = CalculatorState::Normal; self.state = CalculatorState::Normal;

View File

@ -96,15 +96,14 @@ impl Default for CalculatorDisplayMode {
} }
} }
// #[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
// #[serde(tag = "calculator_alignment")] pub enum CalculatorAlignment {
// pub enum CalculatorAlignment { Right,
// Right, Left,
// Left, }
// }
// impl Default for CalculatorAlignment { impl Default for CalculatorAlignment {
// fn default() -> Self { fn default() -> Self {
// CalculatorAlignment::Left CalculatorAlignment::Left
// } }
// } }

View File

@ -6,7 +6,7 @@ mod event;
mod format; mod format;
use calc::{ use calc::{
constants::{CalculatorDisplayMode, CalculatorState, RegisterState}, constants::{CalculatorAlignment, CalculatorDisplayMode, CalculatorState, RegisterState},
errors::CalculatorResult, errors::CalculatorResult,
Calculator, Calculator,
}; };
@ -85,9 +85,9 @@ impl App {
L => Ln e => *10^\n\ L => Ln e => *10^\n\
<right> => Swap <down> => Edit\n\ <right> => Swap <down> => Edit\n\
uU => Undo/Redo ` => Constants\n\ uU => Undo/Redo ` => Constants\n\
m => Macros rR => Registers\n\ r => Load Register R => Save Register\n\
^s => Save Config ^l => Load Config\n\ m => Macros @ => Settings\n\
@ => Settings\ ^s => Save Config ^l => Load Config\
", ",
}, },
f, f,
@ -161,7 +161,9 @@ impl App {
f => Fixed\n\ f => Fixed\n\
F => Fixed (stack precision)\n\ F => Fixed (stack precision)\n\
w => Do not write settings and stack on quit (default)\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, f,
@ -215,7 +217,7 @@ impl App {
.calculator .calculator
.stack .stack
.iter() .iter()
.take(chunks[1].height as usize - 2) .take((chunks[1].height as usize).saturating_sub(2))
.enumerate() .enumerate()
.rev() .rev()
.map(|(i, m)| { .map(|(i, m)| {
@ -234,8 +236,22 @@ impl App {
format!("{:0>.precision$}", m, precision = precision) format!("{:0>.precision$}", m, precision = precision)
} }
}; };
let content = vec![Spans::from(Span::raw(format!("{:>2}: {}", i, number)))]; let content = match self.calculator.calculator_alignment {
ListItem::new(content) 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(); .collect();