Skip to content

Commit cf1aa5f

Browse files
[inspect] Enable hex-mode for byte arrays by default
1 parent 93e8b93 commit cf1aa5f

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

src/orchard/inspect.clj

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
;; Navigating Inspector State
2424
;;
2525

26-
(declare inspect-render)
26+
(declare inspect-render supported-view-modes)
2727

2828
(defn push-item-to-path
2929
"Takes `path` and the role and key of the value to be navigated to, and returns
@@ -177,16 +177,16 @@
177177
;; :current-page may be wrong, recompute it.
178178
current-page (if (number? child-key)
179179
(quot child-key page-size)
180-
current-page)]
181-
(-> inspector
182-
(assoc :value child)
183-
(dissoc :value-analysis)
184-
(update :stack conj value)
185-
(assoc :current-page 0)
186-
(update :pages-stack conj current-page)
187-
(assoc :view-mode :normal)
188-
(update :view-modes-stack conj view-mode)
189-
(update :path push-item-to-path child-role child-key))))
180+
current-page)
181+
ins (-> inspector
182+
(assoc :value child)
183+
(dissoc :value-analysis)
184+
(update :stack conj value)
185+
(assoc :current-page 0)
186+
(update :pages-stack conj current-page)
187+
(update :view-modes-stack conj view-mode)
188+
(update :path push-item-to-path child-role child-key))]
189+
(assoc ins :view-mode (first (supported-view-modes ins)))))
190190

191191
(defn down
192192
"Drill down to an indexed object referred to by the previously rendered value."
@@ -290,7 +290,7 @@
290290

291291
;; View modes
292292

293-
(def ^:private view-mode-order [:normal :hex :table :object])
293+
(def ^:private view-mode-order [:hex :normal :table :object])
294294

295295
(defmulti view-mode-supported? (fn [_inspector view-mode] view-mode))
296296

@@ -318,10 +318,13 @@
318318
(pre-ex (view-mode-supported? inspector mode))
319319
(inspect-render (assoc inspector :view-mode mode)))
320320

321+
(defn- supported-view-modes [inspector]
322+
(filter #(view-mode-supported? inspector %) view-mode-order))
323+
321324
(defn toggle-view-mode
322325
"Switch to the next supported view mode."
323326
[{:keys [view-mode] :as inspector}]
324-
(let [supported (filter #(view-mode-supported? inspector %) view-mode-order)
327+
(let [supported (supported-view-modes inspector)
325328
transitions (zipmap supported (rest (cycle supported)))]
326329
(set-view-mode inspector (transitions view-mode))))
327330

@@ -1116,11 +1119,13 @@
11161119
of supported keys."
11171120
([value] (start {} value))
11181121
([config value]
1119-
(-> default-inspector-config
1120-
(merge (validate-config config))
1121-
(assoc :stack [], :path [], :pages-stack [], :current-page 0,
1122-
:view-modes-stack [], :view-mode :normal, :value value)
1123-
(inspect-render))))
1122+
(let [inspector (-> default-inspector-config
1123+
(merge (validate-config config))
1124+
(assoc :stack [], :path [], :pages-stack [], :current-page 0,
1125+
:view-modes-stack [], :value value))]
1126+
(-> inspector
1127+
(assoc :view-mode (first (supported-view-modes inspector)))
1128+
inspect-render))))
11241129

11251130
(defn ^:deprecated clear
11261131
"If necessary, use `(start inspector nil) instead.`"

test/orchard/inspect_test.clj

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@
560560
:value))))
561561
(testing "sibling functions work with arrays"
562562
(is+ {:value 35, :pages-stack [1], :path '[(nth 35)]}
563-
(-> (byte-array (range 40))
563+
(-> (long-array (range 40))
564564
inspect
565565
(inspect/down 33)
566566
(inspect/next-sibling)
@@ -1527,7 +1527,7 @@
15271527
" 0x00000050 │ 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f │ PQRSTUVWXYZ[\\]^_" [:newline]
15281528
" 0x00000060 │ 60 61 62 63 │ `abc"]
15291529
(contents-section rendered))
1530-
(is+ [#"--- View mode" [:newline] " normal ●hex object pretty"]
1530+
(is+ [#"--- View mode" [:newline] " ●hex normal object pretty"]
15311531
(section rendered "View mode"))))
15321532

15331533
(testing "works with paging"
@@ -1553,7 +1553,25 @@
15531553
(set-page-size 2)
15541554
inspect/next-page
15551555
render
1556-
contents-section))))
1556+
contents-section))
1557+
1558+
(testing "enabled by default for byte arrays"
1559+
(is+ (matchers/prefix
1560+
["--- Contents:" [:newline]
1561+
" 0x00000000 │ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │ ················"])
1562+
(-> (byte-array (range 100))
1563+
inspect
1564+
render
1565+
contents-section))
1566+
1567+
(is+ (matchers/prefix
1568+
["--- Contents:" [:newline]
1569+
" 0x00000000 │ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │ ················"])
1570+
(-> [(byte-array (range 100))]
1571+
inspect
1572+
(inspect/down 1)
1573+
render
1574+
contents-section)))))
15571575

15581576
(deftest toggle-view-mode-test
15591577
(is+ :normal (-> (repeat 10 [1 2]) inspect :view-mode))

0 commit comments

Comments
 (0)