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 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<f64>,
#[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<S, T>(value: &HashMap<char, T>, serializer: S) -> Result<S::Ok, S::Error>
@ -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;

View File

@ -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
}
}

View File

@ -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\
<right> => Swap <down> => 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();