Permalink
Browse files

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>
  • Loading branch information...
1 parent 5263327 commit 1588ff3f70e864d9817bc565bd2c30b08189bc6e @djpowell djpowell committed with stuartsierra Jun 9, 2012
@@ -58,6 +58,8 @@
(let [fields (ref {:max max-columns, :cur 0, :line 0 :base writer})]
(proxy [Writer IDeref] []
(deref [] fields)
+ (flush []
+ (.flush writer))
(write
([^chars cbuf ^Integer off ^Integer len]
(let [^Writer writer (get-field this :base)]
@@ -164,7 +164,7 @@ radix specifier is in the form #XXr where XX is the decimal value of *print-base
(make-pretty-writer base-writer# *print-right-margin* *print-miser-width*)
base-writer#)]
~@body
- (.flush *out*))))
+ (.ppflush *out*))))
;;;TODO: if pretty print is not set, don't use pr but rather something that respects *print-base*, etc.
@@ -380,7 +380,7 @@
:miser-width miser-width
:trailing-white-space nil
:pos 0})]
- (proxy [Writer IDeref] []
+ (proxy [Writer IDeref PrettyFlush] []
(deref [] fields)
(write
@@ -408,13 +408,17 @@
Long
(p-write-char this x))))
- (flush []
+ (ppflush []
(if (= (getf :mode) :buffering)
- (dosync
+ (dosync
(write-tokens this (getf :buffer) true)
(setf :buffer []))
(write-white-space this)))
+ (flush []
+ (.ppflush this)
+ (.flush (getf :base)))
+
(close []
(.flush this)))))
@@ -102,3 +102,6 @@ beginning of aseq"
`(prerr ~@(cons (list 'quote prefix) (mapcat #(list (list 'quote %) "=" %)
(cons arg (seq more-args))))))
+;; Flush the pretty-print buffer without flushing the underlying stream
+(definterface PrettyFlush
+ (^void ppflush []))
@@ -10,7 +10,7 @@
(ns clojure.test-clojure.pprint
(:refer-clojure :exclude [format])
- (:use [clojure.test :only (deftest are run-tests)]
+ (:use [clojure.test :only (deftest is are run-tests)]
[clojure.test-helper :only [platform-newlines]]
clojure.test-clojure.pprint.test-helper
clojure.pprint))
@@ -326,3 +326,49 @@ It is implemented with a number of custom enlive templates.\"
"[1, 2, 3, 4, 5, 6]\n"
)
+(defn- flush-alerting-writer
+ [o]
+ (let [flush-count-atom (atom 0)]
+ [
+ (proxy [java.io.BufferedWriter] [o]
+ (flush []
+ (proxy-super flush)
+ (swap! flush-count-atom inc)))
+ flush-count-atom]))
+
+(deftest test-flush-underlying-prn
+ []
+ (let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter.))]
+ (binding [*out* out
+ *flush-on-newline* true]
+ (prn (range 50))
+ (prn (range 50)))
+ (is (= @flush-count-atom 2) "println flushes on newline")))
+
+(deftest test-flush-underlying-pprint
+ []
+ (let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter.))]
+ (binding [*out* out
+ *flush-on-newline* true]
+ (pprint (range 50))
+ (pprint (range 50)))
+ (is (= @flush-count-atom 2) "pprint flushes on newline")))
+
+(deftest test-noflush-underlying-prn
+ []
+ (let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter.))]
+ (binding [*out* out
+ *flush-on-newline* nil]
+ (prn (range 50))
+ (prn (range 50)))
+ (is (= @flush-count-atom 0) "println flushes on newline")))
+
+(deftest test-noflush-underlying-pprint
+ []
+ (let [[out flush-count-atom] (flush-alerting-writer (java.io.StringWriter.))]
+ (binding [*out* out
+ *flush-on-newline* nil]
+ (pprint (range 50))
+ (pprint (range 50)))
+ (is (= @flush-count-atom 0) "pprint flushes on newline")))
+

0 comments on commit 1588ff3

Please sign in to comment.