I want to print list and its contents as horizontally or vertically placed blocks where head element has leading [
and other elements have leading ;
. Printing integers works fine
[ 1; 1; 1
; 1; 1; 1]
but printing complex structures goes messy
[ { a=1
, b="x" }
; { a=1
, b="x" }
; { a=1111
, b=
"xxxx"
}; {
a=1111
,
b=
"xxxx"
}]
How can I get line breaks before every ;
in last example? Do I need explicit line breaks?
Full code:
type t = {a : int; b: string}
let fmt_t fmt {a; b} =
Format.fprintf fmt
"@[<hov>@[{@ @[a@,=@,%a@]@]@ @[, @[b@,=@,%a@]@]@ }@]"
(fun fmt -> Format.fprintf fmt "%d") a
(fun fmt -> Format.fprintf fmt "%S") b
let fmt_list fa fmt xs =
Format.fprintf fmt "@[@,[";
let () = match xs with
| [] -> ()
| x::xs ->
Format.fprintf fmt "@[ %a@]" fa x;
List.iter (Format.fprintf fmt "@[; %a@]" fa) xs;
in
Format.fprintf fmt "]@]"
let () =
let open Format in
let str = {a=1; b="x"} in
let str2 = {a=1111; b="xxxx"} in
pp_set_margin std_formatter 12;
printf "%a\n" (fmt_list (fun fmt -> fprintf fmt "%d"))
[ 1; 1; 1; 1; 1; 1; ];
printf "%a\n" (fmt_list fmt_t)
[ str; str; str2; str2; ];
()