The Format Module & Pretty Printing

Format adds pretty-printing boxes and line breaking on top of Printf-style formatting.

Format adds pretty-printing boxes and line breaking on top of Printf-style formatting.

open/close a box (@[<v 2>, @[<hov 2>, @[<h>, @[<hv>) break hint (no space) full break hint flush + newline

Libraries: Fmt (combinators: Fmt.(list ~sep:comma int)), ppx_deriving.show generates pp functions automatically.

let pp_point fmt (x, y) = Format.fprintf fmt "(%d, %d)" x y

let () = Format.printf "Point: %a@." pp_point (1, 2)
let pp_list pp_elt fmt l =
  Format.fprintf fmt "@[<hov 2>[%a]@]"
    (Format.pp_print_list ~pp_sep:(fun f () -> Format.fprintf f ";@ ") pp_elt) l

let () = Format.printf "%a@." (pp_list Format.pp_print_int) (List.init 30 Fun.id)

let s = Format.asprintf "%a" pp_point (3, 4)
Format.pp_print_string, pp_print_option, pp_print_result, pp_print_seq, pp_print_text
Format.set_margin 80