-
Notifications
You must be signed in to change notification settings - Fork 28
Open
Description
I'm trying to align a set of decimal numbers on the decimal point. From the decimal, I know how many fractional digits there are, so my idea was to format the decimal and then, with the width, calculate by how much to indent the decimal to align the points. However, it seems that DocBuilder::width does not support this use case.
Is there something I am missing or is this use case not supported currently?
My code that I used for trying it out:
use pretty::Arena;
use pretty::DocAllocator;
use pretty::DocBuilder;
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use std::io;
fn pretty_decimal<'a>(decimal: Decimal, allocator: &'a Arena<'a>) -> DocBuilder<'a, Arena<'a>> {
let decimals = decimal.scale();
let formatted = allocator.as_string(decimal);
formatted.clone().width(move |width| {
formatted
.indent(10 - (width as usize - decimals as usize))
.into_doc()
})
}
fn main() {
let arena = Arena::<()>::default();
arena
.intersperse(
[
pretty_decimal(dec!(123.00), &arena),
pretty_decimal(dec!(456.789), &arena),
],
arena.line(),
)
.render(20, &mut io::stdout())
.unwrap();
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels