Deep-Symmetry/bytefield-svg

Support padding to a particular address

brunchboy opened this issue · 0 comments

@Swiftb0y had another good idea, which is to be able to draw enough padding to get to a particular address in the packet.

Here are a pair of functions that achieve that result. Either start a library of useful functions people can copy for their own shared include files, or more likely build these in and document them—I can see them being useful for many situations.

(defn next-address
  "Calculates the address at which the next box will be drawn."
  []
  (let [{:keys [address column]} @diagram-state]
    (+ address column)))

(defn draw-padding
  "Draws enough related boxes to reach the specified address. If no
  `label` is supplied, draws a zero byte in each box. If `attr-spec`
  is supplied, it is passed along to `draw-related-boxes` along with
  each copy of the label."
  ([address]
   (draw-padding address 0 nil))
  ([address label]
   (draw-padding address label nil))
  ([address label attr-spec]
   (draw-related-boxes (repeat (- address (next-address)) label) attr-spec)))