summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/streams.lisp26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/streams.lisp b/src/streams.lisp
new file mode 100644
index 0000000..766cf07
--- /dev/null
+++ b/src/streams.lisp
@@ -0,0 +1,26 @@
1;; SPDX-License-Identifier: EUPL-1.2
2;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs <perkontevs@gmail.com>
3(defpackage :ukkoclot/src/streams
4 (:documentation "Stream-oriented utilities.")
5 (:import-from :serapeum :-> :with-thunk)
6 (:use :c2cl)
7 (:export :call-with-format-like-stream :with-format-like-stream))
8(in-package :ukkoclot/src/streams)
9
10(-> call-with-format-like-stream ((or stream boolean) (function (stream) t)) t)
11(defun call-with-format-like-stream (stream-spec fn)
12 "Similar to `with-format-like-stream', but instead of binding the translated stream in a macro body,
13calls the function `fn' with it."
14 (case stream-spec
15 ((t) (funcall fn *standard-output*) nil)
16 ((nil) (with-output-to-string (stream) (funcall fn stream)))
17 (otherwise (funcall fn stream-spec) nil)))
18
19(defmacro with-format-like-stream ((stream-out stream-spec) &body body)
20 "Translates the `stream-spec' into a `stream' similar to how `format' does it and binds it as `stream-out' in `body'.
21
22If `stream-spec' is `t': Output to `*standard-output*' and return `nil'.
23If `stream-spec' is `nil': Output to a string and return it.
24Else: `stream-spec' must be a stream, output to it and return `nil'"
25 (with-thunk (body stream-out)
26 `(call-with-format-like-stream ,stream-spec ,body)))