Skip to content

Commit 1588ff3

Browse files
djpowellStuart Sierra
authored andcommitted
pprint now flushes the underlying stream similarly to prn. pprint was using .flush as a method to push and reset its own internal data, but not to flush the underlying stream. The current .flush functionality has been moved into a .ppflush method, and .flush now additionally flushes the underlying stream.
Signed-off-by: Stuart Sierra <mail@stuartsierra.com>
1 parent 5263327 commit 1588ff3

6 files changed

Lines changed: 60 additions & 5 deletions

File tree

src/clj/clojure/pprint/column_writer.clj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
(let [fields (ref {:max max-columns, :cur 0, :line 0 :base writer})]
5959
(proxy [Writer IDeref] []
6060
(deref [] fields)
61+
(flush []
62+
(.flush writer))
6163
(write
6264
([^chars cbuf ^Integer off ^Integer len]
6365
(let [^Writer writer (get-field this :base)]

src/clj/clojure/pprint/pprint_base.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ radix specifier is in the form #XXr where XX is the decimal value of *print-base
164164
(make-pretty-writer base-writer# *print-right-margin* *print-miser-width*)
165165
base-writer#)]
166166
~@body
167-
(.flush *out*))))
167+
(.ppflush *out*))))
168168

169169

170170
;;;TODO: if pretty print is not set, don't use pr but rather something that respects *print-base*, etc.

src/clj/clojure/pprint/pretty_writer.clj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@
380380
:miser-width miser-width
381381
:trailing-white-space nil
382382
:pos 0})]
383-
(proxy [Writer IDeref] []
383+
(proxy [Writer IDeref PrettyFlush] []
384384
(deref [] fields)
385385

386386
(write
@@ -408,13 +408,17 @@
408408
Long
409409
(p-write-char this x))))
410410

411-
(flush []
411+
(ppflush []
412412
(if (= (getf :mode) :buffering)
413-
(dosync
413+
(dosync
414414
(write-tokens this (getf :buffer) true)
415415
(setf :buffer []))
416416
(write-white-space this)))
417417

418+
(flush []
419+
(.ppflush this)
420+
(.flush (getf :base)))
421+
418422
(close []
419423
(.flush this)))))
420424

src/clj/clojure/pprint/utilities.clj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,6 @@ beginning of aseq"
102102
`(prerr ~@(cons (list 'quote prefix) (mapcat #(list (list 'quote %) "=" %)
103103
(cons arg (seq more-args))))))
104104

105+
;; Flush the pretty-print buffer without flushing the underlying stream
106+
(definterface PrettyFlush
107+
(^void ppflush []))

test/clojure/test_clojure/pprint.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
(ns clojure.test-clojure.pprint
1212
(:refer-clojure :exclude [format])
13-
(:use [clojure.test :only (deftest are run-tests)]
13+
(:use [clojure.test :only (deftest is are run-tests)]
1414
[clojure.test-helper :only [platform-newlines]]
1515
clojure.test-clojure.pprint.test-helper
1616
clojure.pprint))

test/clojure/test_clojure/pprint/test_pretty.clj

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,49 @@ It is implemented with a number of custom enlive templates.\"
326326
"[1, 2, 3, 4, 5, 6]\n"
327327
)
328328

329+
(defn- flush-alerting-writer
330+
[o]
331+
(let [flush-count-atom (atom 0)]
332+
[
333+
(proxy [java.io.BufferedWriter] [o]
334+
(flush []
335+
(proxy-super flush)
336+
(swap! flush-count-atom inc)))
337+
flush-count-atom]))
338+
339+
(deftest test-flush-underlying-prn
340+
[]
341+
(let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter.))]
342+
(binding [*out* out
343+
*flush-on-newline* true]
344+
(prn (range 50))
345+
(prn (range 50)))
346+
(is (= @flush-count-atom 2) "println flushes on newline")))
347+
348+
(deftest test-flush-underlying-pprint
349+
[]
350+
(let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter.))]
351+
(binding [*out* out
352+
*flush-on-newline* true]
353+
(pprint (range 50))
354+
(pprint (range 50)))
355+
(is (= @flush-count-atom 2) "pprint flushes on newline")))
356+
357+
(deftest test-noflush-underlying-prn
358+
[]
359+
(let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter.))]
360+
(binding [*out* out
361+
*flush-on-newline* nil]
362+
(prn (range 50))
363+
(prn (range 50)))
364+
(is (= @flush-count-atom 0) "println flushes on newline")))
365+
366+
(deftest test-noflush-underlying-pprint
367+
[]
368+
(let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter.))]
369+
(binding [*out* out
370+
*flush-on-newline* nil]
371+
(pprint (range 50))
372+
(pprint (range 50)))
373+
(is (= @flush-count-atom 0) "pprint flushes on newline")))
374+

0 commit comments

Comments
 (0)