From 6749c14b83101536c66341beb6833ad14c634992 Mon Sep 17 00:00:00 2001 From: Mathieu Barbin Date: Thu, 16 Nov 2023 02:22:38 +0100 Subject: [PATCH] add Pp.verbatimf (#18) * add Pp.verbatimf Follow the same naming scheme as existing functions textf and paragraphf. Signed-off-by: Mathieu Barbin --- CHANGES.md | 2 ++ src/pp.ml | 1 + src/pp.mli | 3 +++ test/tests.ml | 4 ++++ 4 files changed, 10 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 679b048..69bd112 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,8 @@ Unreleased ---------- +- Add `Pp.verbatimf`. (#18, @mbarbin) + - Remove `of_fmt` constructor. (#17, @Alizter) 1.2.0 diff --git a/src/pp.ml b/src/pp.ml index c18c16e..e67d498 100644 --- a/src/pp.ml +++ b/src/pp.ml @@ -134,6 +134,7 @@ let hbox t = Hbox t let hvbox ?(indent = 0) t = Hvbox (indent, t) let hovbox ?(indent = 0) t = Hovbox (indent, t) let verbatim x = Verbatim x +let verbatimf fmt = Printf.ksprintf verbatim fmt let char x = Char x let custom_break ~fits ~breaks = Break (fits, breaks) diff --git a/src/pp.mli b/src/pp.mli index cd5d55e..79a35d2 100644 --- a/src/pp.mli +++ b/src/pp.mli @@ -29,6 +29,9 @@ val concat_mapi : ?sep:'tag t -> 'a list -> f:(int -> 'a -> 'tag t) -> 'tag t (** An indivisible block of text. *) val verbatim : string -> 'tag t +(** Same as [verbatim] but take a format string as argument. *) +val verbatimf : ('a, unit, string, 'tag t) format4 -> 'a + (** A single character. *) val char : char -> 'tag t diff --git a/test/tests.ml b/test/tests.ml index 42aeaf4..42aeeb6 100644 --- a/test/tests.ml +++ b/test/tests.ml @@ -102,6 +102,10 @@ Hello x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x |}] +let%expect_test "verbatimf" = + print (Pp.verbatimf "ident%d" 42); + [%expect {| ident42 |}] + (* Difference between box and hovbox *) let%expect_test _ = let pp f = f (xs 50 ++ Pp.break ~nspaces:2 ~shift:(-1) ++ xs 10) in