|
35 | 35 | (list 'get key))) |
36 | 36 | (conj path '<unknown>))) |
37 | 37 |
|
38 | | -(def ^:private supported-view-modes #{:normal :object :table :hex}) |
39 | | - |
40 | 38 | (def ^:private default-inspector-config |
41 | 39 | "Default configuration values for the inspector." |
42 | 40 | {:page-size 32 ; = Clojure's default chunked sequences chunk size. |
|
272 | 270 | (tap> (:value (get index idx))) |
273 | 271 | (inspect-render inspector)) |
274 | 272 |
|
275 | | -(defn set-view-mode |
276 | | - "Set the view mode for the current value to `mode`. See allowed values in |
277 | | - `supported-view-modes`." |
278 | | - [inspector mode] |
279 | | - (pre-ex (contains? supported-view-modes mode)) |
280 | | - (inspect-render (assoc inspector :view-mode mode))) |
281 | | - |
282 | 273 | (defn display-analytics |
283 | 274 | "Calculates and renders analytics for the current object." |
284 | 275 | [{:keys [analytics-size-cutoff value] :as inspector}] |
|
291 | 282 | (dissoc :display-analytics-hint)) |
292 | 283 | inspector))) |
293 | 284 |
|
| 285 | +;; View modes |
| 286 | + |
| 287 | +(def ^:private view-mode-order [:normal :hex :table :object]) |
| 288 | + |
| 289 | +(defmulti view-mode-supported? (fn [_inspector view-mode] view-mode)) |
| 290 | + |
| 291 | +(defmethod view-mode-supported? :normal [_ _] true) |
| 292 | + |
| 293 | +(defmethod view-mode-supported? :object [{:keys [value]} _] |
| 294 | + ;; A hack - for all "known" types `object-type` returns a keyword. If it's not |
| 295 | + ;; a keyword, it means we render it using object renderer, so :object |
| 296 | + ;; view-mode is redundant for it. |
| 297 | + (keyword? (object-type value))) |
| 298 | + |
| 299 | +(defmethod view-mode-supported? :table [{:keys [chunk value]} _] |
| 300 | + (let [chunk (or chunk value)] |
| 301 | + (and (#{:list :array} (object-type value)) |
| 302 | + (#{:list :array :map} (object-type (first chunk)))))) |
| 303 | + |
| 304 | +(defmethod view-mode-supported? :hex [{:keys [value]} _] |
| 305 | + (when-let [klass (class value)] |
| 306 | + (and (.isArray klass) |
| 307 | + (= (.getComponentType klass) Byte/TYPE)))) |
| 308 | + |
| 309 | +(defn set-view-mode |
| 310 | + "Set the view mode for the current value to `mode`." |
| 311 | + [inspector mode] |
| 312 | + (pre-ex (view-mode-supported? inspector mode)) |
| 313 | + (inspect-render (assoc inspector :view-mode mode))) |
| 314 | + |
| 315 | +(defn toggle-view-mode |
| 316 | + "Switch to the next supported view mode." |
| 317 | + [{:keys [view-mode] :as inspector}] |
| 318 | + (let [supported (filter #(view-mode-supported? inspector %) view-mode-order) |
| 319 | + transitions (zipmap supported (rest (cycle supported)))] |
| 320 | + (set-view-mode inspector (transitions view-mode)))) |
| 321 | + |
| 322 | +;; Rendering |
| 323 | + |
294 | 324 | (defn render-onto [inspector coll] |
295 | 325 | (letfn [(render-one [{:keys [rendered] :as inspector} val] |
296 | 326 | ;; Special case: fuse two last strings together. |
|
432 | 462 | inspector |
433 | 463 | mappable)) |
434 | 464 |
|
435 | | -(defn supports-table-view-mode? |
436 | | - "Return whether the inspected object can be rendered in :table view-mode." |
437 | | - [{:keys [chunk value] :as _inspector}] |
438 | | - (let [chunk (or chunk value)] |
439 | | - (and (#{:list :array} (object-type value)) |
440 | | - (#{:list :array :map} (object-type (first chunk)))))) |
441 | | - |
442 | 465 | (defn- render-chunk-as-table [inspector chunk idx-starts-from] |
443 | 466 | (let [m-i map-indexed |
444 | 467 | fst (first chunk) |
|
523 | 546 | (defn- render-items [inspector items map? start-idx mark-values?] |
524 | 547 | (if map? |
525 | 548 | (render-map-values inspector items mark-values?) |
526 | | - (if (and (= (:view-mode inspector) :table) (supports-table-view-mode? inspector)) |
| 549 | + (if (= (:view-mode inspector) :table) |
527 | 550 | (render-chunk-as-table inspector items start-idx) |
528 | 551 | (render-indexed-chunk inspector items start-idx mark-values?)))) |
529 | 552 |
|
|
1018 | 1041 |
|
1019 | 1042 | (defn render-view-mode [inspector] |
1020 | 1043 | (let [{:keys [view-mode pretty-print]} inspector |
1021 | | - view-mode-str (->> [(when-not (= view-mode :normal) |
1022 | | - (str view-mode)) |
1023 | | - (when pretty-print ":pretty")] |
1024 | | - (remove nil?) |
1025 | | - (str/join " "))] |
1026 | | - (if (str/blank? view-mode-str) |
1027 | | - inspector |
1028 | | - (-> (render-section-header inspector "View mode") |
1029 | | - (indent) |
1030 | | - (render-indent view-mode-str) |
1031 | | - (unindent))))) |
| 1044 | + supported (filter #(view-mode-supported? inspector %) view-mode-order) |
| 1045 | + add-circle #(if %2 (str "●" %1) %1) |
| 1046 | + view-mode-str (str (->> supported |
| 1047 | + (map #(add-circle (name %) (= % view-mode))) |
| 1048 | + (str/join " ")) |
| 1049 | + " " (add-circle "pretty" pretty-print))] |
| 1050 | + (-> (render-section-header inspector "View mode (press 'v' to cycle, 'P' to pretty-print)") |
| 1051 | + (indent) |
| 1052 | + (render-indent view-mode-str) |
| 1053 | + (unindent)))) |
1032 | 1054 |
|
1033 | 1055 | (defn inspect-render |
1034 | 1056 | ([{:keys [max-atom-length max-value-length max-coll-size max-nested-depth value pretty-print] |
|
0 commit comments