From 35f9b32a5b56501c8774d67b7149eea07b32a454 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Fri, 9 Feb 2018 14:20:46 -0600 Subject: [PATCH] add `CCFormat.lazy_{or,force}` for printing thunks --- src/core/CCFormat.ml | 5 +++++ src/core/CCFormat.mli | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/src/core/CCFormat.ml b/src/core/CCFormat.ml index c99aae73..d01f3ad9 100644 --- a/src/core/CCFormat.ml +++ b/src/core/CCFormat.ml @@ -133,6 +133,11 @@ let some pp out = function | None -> () | Some x -> pp out x +let lazy_force pp out (lazy x) = pp out x + +let lazy_or ?(default=return "") pp out x = + if Lazy.is_val x then pp out (Lazy.force x) else default out () + (** {2 IO} *) let output fmt pp x = pp fmt x diff --git a/src/core/CCFormat.mli b/src/core/CCFormat.mli index c750e9e3..1fa0cdfe 100644 --- a/src/core/CCFormat.mli +++ b/src/core/CCFormat.mli @@ -129,6 +129,15 @@ val some : 'a printer -> 'a option printer @since 1.0 *) +val lazy_force : 'a printer -> 'a lazy_t printer +(** [lazy_force pp out x] forces [x] and prints the result with [pp] + @since NEXT_RELEASE *) + +val lazy_or : ?default:unit printer -> 'a printer -> 'a lazy_t printer +(** [lazy_or ?default pp out x] prints [default] if [x] is not + evaluated yet, or uses [pp] otherwise + @since NEXT_RELEASE *) + (** {2 ANSI codes} Use ANSI escape codes https://en.wikipedia.org/wiki/ANSI_escape_code