Skip to content
Snippets Groups Projects
Unverified Commit 68a15a35 authored by Cam Saul's avatar Cam Saul Committed by GitHub
Browse files

Support NO_COLOR env var. pprint-to-str perf improvements (#11950)

parent 972435fb
Branches
Tags
No related merge requests found
......@@ -207,28 +207,33 @@
identity
(constantly "")))
(def ^:private ^{:arglists '([color-symb x])} colorize
"Colorize string `x` with the function matching `color` symbol or keyword, but only if `MB_COLORIZE_LOGS` is
enabled (the default)."
(if (config/config-bool :mb-colorize-logs)
(def ^:private colorize?
;; As of 0.35.0 we support the NO_COLOR env var. See https://no-color.org/ (But who hates color logs?)
(if (config/config-str :no-color)
false
(config/config-bool :mb-colorize-logs)))
(def ^{:arglists '(^String [color-symb x])} colorize
"Colorize string `x` using `color`, a symbol or keyword, but only if `MB_COLORIZE_LOGS` is enabled (the default).
`color` can be `green`, `red`, `yellow`, `blue`, `cyan`, `magenta`, etc. See the entire list of avaliable
colors [here](https://github.com/ibdknox/colorize/blob/master/src/colorize/core.clj)"
(if colorize?
(fn [color x]
(colorize/color (keyword color) x))
(colorize/color (keyword color) (str x)))
(fn [_ x]
x)))
(str x))))
(defn format-color
"Like `format`, but colorizes the output. `color` should be a symbol or keyword like `green`, `red`, `yellow`, `blue`,
`cyan`, `magenta`, etc. See the entire list of avaliable
colors [here](https://github.com/ibdknox/colorize/blob/master/src/colorize/core.clj).
"With one arg, converts something to a string and colorizes it. With two args, behaves like `format`, but colorizes
the output.
(format-color :red \"Fatal error: %s\" error-message)"
{:style/indent 2}
(format-color :red \"%d cans\" 2)"
{:arglists '(^String [color x] ^String [color format-string & args]), :style/indent 2}
(^String [color x]
{:pre [((some-fn symbol? keyword?) color)]}
(colorize color (str x)))
(colorize color x))
(^String [color format-string & args]
(colorize color (apply format (str format-string) args))))
(^String [color format-str & args]
(colorize color (apply format format-str args))))
(defn pprint-to-str
"Returns the output of pretty-printing `x` as a string.
......@@ -239,7 +244,10 @@
{:style/indent 1}
(^String [x]
(when x
(with-out-str (pprint x))))
(with-open [w (java.io.StringWriter.)]
(pprint x w)
(str w))))
(^String [color-symb x]
(colorize color-symb (pprint-to-str x))))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment