Skip to content

Commit 9f7bfed

Browse files
committed
Change show(::MatchedSystemStructure) to use fewer colors.
This includes the following changes for `Base.show(::IO, ::MatchedSystemStructure)`: - var ⇔ eq matches are marked with underline (previously, yellow) - (un)solvable vars/eqs are printed in dark gray and white, respectively (previously, white/yellow) - the ∫ symbol is now printed without coloring - A separator has been added for the var/eq sides of the table These changes are made to encode less semantic information using colors, so that can colors instead be used to present a unified "diff" view of these objects. The new format is arguably more boring, but IMO is also easier to scan for information.
1 parent db52605 commit 9f7bfed

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

src/bipartite_graph.jl

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -212,21 +212,21 @@ end
212212

213213
struct HighlightInt
214214
i::Int
215-
highlight::Union{Symbol, Nothing}
215+
highlight::Symbol
216+
underline::Bool
216217
end
217218
Base.typeinfo_implicit(::Type{HighlightInt}) = true
218-
219219
function Base.show(io::IO, hi::HighlightInt)
220-
if hi.highlight !== nothing
221-
printstyled(io, hi.i, color = hi.highlight)
220+
if hi.underline
221+
printstyled(io, hi.i, color = hi.highlight, underline = true)
222222
else
223-
print(io, hi.i)
223+
printstyled(io, hi.i, color = hi.highlight)
224224
end
225225
end
226226

227227
function Base.show(io::IO, l::BipartiteAdjacencyList)
228228
if l.match === true
229-
printstyled(io, "", color = :light_blue, bold = true)
229+
printstyled(io, "")
230230
end
231231
if l.u === nothing
232232
printstyled(io, '', color = :light_black)
@@ -238,26 +238,30 @@ function Base.show(io::IO, l::BipartiteAdjacencyList)
238238
match = l.match
239239
isa(match, Bool) && (match = unassigned)
240240
function choose_color(i)
241-
i in l.highligh_u ? (i == match ? :light_yellow : :green) :
242-
(i == match ? :yellow : nothing)
241+
i in l.highligh_u ? :default : :light_black
243242
end
244243
if !isempty(setdiff(l.highligh_u, l.u))
245244
# Only for debugging, shouldn't happen in practice
246-
print(io, map(union(l.u, l.highligh_u)) do i
247-
HighlightInt(i, !(i in l.u) ? :light_red : choose_color(i))
245+
print(io,
246+
map(union(l.u, l.highligh_u)) do i
247+
HighlightInt(i, !(i in l.u) ? :light_red : choose_color(i),
248+
i == match)
248249
end)
249250
else
250251
print(io, map(l.u) do i
251-
HighlightInt(i, choose_color(i))
252+
HighlightInt(i, choose_color(i), i == match)
252253
end)
253254
end
254255
end
255256
end
256257

257258
struct Label
258259
s::String
260+
c::Symbol
259261
end
260-
Base.show(io::IO, l::Label) = print(io, l.s)
262+
Label(s::AbstractString) = Label(s, :nothing)
263+
Label(x::Integer) = Label(string(x))
264+
Base.show(io::IO, l::Label) = printstyled(io, l.s, color = l.c)
261265

262266
struct BipartiteGraphPrintMatrix <:
263267
AbstractMatrix{Union{Label, Int, BipartiteAdjacencyList}}

src/systems/systemstructure.jl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import ..ModelingToolkit: isdiffeq, var_from_nested_derivative, vars!, flatten,
1212
equations, isirreducible, input_timedomain, TimeDomain,
1313
VariableType, getvariabletype
1414
using ..BipartiteGraphs
15-
import ..BipartiteGraphs: invview, complete
15+
import ..BipartiteGraphs: invview, complete, HighlightInt
1616
using Graphs
1717
using UnPack
1818
using Setfield
@@ -386,14 +386,14 @@ end
386386

387387
using .BipartiteGraphs: Label, BipartiteAdjacencyList
388388
struct SystemStructurePrintMatrix <:
389-
AbstractMatrix{Union{Label, Int, BipartiteAdjacencyList}}
389+
AbstractMatrix{Union{Label, BipartiteAdjacencyList}}
390390
bpg::BipartiteGraph
391391
highlight_graph::BipartiteGraph
392392
var_to_diff::DiffGraph
393393
eq_to_diff::DiffGraph
394394
var_eq_matching::Union{Matching, Nothing}
395395
end
396-
Base.size(bgpm::SystemStructurePrintMatrix) = (max(nsrcs(bgpm.bpg), ndsts(bgpm.bpg)) + 1, 5)
396+
Base.size(bgpm::SystemStructurePrintMatrix) = (max(nsrcs(bgpm.bpg), ndsts(bgpm.bpg)) + 1, 7)
397397
function compute_diff_label(diff_graph, i)
398398
di = i - 1 <= length(diff_graph) ? diff_graph[i - 1] : nothing
399399
ii = i - 1 <= length(invview(diff_graph)) ? invview(diff_graph)[i - 1] : nothing
@@ -404,13 +404,18 @@ end
404404
function Base.getindex(bgpm::SystemStructurePrintMatrix, i::Integer, j::Integer)
405405
checkbounds(bgpm, i, j)
406406
if i <= 1
407-
return (Label.(("#", "∂ₜ", "eq", "∂ₜ", "v")))[j]
407+
return (Label.(("#", "∂ₜ", "eq", "", "#", "∂ₜ", "v")))[j]
408+
elseif j == 4
409+
colors = Base.text_colors
410+
return Label("|", :light_black)
408411
elseif j == 2
409412
return compute_diff_label(bgpm.eq_to_diff, i)
410-
elseif j == 4
413+
elseif j == 6
411414
return compute_diff_label(bgpm.var_to_diff, i)
412415
elseif j == 1
413-
return i - 1
416+
return Label((i - 1 <= length(bgpm.eq_to_diff)) ? string(i - 1) : "")
417+
elseif j == 5
418+
return Label((i - 1 <= length(bgpm.var_to_diff)) ? string(i - 1) : "")
414419
elseif j == 3
415420
return BipartiteAdjacencyList(i - 1 <= nsrcs(bgpm.bpg) ?
416421
𝑠neighbors(bgpm.bpg, i - 1) : nothing,
@@ -421,7 +426,7 @@ function Base.getindex(bgpm::SystemStructurePrintMatrix, i::Integer, j::Integer)
421426
bgpm.var_eq_matching !== nothing &&
422427
(i - 1 <= length(invview(bgpm.var_eq_matching))) ?
423428
invview(bgpm.var_eq_matching)[i - 1] : unassigned)
424-
elseif j == 5
429+
elseif j == 7
425430
match = unassigned
426431
if bgpm.var_eq_matching !== nothing && i - 1 <= length(bgpm.var_eq_matching)
427432
match = bgpm.var_eq_matching[i - 1]

0 commit comments

Comments
 (0)