Skip to content

Commit

Permalink
Update change log, hex-text, tweak example
Browse files Browse the repository at this point in the history
  • Loading branch information
brunchboy committed Aug 14, 2024
1 parent be9b6ff commit ca7cdb3
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 65 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ This change log follows the conventions of

## [Unreleased][unreleased]

Nothing so far.
### Added

- A nice examples section to the user guide, for showcasing community
contributions, was set up by
[@simonarnell](https://github.com/simonarnell), starting with
content submitted a while ago by
[@lorrden](https://github.com/lorrden), which I had never found the
time to review and incorporate.

## [1.10.0] - 2024-08-11

Expand Down
4 changes: 2 additions & 2 deletions doc/modules/ROOT/examples/udp.edn
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(def column-labels (mapv #(number-as-hex % 2) (range 32))) ;; creates column labels 0 .. 1f
(def column-labels (mapv #(number-as-hex % 2) (range 32))) ; 00..1f
(def boxes-per-row 32)
(draw-column-headers)
(draw-box "Source port" {:span 16})
(draw-box "Destination port" {:span 16})
(draw-box "Length" {:span 16})
(draw-box "Checksum" {:span 16})
(draw-box "Checksum" {:span 16})
123 changes: 61 additions & 62 deletions src/org/deepsymmetry/bytefield/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,36 @@
(update :y + height)
(assoc :column 0))))))

(defn char->int
"Yields the UTF-16 code unit of a character. E.g. `(char->int \\A) ;; => 65`."
[char]
(.charCodeAt ^String char))

(defn normalize-bit
"Converts a value to either `true` or `false`. All non-zero numbers
become `true`. Other values are tested for truthiness and translated
to `true` or `false` accordingly."
[value]
(cond
(number? value)
(zero? value)

:else
(if value true false)))

(defn number-as-bits
"Takes a number and transforms it into a sequence of `boolean` bit
values of the specified length."
[number length]
(map #(normalize-bit (bit-test number %)) (reverse (range length))))

(defn number-as-hex
"Takes a number and transforms it into a hexadecimal value of the specified
length."
[number length]
(let [fmt (str "~" length ",'0x")]
(pprint/cl-format nil fmt number)))

(defn hex-text
"Formats a number as an SVG text object containing a hexadecimal
string with the specified number of digits (defaults to 2 if no
Expand All @@ -274,8 +304,7 @@
([n length]
(hex-text n length :hex))
([n length attr-spec]
(let [fmt (str "~" length ",'0x")]
(text (pprint/cl-format nil fmt n) [:hex attr-spec]))))
(text (number-as-hex n length) [:hex attr-spec])))

(defn format-box-label
"Builds an appropriate SVG text object to label a box spanning the
Expand Down Expand Up @@ -303,36 +332,6 @@
:else
(throw (js/Error. (str "Don't know how to format box label: " label)))))

(defn char->int
"Yields the UTF-16 code unit of a character. E.g. `(char->int \\A) ;; => 65`."
[char]
(.charCodeAt ^String char))

(defn normalize-bit
"Converts a value to either `true` or `false`. All non-zero numbers
become `true`. Other values are tested for truthiness and translated
to `true` or `false` accordingly."
[value]
(cond
(number? value)
(zero? value)

:else
(if value true false)))

(defn number-as-bits
"Takes a number and transforms it into a sequence of `boolean` bit
values of the specified length."
[number length]
(map #(normalize-bit (bit-test number %)) (reverse (range length))))

(defn number-as-hex
"Takes a number and transforms it into a hexadecimal value of the specified
length."
[number length]
(let [fmt (str "~" length ",'0x")]
(pprint/cl-format nil fmt number)))

(defn- center-baseline
"Recursively ensures that the a tag and any content tags it contains
have their dominant baseline set to center them vertically, so
Expand Down Expand Up @@ -531,20 +530,20 @@
(when fill
(append-svg (svg/polygon (concat top-left top-right bottom-right bottom-left)
:fill fill)))
(if (#{:trapezoid-type/upper :trapezoid-type/lower} trapezoid-type)
[(apply draw-line (concat top-left bottom-left))
(apply draw-line (concat top-right bottom-right))
(if (= trapezoid-type :trapezoid-type/upper)
(apply draw-line (concat bottom-left bottom-right [gap-style]))
(when (= trapezoid-type :trapezoid-type/lower)
(apply draw-line (concat top-left top-right [gap-style]))))]
(when (#{:trapezoid-type/left :trapezoid-type/right} trapezoid-type)
[(apply draw-line (concat top-left top-right))
(apply draw-line (concat bottom-left bottom-right))
(if (= trapezoid-type :trapezoid-type/left)
(apply draw-line (concat top-right bottom-right [gap-style]))
(when (= trapezoid-type :trapezoid-type/right)
(apply draw-line (concat top-left bottom-left [gap-style]))))]))))
(if (#{:trapezoid-type/upper :trapezoid-type/lower} trapezoid-type)
[(apply draw-line (concat top-left bottom-left))
(apply draw-line (concat top-right bottom-right))
(if (= trapezoid-type :trapezoid-type/upper)
(apply draw-line (concat bottom-left bottom-right [gap-style]))
(when (= trapezoid-type :trapezoid-type/lower)
(apply draw-line (concat top-left top-right [gap-style]))))]
(when (#{:trapezoid-type/left :trapezoid-type/right} trapezoid-type)
[(apply draw-line (concat top-left top-right))
(apply draw-line (concat bottom-left bottom-right))
(if (= trapezoid-type :trapezoid-type/left)
(apply draw-line (concat top-right bottom-right [gap-style]))
(when (= trapezoid-type :trapezoid-type/right)
(apply draw-line (concat top-left bottom-left [gap-style]))))]))))

(defn draw-gap
"Draws an indication of discontinuity. Takes a full row, the default
Expand Down Expand Up @@ -612,20 +611,20 @@
:top-right [right bottom]
:bottom-left [left (+ y height)]
:bottom-right [right (+ y height)]}]
(draw-gap-trapezoid (upper-trapezoid :top-left)
(upper-trapezoid :top-right)
(upper-trapezoid :bottom-right)
(upper-trapezoid :bottom-left)
:trapezoid-type/upper
fill
gap-style)
(draw-gap-trapezoid (lower-trapezoid :top-left)
(lower-trapezoid :top-right)
(lower-trapezoid :bottom-right)
(lower-trapezoid :bottom-left)
:trapezoid-type/lower
fill
gap-style))
(draw-gap-trapezoid (upper-trapezoid :top-left)
(upper-trapezoid :top-right)
(upper-trapezoid :bottom-right)
(upper-trapezoid :bottom-left)
:trapezoid-type/upper
fill
gap-style)
(draw-gap-trapezoid (lower-trapezoid :top-left)
(lower-trapezoid :top-right)
(lower-trapezoid :bottom-right)
(lower-trapezoid :bottom-left)
:trapezoid-type/lower
fill
gap-style))
(let [state (swap! @('diagram-state @*globals*)
(fn [current]
(-> current
Expand Down Expand Up @@ -912,8 +911,8 @@
"Creates the sci vars to populate the symbol table for the
interpreter."
[]
(reduce (fn [acc [k v]]
(assoc acc k (sci/new-var k v)))
(reduce (fn [acc [k v]]
(assoc acc k (sci/new-var k v)))
{}
(build-initial-globals)))

Expand Down

0 comments on commit ca7cdb3

Please sign in to comment.