Help with padding while formatting

I’m trying to wrap my head around formatters and tried reading all of these with varying degree of success:

https://cedeela.fr/format-all-the-data-structures.html
http://caml.inria.fr/pub/docs/manual-ocaml/libref/Format.html
http://erratique.ch/software/fmt/doc/Fmt.html

I am still not sure how to address my actual problem…

I have parsed lines, which (for the sake of example) look something like this:

type fruit = Apples | Oranges
type quantity = (int * fruit)
type ast_line = {name: string;
                 quantity:  quantity option;
                 comment: string option}

I’d like to be able to print something like this (note the padding)

Alice                         5 apples      // some comment
Bob                        5553 oranges     // some comment
Charles                                     // some comment

Of course, I know how to do that by calculating lengths of strings, but I suspect there is a better way by using some of these libraries, though I can’t figure out if they do provide the needed tools.

If the name field of ast_line can contain arbitrarily long strings, you’ll need to compute the correct padding for a whole set of line before printing anyway. In this case, you might be interested by the PrintBox library whose purpose is to make these calculations for you.

2 Likes