Skip to content

Commit 17da143

Browse files
[inspect] Rework view-mode toggling
1 parent 868047d commit 17da143

File tree

3 files changed

+300
-321
lines changed

3 files changed

+300
-321
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## master (unreleased)
44

55
- [#342](https://github.com/clojure-emacs/orchard/pull/342): Inspector: add hexdump view mode.
6+
- [#343](https://github.com/clojure-emacs/orchard/pull/343): Inspector: rework view-mode toggling.
67

78
## 0.34.3 (2025-04-28)
89

src/orchard/inspect.clj

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
(list 'get key)))
3636
(conj path '<unknown>)))
3737

38-
(def ^:private supported-view-modes #{:normal :object :table :hex})
39-
4038
(def ^:private default-inspector-config
4139
"Default configuration values for the inspector."
4240
{:page-size 32 ; = Clojure's default chunked sequences chunk size.
@@ -272,13 +270,6 @@
272270
(tap> (:value (get index idx)))
273271
(inspect-render inspector))
274272

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-
282273
(defn display-analytics
283274
"Calculates and renders analytics for the current object."
284275
[{:keys [analytics-size-cutoff value] :as inspector}]
@@ -291,6 +282,45 @@
291282
(dissoc :display-analytics-hint))
292283
inspector)))
293284

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+
294324
(defn render-onto [inspector coll]
295325
(letfn [(render-one [{:keys [rendered] :as inspector} val]
296326
;; Special case: fuse two last strings together.
@@ -432,13 +462,6 @@
432462
inspector
433463
mappable))
434464

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-
442465
(defn- render-chunk-as-table [inspector chunk idx-starts-from]
443466
(let [m-i map-indexed
444467
fst (first chunk)
@@ -523,7 +546,7 @@
523546
(defn- render-items [inspector items map? start-idx mark-values?]
524547
(if map?
525548
(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)
527550
(render-chunk-as-table inspector items start-idx)
528551
(render-indexed-chunk inspector items start-idx mark-values?))))
529552

@@ -1018,17 +1041,16 @@
10181041

10191042
(defn render-view-mode [inspector]
10201043
(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))))
10321054

10331055
(defn inspect-render
10341056
([{:keys [max-atom-length max-value-length max-coll-size max-nested-depth value pretty-print]

0 commit comments

Comments
 (0)