Skip to content

Commit ed37d63

Browse files
committed
Refactor show()
1 parent 41de08f commit ed37d63

File tree

1 file changed

+44
-73
lines changed

1 file changed

+44
-73
lines changed

src/systems/abstractsystem.jl

Lines changed: 44 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,84 +1883,55 @@ function n_extra_equations(sys::AbstractSystem)
18831883
nextras = n_outer_stream_variables + length(ceqs)
18841884
end
18851885

1886-
function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem)
1886+
function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem; bold = true)
1887+
limit = get(io, :limit, false) # if output should be limited,
1888+
rows = first(displaysize(io)) ÷ 5 # then allocate ≈1/5 of display height to each list
1889+
1890+
# Print name
1891+
printstyled(io, "Model $(nameof(sys))"; bold)
1892+
1893+
# Print equations
18871894
eqs = equations(sys)
1888-
vars = unknowns(sys)
1889-
nvars = length(vars)
1890-
if eqs isa AbstractArray && eltype(eqs) <: Equation
1891-
neqs = count(eq -> !(eq.lhs isa Connection), eqs)
1892-
Base.printstyled(io, "Model $(nameof(sys)) with $neqs "; bold = true)
1893-
nextras = n_extra_equations(sys)
1894-
if nextras > 0
1895-
Base.printstyled(io, "("; bold = true)
1896-
Base.printstyled(io, neqs + nextras; bold = true, color = :magenta)
1897-
Base.printstyled(io, ") "; bold = true)
1898-
end
1899-
Base.printstyled(io, "equations\n"; bold = true)
1900-
else
1901-
Base.printstyled(io, "Model $(nameof(sys))\n"; bold = true)
1902-
end
1903-
# The reduced equations are usually very long. It's not that useful to print
1904-
# them.
1905-
#Base.print_matrix(io, eqs)
1906-
#println(io)
1907-
1908-
rows = first(displaysize(io)) ÷ 5
1909-
limit = get(io, :limit, false)
1910-
1911-
Base.printstyled(io, "Unknowns ($nvars):"; bold = true)
1912-
nrows = min(nvars, limit ? rows : nvars)
1913-
limited = nrows < length(vars)
1914-
defs = has_defaults(sys) ? defaults(sys) : nothing
1915-
for i in 1:nrows
1916-
s = vars[i]
1917-
print(io, "\n ", s)
1918-
1919-
if defs !== nothing
1920-
val = get(defs, s, nothing)
1921-
if val !== nothing
1922-
print(io, " [defaults to ")
1923-
show(
1924-
IOContext(io, :compact => true, :limit => true,
1925-
:displaysize => (1, displaysize(io)[2])),
1926-
val)
1927-
print(io, "]")
1928-
end
1929-
description = getdescription(s)
1930-
if description !== nothing && description != ""
1931-
print(io, ": ", description)
1932-
end
1933-
end
1934-
end
1935-
limited && print(io, "\n")
1936-
println(io)
1937-
1938-
vars = parameters(sys)
1939-
nvars = length(vars)
1940-
Base.printstyled(io, "Parameters ($nvars):"; bold = true)
1941-
nrows = min(nvars, limit ? rows : nvars)
1942-
limited = nrows < length(vars)
1943-
for i in 1:nrows
1944-
s = vars[i]
1945-
print(io, "\n ", s)
1946-
1947-
if defs !== nothing
1948-
val = get(defs, s, nothing)
1949-
if val !== nothing
1950-
print(io, " [defaults to ")
1951-
show(
1952-
IOContext(io, :compact => true, :limit => true,
1953-
:displaysize => (1, displaysize(io)[2])),
1954-
val)
1955-
print(io, "]")
1895+
neqs = count(eq -> !(eq.lhs isa Connection), eqs)
1896+
next = n_extra_equations(sys)
1897+
ntot = neqs + next
1898+
ntot > 0 && printstyled(io, "\nEquations ($ntot):"; bold)
1899+
neqs > 0 && printstyled(io, "\n $neqs normal")
1900+
next > 0 && printstyled(io, "\n $next extra")
1901+
#Base.print_matrix(io, eqs) # usually too long and seldom useful to print
1902+
1903+
# Print variables
1904+
for varfunc in [unknowns, parameters]
1905+
vars = varfunc(sys)
1906+
nvars = length(vars)
1907+
nvars == 0 && continue # skip
1908+
header = titlecase(String(nameof(varfunc))) # e.g. "Unknowns"
1909+
printstyled(io, "\n$header ($nvars):"; bold)
1910+
nrows = min(nvars, limit ? rows : nvars)
1911+
defs = has_defaults(sys) ? defaults(sys) : nothing
1912+
for i in 1:nrows
1913+
s = vars[i]
1914+
print(io, "\n ", s)
1915+
if !isnothing(defs)
1916+
val = get(defs, s, nothing)
1917+
if !isnothing(val)
1918+
print(io, " [defaults to ")
1919+
show(
1920+
IOContext(io, :compact => true, :limit => true,
1921+
:displaysize => (1, displaysize(io)[2])),
1922+
val)
1923+
print(io, "]")
1924+
end
1925+
desc = getdescription(s)
19561926
end
1957-
description = getdescription(s)
1958-
if description !== nothing && description != ""
1959-
print(io, ": ", description)
1927+
if !isnothing(desc) && desc != ""
1928+
print(io, ": ", desc)
19601929
end
19611930
end
1931+
limited = nrows < length(vars)
1932+
limited && print(io, "\n") # too many variables to print
19621933
end
1963-
limited && print(io, "\n")
1934+
19641935
return nothing
19651936
end
19661937

0 commit comments

Comments
 (0)