Skip to content

Commit

Permalink
Fix *flush-on-newline* behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
eerohele committed Nov 25, 2023
1 parent c0e4d33 commit 1fa14fe
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

## UNRELEASED

- Fix `*flush-on-newline*` behavior

If `*flush-on-newline` is set to `true` (the default), like clojure.pprint, pp now only flushes after pretty-printing the entire form, instead of flushing after every newline.

- Add Maven (Clojars) release
- Add ClojureScript support
- Add explicit (unit tested) Babashka support
Expand Down
15 changes: 6 additions & 9 deletions src/me/flowthing/pp.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@
"Write a newline into the underlying java.io.Writer.
Resets the number of characters allotted to the current line to
zero.
If *flush-on-newline* is true, flushes the underlying writer."))
zero."))

(defn ^:private write-into
"Given a writer (java.io.Writer or cljs.core.IWriter) and a string,
Expand Down Expand Up @@ -103,10 +101,6 @@
(- max-width @c))
(nl [_]
(write-into writer "\n")

(when *flush-on-newline*
#?(:clj (.flush ^java.io.Writer writer) :cljs (-flush writer)))

(vreset! c 0)
nil))))

Expand Down Expand Up @@ -584,7 +578,9 @@
(do
(print-dup x writer)
(.write ^java.io.Writer writer "\n"))
(pp writer)))
(pp writer))

(when *flush-on-newline* (.flush ^java.io.Writer writer)))

:cljs
(if writer
Expand All @@ -598,4 +594,5 @@
(let [sb (goog.string.StringBuffer.)
writer (StringBufferWriter. sb)]
(pp writer)
(-> sb str string-print)))))))
(-> sb str string-print)
(when *flush-on-newline* (-flush writer))))))))
32 changes: 32 additions & 0 deletions test/me/flowthing/pp_flush_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
(ns me.flowthing.pp-flush-test
(:require [clojure.test :refer [deftest is testing]]
[me.flowthing.pp :as sut])
(:import (java.io BufferedWriter StringWriter)))

(defn ^:private flush-observing-writer
[writer]
(let [a (atom 0)]
[(proxy [BufferedWriter] [writer]
(flush []
(proxy-super flush)
(swap! a inc)))
a]))

(deftest flush-on-newline
(testing "Does not flush after printing if *flush-on-newline* is nil"
(let [sw (StringWriter.)
[writer a] (flush-observing-writer sw)]
(binding [*flush-on-newline* nil]
(sut/pprint writer {:a 1 :b 2 :c 3 :d 4} {:max-width 10}))
(is (= [0 ""] [@a (str sw)]))))

(testing "Only flushes after printing the entire top-level form if *flush-on-newline* is true"
;; So when pretty-printing, *flush-on-newline* doesn't actually flush on
;; newline, only after printing the entire top-level form.
(let [sw (StringWriter.)
[writer a] (flush-observing-writer sw)]
(binding [*flush-on-newline* true]
(sut/pprint writer {:a 1 :b 2 :c 3 :d 4} {:max-width 10})
(sut/pprint writer {:a 1 :b 2 :c 3 :d 4} {:max-width 10}))
(is (= [2 "{:a 1,\n :b 2,\n :c 3,\n :d 4}\n{:a 1,\n :b 2,\n :c 3,\n :d 4}\n"]
[@a (str sw)])))))

0 comments on commit 1fa14fe

Please sign in to comment.