Setup calculator settings

This commit is contained in:
Austen Adler 2021-05-03 19:22:25 -04:00
parent 3b81fc7d1b
commit 4601b5104f
3 changed files with 17 additions and 4 deletions

View File

@ -260,9 +260,19 @@ impl<'a> Calculator<'a> {
',' => self.display_mode = CalculatorDisplayMode::Default(Some(',')),
' ' => self.display_mode = CalculatorDisplayMode::Default(Some(' ')),
's' => self.display_mode = CalculatorDisplayMode::Scientific(3),
'S' => self.display_mode = CalculatorDisplayMode::Scientific(self.pop_usize()?),
'S' => {
let precision = self.checked_get(0)? as usize;
if precision >= 20 {
return Err(CalculatorError::PrecisionTooHigh);
}
self.display_mode = CalculatorDisplayMode::Scientific(self.pop_usize()?)
}
'e' => self.display_mode = CalculatorDisplayMode::Engineering(3),
'E' => {
let precision = self.checked_get(0)? as usize;
if precision >= 20 {
return Err(CalculatorError::PrecisionTooHigh);
}
self.display_mode = CalculatorDisplayMode::Engineering(self.pop_usize()?)
}
_ => return Err(CalculatorError::NoSuchSetting(c)),

View File

@ -14,6 +14,7 @@ pub enum CalculatorError {
NoSuchSetting(char),
RecursiveMacro(char),
ParseError,
PrecisionTooHigh,
}
impl fmt::Display for CalculatorError {
@ -32,6 +33,7 @@ impl fmt::Display for CalculatorError {
CalculatorError::NoSuchSetting(c) => write!(f, "No such setting '{}'", c),
CalculatorError::RecursiveMacro(c) => write!(f, "Recursive macro '{}'", c),
CalculatorError::ParseError => write!(f, "Parse error"),
CalculatorError::PrecisionTooHigh => write!(f, "Precision too high"),
}
}
}

View File

@ -390,11 +390,12 @@ fn draw_clippy_rect<T: std::io::Write>(c: ClippyRectangle, f: &mut Frame<Termion
fn fmt_scientific(i: usize, f: f64, precision: usize) -> String {
let mut ret = format!("{:.precision$E}", f, precision = precision);
let exp = ret.split_off(ret.find('E').unwrap_or(0));
let (pow_sign, exp) = if exp.starts_with("E-") {
('-', &exp[2..])
let (pow_sign, exp) = if let Some(stripped) = exp.strip_prefix("E-") {
('-', stripped)
} else {
('+', &exp[1..])
};
let sign = if !ret.starts_with('-') { " " } else { "" };
format!(
"{:>2}: {}{}E{}{:0>pad$}",
@ -435,7 +436,7 @@ fn fmt_scientific(i: usize, f: f64, precision: usize) -> String {
fn fmt_separated(i: usize, f: f64, sep: char) -> String {
let mut ret = f.to_string();
let start = if ret.starts_with('-') { 1 } else { 0 };
let end = ret.find('.').unwrap_or(ret.len());
let end = ret.find('.').unwrap_or_else(|| ret.len());
for i in 0..((end - start - 1).div_euclid(3)) {
ret.insert(end - (i + 1) * 3, sep);
}