Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Binary file modified color-scheme.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 24 additions & 12 deletions src/lambdaisland/deep_diff2/printer_impl.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down