 (defn- color-text
   "Constructs a text doc, which may be colored if *colored-output* is true."
   [text & codes]
-  (if *colored-output*
+  (if (and *colored-output* (not (empty? codes)))
     [:span [:pass (ansi/esc codes)] text [:pass (ansi/escape :none)]]
     text))
 




;; DISPATCH MULTIMETHOD

(defmulti canonize
  "Converts the given value into a 'canonical' structured document, suitable
  for printing with fipp. This method also supports ANSI color escapes for
  syntax highlighting if desired."
  type)


(defmacro ^:private color-primitive

     (color-text (pr-str value#) ~@codes)))


(color-primitive nil :bold :black)
(color-primitive java.lang.Boolean :cyan)
(color-primitive java.lang.Number :green)
(color-primitive java.lang.Character :bold :magenta)
(color-primitive java.lang.String :bold :magenta)
(color-primitive clojure.lang.Keyword :bold :yellow)
(color-primitive clojure.lang.Symbol)


(defmethod canonize clojure.lang.ISeq
  [s]
  (let [elements (if (symbol? (first s))
                   (cons (color-text (str (first s)) :bold :blue)
                         (map canonize (rest s)))
                   (map canonize s))]
    [:group
     (delimiter "(")
     [:align (interpose :line elements)]
     (delimiter ")")]))


(defmethod canonize clojure.lang.IPersistentVector
  [v]
  [:group
   (delimiter "[")
   [:align (interpose :line (map canonize v))]
   (delimiter "]")])


(defmethod canonize clojure.lang.IPersistentSet
  [s]
  (let [entries (sort total-order (seq s))]
    [:group
     (delimiter "#{")
     [:align (interpose :line (map canonize entries))]
     (delimiter "}")]))


(defmethod canonize clojure.lang.IPersistentMap
  [m]
  (let [entries (sort-by first total-order (seq m))
        entries (for [[k v] entries]
                  [:span (canonize k) " " (canonize v)])]
    [:group
     (delimiter "{")
     [:align (interpose [:span "," :line] entries)]
     (delimiter "}")]))


(defmethod canonize :tagged-value
  [v]
  [:span (color-text (str \# (edn-tag v)) :red) " " (canonize (edn-value v))])


(defmethod canonize :default
  [value]
  [:span "<" (color-text (.getName (class value)) :green)
   (str \space (pr-str value) \>)])


(defprinter cprint canonize {:width 80})
