diff options
| author | 2025-10-23 10:34:58 +0300 | |
|---|---|---|
| committer | 2025-10-23 10:34:58 +0300 | |
| commit | 8c03a45147ebff513d0137521476a3e4990058a2 (patch) | |
| tree | 65eb9ecc5f1e5ff5706a04129cd08519d4c2859d /src/streams.lisp | |
| parent | Use serapeum's -> & defsubst (diff) | |
| download | ukkoclot-8c03a45147ebff513d0137521476a3e4990058a2.tar.gz ukkoclot-8c03a45147ebff513d0137521476a3e4990058a2.tar.xz ukkoclot-8c03a45147ebff513d0137521476a3e4990058a2.zip | |
Forgot to commit the new file
Diffstat (limited to 'src/streams.lisp')
| -rw-r--r-- | src/streams.lisp | 26 |
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, | ||
| 13 | calls 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 | |||
| 22 | If `stream-spec' is `t': Output to `*standard-output*' and return `nil'. | ||
| 23 | If `stream-spec' is `nil': Output to a string and return it. | ||
| 24 | Else: `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))) | ||