<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -45,15 +45,15 @@ In stack traces printed by &lt;code&gt;pst&lt;/code&gt;:
 * Elements are vertically aligned for better readability.
 * Printing is directed to &lt;code&gt;*out*&lt;/code&gt;.
 
-If you want to direct the printing to somewhere other than stdout, either dynamically rebind &lt;code&gt;*out*&lt;/code&gt; to the desired location or use &lt;code&gt;(pst-str e)&lt;/code&gt; to capture the stacktrace dump as a string.
+If you want to direct the printing to somewhere other than &lt;code&gt;*out*&lt;/code&gt;, either use &lt;code&gt;pst-on&lt;/code&gt; to specify the output location or &lt;code&gt;pst-str&lt;/code&gt; to capture the printing as a string.
 
 The library also offers an API for programatically 'parsing' exceptions. This API is used internal for &lt;code&gt;pst&lt;/code&gt; and can be used to e.g. improve development tools. Try for example:
 
 &lt;pre&gt;&lt;code&gt;
-=&gt; (use '(clj-stacktrace core repl))
-=&gt; (try (&quot;foo&quot;) 
+=&gt; (use '(clj-stacktrace core))
+=&gt; (try (nofn) 
      (catch Exception e 
-       (print-trace-elems (take 5 (:trace-elems (parse-exception e))))))
+       (prn (take 5 (:trace-elems (parse-exception e))))))
 &lt;/code&gt;&lt;/pre&gt;
 
 ---</diff>
      <filename>README.textile</filename>
    </modified>
    <modified>
      <diff>@@ -1,8 +1,6 @@
 (ns clj-stacktrace.repl
   (:use (clj-stacktrace core utils)))
 
-(def *use-color* false)
-
 (def color-codes
   {:red     &quot;\033[31m&quot;
    :green   &quot;\033[32m&quot;
@@ -12,31 +10,28 @@
    :cyan    &quot;\033[36m&quot;
    :default &quot;\033[39m&quot;})
 
-(defn with-color
-  &quot;If *use-color* is bound to logical truth, returns text surrounded by 
-  strings that will turn the text the given color and then reset the color
-  to default. Otherwise returns the given string.&quot;
-  [color text]
-  (if *use-color*
+(defn- colored
+  [color? color text]
+  (if color?
     (str (color-codes color) text (color-codes :default))
     text))
 
 (defn elem-color
   &quot;Returns a symbol identifying the color appropriate for the given trace elem.
   :green   All Java elems
-  :magenta Any fn in the user or repl* namespaces (i.e. entered at REPL)
+  :yellow  Any fn in the user or repl* namespaces (i.e. entered at REPL)
   :blue    Any fn in clojure.* (e.g. clojure.core, clojure.contrib.*)
-  :default Anything else - i.e. Clojure libraries and app code.&quot;
+  :magenta Anything else - i.e. Clojure libraries and app code.&quot;
   [elem]
   (cond
     (:java elem)
       :green
     (or (nil? (:ns elem)) (re-match? #&quot;^(user|repl)&quot; (:ns elem)))
-      :magenta
+      :yellow
     (re-match? #&quot;^clojure\.&quot; (:ns elem))
       :blue
     :else
-      :default))
+      :magenta))
 
 (defn source-str [parsed]
   (if (and (:file parsed) (:line parsed))
@@ -52,24 +47,30 @@
 (defn method-str [parsed]
   (if (:java parsed) (java-method-str parsed) (clojure-method-str parsed)))
 
-(defn print-trace-elems
-  &quot;Print a pretty stack trace for the parsed elems.&quot;
-  [parsed-elems &amp; [source-width]]
+(defn pst-message-on [on color? message]
+  (.append on (colored color? :red message))
+  (.append on &quot;\n&quot;)
+  (.flush on))
+
+(defn pst-elems-on
+  [on color? parsed-elems &amp; [source-width]]
   (let [print-width
           (+ 6 (or source-width
                    (high (map (memfn length) (map source-str parsed-elems)))))]
     (doseq [parsed-elem parsed-elems]
-      (println (with-color (elem-color parsed-elem)
-                 (str (rjust print-width (source-str parsed-elem))
-                      &quot; &quot; (method-str parsed-elem)))))))
+      (.append on
+        (colored color? (elem-color parsed-elem)
+          (str (rjust print-width (source-str parsed-elem))
+               &quot; &quot; (method-str parsed-elem))))
+      (.append on &quot;\n&quot;)
+      (.flush on))))
 
-(defn- pst-cause
-  &quot;Print a pretty stack trace for a parsed exception in a causal chain.&quot;
-  [exec source-width]
-  (println (with-color :red (str &quot;Caused by: &quot; (:message exec))))
-  (print-trace-elems (:trimmed-elems exec) source-width)
+(defn- pst-cause-on
+  [on color? exec source-width]
+  (pst-message-on on color? (str &quot;Caused by: &quot; (:message exec)))
+  (pst-elems-on on color? (:trimmed-elems exec) source-width)
   (if-let [cause (:cause exec)]
-    (pst-cause cause source-width)))
+    (pst-cause-on on color? cause source-width)))
 
 (defn- find-source-width
   &quot;Returns the width of the longest source-string among all trace elems of the 
@@ -81,23 +82,29 @@
         (max this-source-width (find-source-width cause))
         this-source-width)))
 
+(defn pst-on [on color? e]
+  &quot;Prints to the given Writer on a pretty stack trace for the given exception e,
+  ANSI colored if color? is true.&quot;
+  (let [exec         (parse-exception e)
+        source-width (find-source-width exec)]
+    (pst-message-on on color? (:message exec))
+    (pst-elems-on on color? (:trace-elems exec) source-width)
+    (if-let [cause (:cause exec)]
+      (pst-cause-on on color? cause source-width))))
+
 (defn pst
   &quot;Print to *out* a pretty stack trace for an exception, by default *e.&quot;
   [&amp; [e]]
-  (let [exec      (parse-exception (or e *e))
-        source-width (find-source-width exec)]
-    (println (with-color :red (:message exec)))
-    (print-trace-elems (:trace-elems exec) source-width)
-    (if-let [cause (:cause exec)]
-      (pst-cause cause source-width))))
+  (pst-on *out* false (or e *e)))
 
 (defn pst-str
   &quot;Like pst, but returns a string instead of printing that string to *out*&quot;
   [&amp; [e]]
-  (with-out-str (pst (or e *e))))
+  (let [sw (java.io.StringWriter.)]
+    (pst-on sw false (or e *e))
+    (str sw)))
 
 (defn pst+
-  &quot;Experimenal. Like pst, but with ANSI terminal color coding.&quot;
+  &quot;Like pst, but with ANSI terminal color coding.&quot;
   [&amp; [e]]
-  (binding [*use-color* true]
-    (pst e)))
+  (pst-on *out* true (or e *e)))</diff>
      <filename>src/clj_stacktrace/repl.clj</filename>
    </modified>
    <modified>
      <diff>@@ -43,4 +43,4 @@
   &quot;If width is greater than the length of s, returns a new string
   of length width with s right justified within it, otherwise returns s.&quot;
   [width s]
-  (format (str &quot;%&quot; width &quot;s&quot;) s))
\ No newline at end of file
+  (format (str &quot;%&quot; width &quot;s&quot;) s))</diff>
      <filename>src/clj_stacktrace/utils.clj</filename>
    </modified>
    <modified>
      <diff>@@ -1,6 +1,6 @@
 (ns clj-stacktrace.repl-test
   (:use (clj-unit core)
-        (clj-stacktrace repl)))
+        (clj-stacktrace repl utils)))
 
 (defmacro with-cascading-exception
   &quot;Execute body in the context of a variable bound to an exception instance
@@ -11,7 +11,13 @@
        (let [~binding-sym e#]
          ~@body))))
 
-(deftest &quot;pst, pst-str&quot;
+(deftest &quot;pst&quot;
+  (with-cascading-exception e
+    (assert-that (with-out-str (pst e)))
+    (binding [*e e]
+      (assert-that (with-out-str (pst))))))
+
+(deftest &quot;pst&quot;
   (with-cascading-exception e
     (assert-that (pst-str e))
     (binding [*e e]
@@ -19,8 +25,6 @@
 
 (deftest &quot;pst+&quot;
   (with-cascading-exception e
-    (assert-not=
-      (pst-str e)
-      (with-out-str (pst+ e)))
+    (assert-that (with-out-str (pst+ e)))
     (binding [*e e]
       (assert-that (with-out-str (pst+))))))
\ No newline at end of file</diff>
      <filename>test/clj_stacktrace/repl_test.clj</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>459f2e5ea1a190e4ff4df5659c5b61b1513a9a6e</id>
    </parent>
  </parents>
  <author>
    <name>Mark McGranaghan</name>
    <email>mmcgrana@gmail.com</email>
  </author>
  <url>http://github.com/mmcgrana/clj-stacktrace/commit/0e99eb63e7a8730fb20859cff305007be27bac8d</url>
  <id>0e99eb63e7a8730fb20859cff305007be27bac8d</id>
  <committed-date>2009-11-01T06:42:09-08:00</committed-date>
  <authored-date>2009-11-01T06:42:09-08:00</authored-date>
  <message>Explicitly pass output targets and coloring option for pretty printing.</message>
  <tree>f393a043571a33a365e1ff4ea5d0806af91048f1</tree>
  <committer>
    <name>Mark McGranaghan</name>
    <email>mmcgrana@gmail.com</email>
  </committer>
</commit>
