Skip to content

Commit 5604d66

Browse files
authored
Merge pull request #263 from JuliaDynamics/hw/show
fix aligned multiline show
2 parents bda809b + ac194bd commit 5604d66

File tree

3 files changed

+61
-6
lines changed

3 files changed

+61
-6
lines changed

src/show.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# overload to reduce excessive printing
44
function Base.show(io::IO, @nospecialize(nw::Network))
5-
print(io, "Network($(nv(nw.im.g)) vertices, $(ne(nw.im.g)) edges")
5+
print(io, "Network($(nv(nw.im.g)) vertices, $(ne(nw.im.g)) edges)")
66
end
77

88
function Base.show(io::IO, ::MIME"text/plain", @nospecialize(nw::Network))
@@ -304,11 +304,12 @@ function align_strings(_vecofstr::AbstractVector{<:AbstractString}; padding=:alt
304304
end
305305
end
306306
alig = align_strings(splitted; padding)
307-
if !isempty(newlines)
308-
for i in newlines
309-
alig[i] = alig[i] * "\n" * alig[i+1]
310-
end
311-
deleteat!(alig, newlines .+1 )
307+
308+
@assert issorted(newlines)
309+
while !isempty(newlines)
310+
i = pop!(newlines)
311+
alig[i] = alig[i] * "\n" * alig[i+1]
312+
deleteat!(alig, i+1)
312313
end
313314
alig
314315
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ haskey(ENV, "BUILDKITE") && @test CUDA.functional() # fail early in buildkite if
4545
@safetestset "initialization test" begin include("initialization_test.jl") end
4646
@safetestset "Callbacks test" begin include("callbacks_test.jl") end
4747
@safetestset "Metadata test" begin include("metadata_test.jl") end
48+
@safetestset "Show-methods test" begin include("show_test.jl") end
4849

4950
@safetestset "AD test" begin include("AD_test.jl") end
5051

test/show_test.jl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using NetworkDynamics
2+
using Graphs
3+
using Test
4+
5+
(isinteractive() && @__MODULE__()==Main ? includet : include)("ComponentLibrary.jl")
6+
7+
g = SimpleGraph([0 1 1 0 1;
8+
1 0 1 1 0;
9+
1 1 0 1 0;
10+
0 1 1 0 1;
11+
1 0 0 1 0])
12+
13+
vs = [Lib.swing_mtk() for _ in 1:5];
14+
set_default!(vs[1], :Pmech, -1)
15+
set_default!(vs[2], :Pmech, 1.5)
16+
set_default!(vs[3], :Pmech, -1)
17+
set_default!(vs[4], :Pmech, -1)
18+
set_default!(vs[5], :Pmech, 1.5)
19+
20+
ls = [Lib.line_mtk() for _ in 1:7];
21+
nw = Network(g, vs, ls)
22+
s = NWState(nw)
23+
p = NWParameter(nw)
24+
25+
repr(nw)
26+
repr("text/plain", nw)
27+
28+
repr("text/plain", vs[1])
29+
repr("text/plain", ls[1])
30+
31+
32+
repr("text/plain", s)
33+
repr("text/plain", p)
34+
repr("text/plain", s.v)
35+
repr("text/plain", s.e)
36+
repr("text/plain", p.v)
37+
repr("text/plain", p.e)
38+
39+
affect = ComponentAffect([], [:limit, :K]) do u, p, ctx
40+
nothing
41+
end
42+
condition = ComponentCondition([:P, :₋P, :srcθ], [:limit, :K]) do u, p, t
43+
nothing
44+
end
45+
cb1 = PresetTimeComponentCallback(1.0, affect)
46+
repr("text/plain", cb1)
47+
cb2 = ContinousComponentCallback(condition, affect)
48+
repr("text/plain", cb2)
49+
cb3 = DiscreteComponentCallback(condition, affect)
50+
repr("text/plain", cb3)
51+
set_callback!(ls[1], (cb1, cb2, cb3))
52+
repr("text/plain", ls[1])
53+
repr("text/plain", nw)

0 commit comments

Comments
 (0)