Skip to content

Commit b43097a

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

File tree

1 file changed

+44
-71
lines changed

1 file changed

+44
-71
lines changed

src/systems/abstractsystem.jl

Lines changed: 44 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,84 +1883,57 @@ 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)
18901895
if eqs isa AbstractArray && eltype(eqs) <: Equation
18911896
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, "]")
1897+
next = n_extra_equations(sys)
1898+
ntot = neqs + next
1899+
ntot > 0 && printstyled(io, "\nEquations ($ntot):"; bold)
1900+
neqs > 0 && printstyled(io, "\n $neqs solvable … see equations(sys) for all")
1901+
next > 0 && printstyled(io, "\n $next extra") # TODO: what are these equations?
1902+
#Base.print_matrix(io, eqs) # usually too long and not useful to print all equations
1903+
end
1904+
1905+
# Print variables
1906+
for varfunc in [unknowns, parameters]
1907+
vars = varfunc(sys)
1908+
nvars = length(vars)
1909+
nvars == 0 && continue # skip
1910+
header = titlecase(String(nameof(varfunc))) # e.g. "Unknowns"
1911+
printstyled(io, "\n$header ($nvars):"; bold)
1912+
nrows = min(nvars, limit ? rows : nvars)
1913+
defs = has_defaults(sys) ? defaults(sys) : nothing
1914+
for i in 1:nrows
1915+
s = vars[i]
1916+
print(io, "\n ", s)
1917+
if !isnothing(defs)
1918+
val = get(defs, s, nothing)
1919+
if !isnothing(val)
1920+
print(io, " [defaults to ")
1921+
show(
1922+
IOContext(io, :compact => true, :limit => true,
1923+
:displaysize => (1, displaysize(io)[2])),
1924+
val)
1925+
print(io, "]")
1926+
end
1927+
desc = getdescription(s)
19561928
end
1957-
description = getdescription(s)
1958-
if description !== nothing && description != ""
1959-
print(io, ": ", description)
1929+
if !isnothing(desc) && desc != ""
1930+
print(io, ": ", desc)
19601931
end
19611932
end
1933+
limited = nrows < length(vars)
1934+
limited && print(io, "\n") # too many variables to print
19621935
end
1963-
limited && print(io, "\n")
1936+
19641937
return nothing
19651938
end
19661939

0 commit comments

Comments
 (0)