feat: preserve leading_spaces

This commit is contained in:
electria 2026-09-30 15:18:15 -07:00
commit 9bacccfb71
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs

View file

@ -22,6 +22,12 @@ fn main() {
} }
pub fn parse_and_calc_humanreadable(mut input: String) -> String { pub fn parse_and_calc_humanreadable(mut input: String) -> String {
// no need to remove the characters here,
// as we ignore the them in all other logic already
let should_add_back_alignment = input.contains('&');
let should_add_back_backslash = input.contains('\\');
let leading_spaces = input.chars().take_while(|c| *c == ' ').count();
let should_add_back_equals = input let should_add_back_equals = input
.find('=') .find('=')
.inspect(|i| { .inspect(|i| {
@ -29,15 +35,12 @@ pub fn parse_and_calc_humanreadable(mut input: String) -> String {
}) })
.is_some(); .is_some();
// no need to remove the characters here,
// as we ignore the them in all other logic already
let should_add_back_alignment = input.contains('&');
let should_add_back_backslash = input.contains('\\');
let result = parse_and_calc(&input); let result = parse_and_calc(&input);
let significant_digits = get_significant_digits(&result);
format!( format!(
"{0}{result:.1$}{2}", "{:leading_spaces$}{}{result:.significant_digits$}{}",
' ',
if should_add_back_equals && should_add_back_alignment { if should_add_back_equals && should_add_back_alignment {
"& = " "& = "
} else if should_add_back_equals { } else if should_add_back_equals {
@ -47,15 +50,14 @@ pub fn parse_and_calc_humanreadable(mut input: String) -> String {
} else { } else {
"" ""
}, },
get_significant_digits(&result),
if should_add_back_backslash { " \\" } else { "" } if should_add_back_backslash { " \\" } else { "" }
) )
} }
#[test] #[test]
fn test_equals_readdition() { fn test_equals_readdition() {
assert_eq!(parse_and_calc_humanreadable(" = 11".into()), "= 11"); assert_eq!(parse_and_calc_humanreadable(" = 11".into()), " = 11");
assert_eq!(parse_and_calc_humanreadable(" &= 11".into()), "& = 11"); assert_eq!(parse_and_calc_humanreadable(" &= 11".into()), " & = 11");
assert_eq!(parse_and_calc_humanreadable(" & 11".into()), "& 11"); assert_eq!(parse_and_calc_humanreadable(" & 11".into()), " & 11");
} }
pub fn parse_and_calc(s: &str) -> Float { pub fn parse_and_calc(s: &str) -> Float {