Skip to content

Commit cb8385f

Browse files
authored
Merge pull request #56 from lambdaisland/laurence/fix-bug-issue-41
fix the bug pointed out from issue 41 by Yuri Govorushchenko
2 parents 26ab48b + b18871d commit cb8385f

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,16 @@ In the Puget libray, 8-bit scheme is expressed via `[:fg-256 5 n]` where n is be
172172

173173
#### An example of customizing color
174174

175-
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`
175+
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`
176176

177177
```
178178
user=> (use 'lambdaisland.deep-diff2)
179179
nil
180-
user=> (def color-printer (printer {:color-scheme {:lambdaisland.deep-diff2.printer-impl/deletion [:bg-256 5 13]}}))
180+
user=> (def color-printer (printer {:color-scheme {:lambdaisland.deep-diff2/deletion [:bg-256 5 11]}}))
181181
#'user/color-printer
182182
user=> (pretty-print (diff {:a 1} {:b 2}) color-printer)
183183
{+:b 2, -:a 1}
184184
```
185-
186185
That results in the following highlighting:
187186
![screenshot showing color customization](color-scheme.png)
188187

color-scheme.png

-150 Bytes
Loading

src/lambdaisland/deep_diff2/printer_impl.cljc

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,25 +127,37 @@
127127
[type handler]
128128
(swap! print-handlers assoc type handler))
129129

130+
(defn- color-scheme-mapping
131+
"Translates user-friendly keys to internal namespaced keys."
132+
[colors]
133+
(let [mapping {:lambdaisland.deep-diff2/deletion ::deletion
134+
:lambdaisland.deep-diff2/insertion ::insertion
135+
:lambdaisland.deep-diff2/other ::other}]
136+
(reduce-kv (fn [m k v]
137+
(assoc m (get mapping k k) v)) ;; Fallback to original key if not in mapping
138+
{}
139+
colors)))
140+
130141
(defn puget-printer
131142
([]
132143
(puget-printer {}))
133144
([opts]
134-
(let [extra-handlers (:extra-handlers opts)]
135-
(puget-printer/pretty-printer (merge {:width (or *print-length* 100)
136-
:print-color true
137-
:color-scheme {::deletion [:red]
138-
::insertion [:green]
139-
::other [:yellow]
145+
(let [opts (update opts :color-scheme color-scheme-mapping)
146+
extra-handlers (:extra-handlers opts)]
147+
(puget-printer/pretty-printer (puget-printer/merge-options {:width (or *print-length* 100)
148+
:print-color true
149+
:color-scheme {::deletion [:red]
150+
::insertion [:green]
151+
::other [:yellow]
140152
;; lambdaisland.deep-diff2.puget uses green and red for
141153
;; boolean/tag, but we want to reserve
142154
;; those for diffed values.
143-
:boolean [:bold :cyan]
144-
:tag [:magenta]}
145-
:print-handlers (dispatch/chained-lookup
146-
(print-handler-resolver extra-handlers)
147-
puget-printer/common-handlers)}
148-
(dissoc opts :extra-handlers))))))
155+
:boolean [:bold :cyan]
156+
:tag [:magenta]}
157+
:print-handlers (dispatch/chained-lookup
158+
(print-handler-resolver extra-handlers)
159+
puget-printer/common-handlers)}
160+
(dissoc opts :extra-handlers))))))
149161

150162
(defn format-doc [expr printer]
151163
(puget-printer/format-doc printer expr))

0 commit comments

Comments
 (0)