11# frozen_string_literal: true
22
33require "optparse"
4+ require "cgi/escape"
45
56module 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 )
0 commit comments