Skip to content

Commit fd284c2

Browse files
committed
Show added and removed wires in HTML diff
1 parent d52bcb2 commit fd284c2

5 files changed

Lines changed: 89 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Draw model-specific seven-segment displays and RGB LEDs from the expanded core part catalog.
1313
- Add `--focus` and `--highlight-net` to emphasize selected components and nets.
1414
- Add a standalone HTML viewer with zoom, pan, layer controls, and net hover.
15+
- Add `--diff OLD NEW` with colored added and removed wires in a two-panel HTML viewer.
1516

1617
## 0.1.0 — 2026-09-27
1718

‎README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Choose a theme, add net labels, or export a raster image:
6363
bkrender circuit.bk.rb -o circuit.svg --theme dark --show-nets --legend
6464
bkrender circuit.bk.rb -o circuit.png --scale 3
6565
bkrender circuit.bk.rb -o circuit.html --theme dark
66+
bkrender --diff before.bk.rb after.bk.rb -o changes.html
6667
bkrender circuit.bk.rb --format svg > circuit.svg
6768
```
6869

@@ -91,6 +92,9 @@ PNG and JPEG need a raster backend; SVG works without one.
9192
`--highlight-net VCC` emphasizes the selected net and its connected parts.
9293
- HTML output provides a standalone viewer with zoom, pan, layer controls, and
9394
net highlighting on hover.
95+
- `--diff OLD NEW` creates a two-panel HTML viewer. Removed wires are red in
96+
the old view and added wires are green in the new view; matching wires use
97+
their normal colors.
9498

9599
The demo was generated with:
96100

@@ -117,6 +121,7 @@ bkrender ../breadkit/examples/05_sensor_demo.bk.rb \
117121
| `--layer NAME` | all layers | Render one named layer, including in PNG and JPEG output. |
118122
| `--focus REF` | none | Emphasize one component and its connected wires. |
119123
| `--highlight-net NAME` | none | Emphasize one net and its connected components. |
124+
| `--diff` | off | Compare two circuit files in a standalone HTML viewer. |
120125
| `--backend NAME` | `auto` | Use `rsvg`, `vips`, or `magick` for raster output. |
121126
| `--background COLOR` | white | Set the JPEG background with a CSS name, `#RGB`, or `#RRGGBB`; PNG and SVG reject it. |
122127
| `--quality N` | `90` | JPEG quality. |

‎lib/breadkit/render/cli.rb‎

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "optparse"
4+
require "cgi/escape"
45

56
module Breadkit
67
module Render
@@ -24,6 +25,7 @@ def run(argv)
2425
opts.on("--layer NAME") { |value| options[:layer] = value }
2526
opts.on("--focus REF") { |value| options[:focus] = value }
2627
opts.on("--highlight-net NAME") { |value| options[:highlight_net] = value }
28+
opts.on("--diff") { options[:diff] = true }
2729
opts.on("--backend NAME", %w[auto rsvg vips magick]) { |value| options[:backend] = value }
2830
opts.on("--background COLOR") { |value| options[:background] = value }
2931
opts.on("--static") { options[:static] = true }
@@ -36,10 +38,13 @@ def run(argv)
3638
parser.parse!(argv)
3739
input = argv.shift
3840
raise ArgumentError, "input file required\n#{parser}" unless input
41+
second_input = argv.shift if options[:diff]
42+
raise ArgumentError, "--diff requires OLD and NEW inputs" if options[:diff] && !second_input
3943
raise ArgumentError, "unexpected arguments: #{argv.join(' ')}" unless argv.empty?
4044
format = output_format(options)
4145
raise ArgumentError, "--background is only supported for JPEG" if options[:background] && format != "jpeg"
4246
raise ArgumentError, "cannot write binary image data to a terminal; use -o PATH" if !%w[svg html].include?(format) && !options[:output] && $stdout.tty?
47+
return render_diff(input, second_input, options, format) if options[:diff]
4348
circuit = Breadkit.load(input)
4449
circuit.diagnostics.each do |item|
4550
location = [item.location&.path, item.location&.line].compact.join(":")
@@ -82,7 +87,66 @@ def output_format(options)
8287
if options[:format] && from_path && options[:format] != from_path
8388
raise ArgumentError, "--format conflicts with output extension"
8489
end
85-
options[:format] || from_path || "svg"
90+
options[:format] || from_path || (options[:diff] ? "html" : "svg")
91+
end
92+
93+
def render_diff(before_path, after_path, options, format)
94+
raise ArgumentError, "--diff requires HTML output" unless format == "html"
95+
raise ArgumentError, "--diff does not support annotations, state, layer, or focus selection" if options.values_at(:annotations, :state, :layer, :focus, :highlight_net).any?
96+
97+
before, after = [before_path, after_path].map { |path| Breadkit.load(path) }
98+
[before, after].zip([before_path, after_path]).each do |circuit, path|
99+
circuit.diagnostics.each { |item| warn "#{path}: #{item.severity || 'error'}: #{item.message}" }
100+
end
101+
return 1 if !options[:force] && [before, after].any? { |circuit| circuit.diagnostics.any? { |item| !%w[warning info].include?(item.severity) } }
102+
103+
removed = unmatched_wires(before, after).to_h { |id| [id, "removed"] }
104+
added = unmatched_wires(after, before).to_h { |id| [id, "added"] }
105+
render_options = { crop: options[:crop], theme: options[:theme], orientation: options[:orientation],
106+
show_nets: options[:show_nets], legend: options[:legend], color_by: options[:color_by],
107+
rail_pattern: options[:rail_pattern], interactive_layers: !options[:static] }
108+
old_svg = SvgRenderer.new.render(before, **render_options, diff_wires: removed)
109+
new_svg = SvgRenderer.new.render(after, **render_options, diff_wires: added)
110+
html = diff_viewer(old_svg, new_svg, theme: options[:theme])
111+
options[:output] ? File.binwrite(options[:output], html) : $stdout.write(html)
112+
0
113+
end
114+
115+
def unmatched_wires(first, second)
116+
remaining = second.wires.map { |wire| wire_signature(wire) }.tally
117+
first.wires.filter_map do |wire|
118+
signature = wire_signature(wire)
119+
if remaining.fetch(signature, 0).positive?
120+
remaining[signature] -= 1
121+
nil
122+
else
123+
wire.id
124+
end
125+
end
126+
end
127+
128+
def wire_signature(wire)
129+
[*([wire.from, wire.to].sort), wire.color, wire.route, wire.layer, wire.electrical, wire.dashed]
130+
end
131+
132+
def diff_viewer(old_svg, new_svg, theme:)
133+
background = theme == "dark" ? "#151d19" : "#f1f4f1"
134+
foreground = theme == "dark" ? "#ecf3ee" : "#203029"
135+
before = CGI.escapeHTML(html_viewer(old_svg, theme: theme))
136+
after = CGI.escapeHTML(html_viewer(new_svg, theme: theme))
137+
<<~HTML
138+
<!doctype html>
139+
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
140+
<title>Breadkit wiring diff</title><style>
141+
*{box-sizing:border-box}body{margin:0;background:#{background};color:#{foreground};font:14px system-ui,sans-serif}
142+
main{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:8px;padding:8px}section{min-width:0}
143+
h2{margin:0 0 8px;font-size:14px;font-weight:600}iframe{width:100%;height:calc(100vh - 48px);border:1px solid currentColor;border-radius:8px}
144+
@media(max-width:700px){main{grid-template-columns:1fr}iframe{height:70vh}}
145+
</style></head><body><main>
146+
<section><h2>Before · removed wires in red</h2><iframe title="Before" sandbox="allow-scripts" srcdoc="#{before}"></iframe></section>
147+
<section><h2>After · added wires in green</h2><iframe title="After" sandbox="allow-scripts" srcdoc="#{after}"></iframe></section>
148+
</main></body></html>
149+
HTML
86150
end
87151

88152
def read_annotations(path, input)

‎lib/breadkit/render/svg_renderer.rb‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ class SvgRenderer
5656
module_border: "#666666", muted: "#777777", accent: "#333333" }
5757
}.freeze
5858

59-
def render(circuit, crop: "auto", theme: "light", orientation: "portrait", show_nets: false, legend: false, color_by: "wire", annotations: [], rail_pattern: nil, interactive_layers: true, state: nil, active_layer: nil, focus: nil, highlight_net: nil)
59+
def render(circuit, crop: "auto", theme: "light", orientation: "portrait", show_nets: false, legend: false, color_by: "wire", annotations: [], rail_pattern: nil, interactive_layers: true, state: nil, active_layer: nil, focus: nil, highlight_net: nil, diff_wires: {})
6060
@circuit, @theme, @orientation, @show_nets, @legend_enabled, @color_by, @annotations = circuit, theme.to_s, orientation.to_s, show_nets, legend, color_by, annotations
6161
@state, @active_layer = state, active_layer&.to_s
62+
@diff_wires = diff_wires
6263
@nets = circuit.nets(state)
6364
@focus_ref = focus&.to_s
6465
raise ArgumentError, "unknown component: #{@focus_ref}" if @focus_ref && !circuit.components.key?(@focus_ref)
@@ -546,6 +547,7 @@ def wires_svg
546547
from, to = endpoint_point(wire.from), endpoint_point(wire.to)
547548
next unless from && to
548549
color = @theme == "print" ? "#222222" : render_color(wire.color || wire_color(wire), "wire #{wire.id}") { wire_color(wire) }
550+
color = { "removed" => "#d94c48", "added" => "#23a86b" }.fetch(@diff_wires[wire.id], color)
549551
path = if wire.route == "arc"
550552
control_y = (py(from[1]) + py(to[1])) / 2.0 - 8
551553
"M #{fmt(px(from[0]))} #{fmt(py(from[1]))} Q #{fmt((px(from[0]) + px(to[0])) / 2)} #{fmt(control_y)} #{fmt(px(to[0]))} #{fmt(py(to[1]))}"
@@ -563,7 +565,8 @@ def wires_svg
563565
end
564566
casing = "<path d=\"#{path}\" fill=\"none\" stroke=\"#{@colors[:board]}\" stroke-width=\"3.6\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/>"
565567
net_attr = @nets_by_member[wire.id] ? " data-net=\"#{escape(@nets_by_member[wire.id].name)}\"" : ""
566-
wire_svg = "<path d=\"#{path}\" fill=\"none\" stroke=\"#{escape(color)}\" stroke-width=\"2.4\" stroke-linecap=\"round\" stroke-linejoin=\"round\" data-ref=\"#{escape(wire.id)}\"#{net_attr}#{dash}/>"
568+
diff_attr = @diff_wires[wire.id] ? " data-diff=\"#{@diff_wires[wire.id]}\"" : ""
569+
wire_svg = "<path d=\"#{path}\" fill=\"none\" stroke=\"#{escape(color)}\" stroke-width=\"2.4\" stroke-linecap=\"round\" stroke-linejoin=\"round\" data-ref=\"#{escape(wire.id)}\"#{net_attr}#{diff_attr}#{dash}/>"
567570
dots = [from, to].uniq.map do |x, y|
568571
circle(px(x), py(y), 2.8, fill: color, stroke: @colors[:board], stroke_width: 1.2,
569572
data_wire: wire.id)

‎spec/breadkit/render_spec.rb‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,19 @@
531531
end
532532
end
533533

534+
it "renders changed wires in a side-by-side diff viewer" do
535+
Dir.mktmpdir do |directory|
536+
before = File.join(directory, "before.bk.rb")
537+
after = File.join(directory, "after.bk.rb")
538+
output = File.join(directory, "diff.html")
539+
File.write(before, 'board :half; wire "a1", "a2"')
540+
File.write(after, 'board :half; wire "a3", "a4"')
541+
expect(described_class.new.run(["--diff", before, after, "-o", output])).to eq(0)
542+
html = File.read(output)
543+
expect(html).to include("Before", "After", "data-diff=&quot;removed&quot;", "data-diff=&quot;added&quot;")
544+
end
545+
end
546+
534547
it "passes the selected rail pattern through to SVG output" do
535548
input = File.expand_path("../../../breadkit/examples/01_led_button.bk.rb", __dir__)
536549
Dir.mktmpdir do |directory|

0 commit comments

Comments
 (0)