diff --git a/README.md b/README.md index 6ee031a..f720de8 100644 --- a/README.md +++ b/README.md @@ -172,17 +172,16 @@ In the Puget libray, 8-bit scheme is expressed via `[:fg-256 5 n]` where n is be #### An example of customizing color -For example, if we change the `:lambdaisland.deep-diff2.printer-impl/deletion` from `[:red]` to `[:bg-256 5 13]`, the color code it outputs will change from `\u001b[31m` to `\u001b[48;5;13m` +For example, if we change the `:lambdaisland.deep-diff2.printer-impl/deletion` from `[:red]` to `[:bg-256 5 11]`, the color code it outputs will change from `\u001b[31m` to `\u001b[48;5;11m` ``` user=> (use 'lambdaisland.deep-diff2) nil -user=> (def color-printer (printer {:color-scheme {:lambdaisland.deep-diff2.printer-impl/deletion [:bg-256 5 13]}})) +user=> (def color-printer (printer {:color-scheme {:lambdaisland.deep-diff2/deletion [:bg-256 5 11]}})) #'user/color-printer user=> (pretty-print (diff {:a 1} {:b 2}) color-printer) {+:b 2, -:a 1} ``` - That results in the following highlighting: ![screenshot showing color customization](color-scheme.png) diff --git a/color-scheme.png b/color-scheme.png index bdd9f30..ebaf500 100644 Binary files a/color-scheme.png and b/color-scheme.png differ diff --git a/src/lambdaisland/deep_diff2/printer_impl.cljc b/src/lambdaisland/deep_diff2/printer_impl.cljc index 0f76acc..9c46687 100644 --- a/src/lambdaisland/deep_diff2/printer_impl.cljc +++ b/src/lambdaisland/deep_diff2/printer_impl.cljc @@ -127,25 +127,37 @@ [type handler] (swap! print-handlers assoc type handler)) +(defn- color-scheme-mapping + "Translates user-friendly keys to internal namespaced keys." + [colors] + (let [mapping {:lambdaisland.deep-diff2/deletion ::deletion + :lambdaisland.deep-diff2/insertion ::insertion + :lambdaisland.deep-diff2/other ::other}] + (reduce-kv (fn [m k v] + (assoc m (get mapping k k) v)) ;; Fallback to original key if not in mapping + {} + colors))) + (defn puget-printer ([] (puget-printer {})) ([opts] - (let [extra-handlers (:extra-handlers opts)] - (puget-printer/pretty-printer (merge {:width (or *print-length* 100) - :print-color true - :color-scheme {::deletion [:red] - ::insertion [:green] - ::other [:yellow] + (let [opts (update opts :color-scheme color-scheme-mapping) + extra-handlers (:extra-handlers opts)] + (puget-printer/pretty-printer (puget-printer/merge-options {:width (or *print-length* 100) + :print-color true + :color-scheme {::deletion [:red] + ::insertion [:green] + ::other [:yellow] ;; lambdaisland.deep-diff2.puget uses green and red for ;; boolean/tag, but we want to reserve ;; those for diffed values. - :boolean [:bold :cyan] - :tag [:magenta]} - :print-handlers (dispatch/chained-lookup - (print-handler-resolver extra-handlers) - puget-printer/common-handlers)} - (dissoc opts :extra-handlers)))))) + :boolean [:bold :cyan] + :tag [:magenta]} + :print-handlers (dispatch/chained-lookup + (print-handler-resolver extra-handlers) + puget-printer/common-handlers)} + (dissoc opts :extra-handlers)))))) (defn format-doc [expr printer] (puget-printer/format-doc printer expr))