Debian Codegraph @

lib/srfi-159/internal/base.scm

raw
1 ;; base.scm - base formatting monad
2 ;; Copyright (c) 2013 Alex Shinn. All rights reserved.
3 ;; BSD-style license: http://synthcode.com/license.txt
4
5 ;;> The minimal base formatting combinators and show interface.
6
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8
9 ;; The base formatting handles outputting raw strings and a simple,
10 ;; configurable handler for formatting objects.
11
12 ;; Utility - default value of string-width.
13 (define (substring-length str . o)
14 (let ((start (if (pair? o) (car o) 0))
15 (end (if (and (pair? o) (pair? (cdr o))) (cadr o) (string-length str))))
16 (- end start)))
17
18 ;; Raw output. All primitive output should go through this operation.
19 ;; Overridable, defaulting to output-default.
20 (define (output str)
21 (fn (output) ((or output output-default) str)))
22
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
24
25 ;;> \procedure{(show out [args ...])}
26 ;;>
27 ;;> Run the combinators \var{args}, accumulating the output to
28 ;;> \var{out}, which is either an output port or a boolean, with
29 ;;> \scheme{#t} indicating \scheme{current-output-port} and
30 ;;> \scheme{#f} to collect the output as a string.
31 (define (show out . args)
32 (let ((proc (each-in-list args)))
33 (cond
34 ((output-port? out)
35 (show-run out proc))
36 ((eq? #t out)
37 (show-run (current-output-port) proc))
38 ((eq? #f out)
39 (let ((out (open-output-string)))
40 (show-run out proc)
41 (get-output-string out)))
42 (else
43 (error "unknown output to show" out)))))
44
45 ;; Run with an output port with initial default values.
46 (define (show-run out proc)
47 (run (sequence (with! (port out)
48 (col 0)
49 (row 0)
50 (width 78)
51 (radix 10)
52 (pad-char #\space)
53 (output output-default)
54 (string-width substring-length))
55 proc)))
56
57 ;;> The noop formatter. Generates no output and leaves the state
58 ;;> unmodified.
59 (define nothing (fn () (with!)))
60
61 ;;> Formats a displayed version of x - if a string or char, outputs the
62 ;;> raw characters (as with `display'), if x is already a formatter
63 ;;> defers to that, otherwise outputs a written version of x.
64 (define (displayed x)
65 (cond
66 ((procedure? x) x)
67 ((string? x) (output x))
68 ((char? x) (output (string x)))
69 (else (written x))))
70
71 ;;> Formats a written version of x, as with `write'. The formatting
72 ;;> can be updated with the \scheme{'writer} field.
73 (define (written x)
74 (fn (writer) ((or writer written-default) x)))
75
76 ;;> Takes a single list of formatters, combined in sequence with
77 ;;> \scheme{each}.
78 (define (each-in-list args)
79 (if (pair? args)
80 (sequence (displayed (car args)) (each-in-list (cdr args)))
81 nothing))
82
83 ;;> Combines each of the formatters in a sequence using
84 ;;> \scheme{displayed}, so that strings and chars will be output
85 ;;> directly and other objects will be \scheme{written}.
86 (define (each . args)
87 (each-in-list args))
88
89 ;;> Raw output - displays str to the formatter output port and updates
90 ;;> row and col.
91 (define (output-default str)
92 (fn (port row col string-width)
93 (display str port)
94 (let ((nl-index (string-index-right str #\newline)))
95 (if (string-cursor>? nl-index (string-cursor-start str))
96 (with! (row (+ row (string-count str #\newline)))
97 (col (string-width str (string-cursor->index str nl-index))))
98 (with! (col (+ col (string-width str))))))))
99
100 ;;> Captures the output of \var{producer} and formats the result with
101 ;;> \var{consumer}.
102 (define (call-with-output producer consumer)
103 (let ((out (open-output-string)))
104 (forked (with ((port out) (output output-default)) producer)
105 (fn () (consumer (get-output-string out))))))