Skip to content

Commit 547c5ae

Browse files
committed
add pretty-json
1 parent 4270619 commit 547c5ae

File tree

4 files changed

+44
-32
lines changed

4 files changed

+44
-32
lines changed

show.sld

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
radix precision decimal-sep decimal-align
1616
sign-rule comma-rule comma-sep word-separator?
1717
ambiguous-is-wide?)
18-
(export pretty pretty-shared pretty-simply pretty-color)
18+
(export pretty pretty-shared pretty-simply pretty-color
19+
pretty-json pretty-json-color)
1920
(export columnar tabular
2021
boxed boxed/double boxed/ascii boxed/custom
2122
wrapped wrapped/list wrapped/char justified

show/block/json.sld

+16-13
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@
1515
(schemepunk syntax)
1616
(schemepunk list)
1717
(schemepunk json)
18-
(schemepunk generator)
1918
(schemepunk term-colors)
2019
(schemepunk show span)
21-
(schemepunk show block))
20+
(schemepunk show block)
21+
(schemepunk show block datum))
2222

2323
(begin
24-
(define json-color-object (make-parameter yellow))
25-
(define json-color-object-key (make-parameter cyan))
26-
(define json-color-array (make-parameter white))
27-
(define json-color-string (make-parameter green))
28-
(define json-color-number (make-parameter magenta))
29-
(define json-color-true (make-parameter red))
30-
(define json-color-false (make-parameter red))
31-
(define json-color-null (make-parameter red))
32-
(define json-color-error (make-parameter red))
24+
(define json-color-object (make-parameter #f))
25+
(define json-color-object-key (make-parameter #f))
26+
(define json-color-array (make-parameter #f))
27+
(define json-color-string (make-parameter #f))
28+
(define json-color-number (make-parameter #f))
29+
(define json-color-true (make-parameter #f))
30+
(define json-color-false (make-parameter #f))
31+
(define json-color-null (make-parameter #f))
32+
(define json-color-error (make-parameter #f))
3333

3434
(define (with-default-json-colors thunk)
3535
(parameterize ((json-color-object yellow)
@@ -101,6 +101,9 @@
101101
('null
102102
(make-block
103103
(list (text-span "null" (json-color-null)))))
104-
(else
104+
(datum
105105
(make-block
106-
(list (text-span "<NOT JSON>" (json-color-error)))))))))
106+
(list (text-span "(NOT JSON:" (json-color-error))
107+
(whitespace-span))
108+
(list (datum->block datum))
109+
(list (text-span ")" (json-color-error)))))))))

show/debug.sld

+4-16
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
11
(define-library (schemepunk show debug)
22
(export write-debug write-debug-json)
33
(import (scheme base)
4-
(schemepunk syntax)
5-
(schemepunk generator)
6-
(schemepunk show span)
7-
(schemepunk show block)
8-
(schemepunk show block datum)
9-
(schemepunk show block json))
4+
(schemepunk show base)
5+
(schemepunk show pretty))
106
(begin
117
(define (write-debug datum)
12-
(with-default-datum-colors (λ ()
13-
(chain (datum->block datum)
14-
(block->span-generator/indented)
15-
(generator-for-each write-span))
16-
(newline))))
8+
(show #t (pretty-color datum) fl))
179

1810
(define (write-debug-json json)
19-
(with-default-json-colors (λ ()
20-
(chain (json->block json)
21-
(block->span-generator/indented)
22-
(generator-for-each write-span))
23-
(newline))))))
11+
(show #t (pretty-json-color json) fl))))

show/pretty.sld

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
(define-library (schemepunk show pretty)
22
(export pretty pretty-shared pretty-simply pretty-color
3-
indent-size)
3+
pretty-json pretty-json-color indent-size)
44
(import (scheme base)
55
(schemepunk syntax)
66
(schemepunk show base)
77
(schemepunk show span)
88
(schemepunk show block)
9-
(schemepunk show block datum))
9+
(schemepunk show block datum)
10+
(schemepunk show block json))
1011

1112
(begin
1213
(define indent-size (make-state-variable "indent-size" 2))
@@ -46,4 +47,23 @@
4647
(datum->block obj)
4748
width
4849
string-width
50+
indent-size))))))
51+
52+
(define (pretty-json json)
53+
(fn (width string-width indent-size)
54+
(span-generator->formatter
55+
(block->span-generator/indented
56+
(json->block json)
57+
width
58+
string-width
59+
indent-size))))
60+
61+
(define (pretty-json-color json)
62+
(fn (width string-width indent-size)
63+
(span-generator->formatter
64+
(with-default-json-colors (λ()
65+
(block->span-generator/indented
66+
(json->block json)
67+
width
68+
string-width
4969
indent-size))))))))

0 commit comments

Comments
 (0)