Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #11 #12

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ function Base.IOContext(io::GarishIO, KVs::Pair...)
)
end

Base.show_circular(io::GarishIO, x) = Base.show_circular(io.bland_io, x)
Base.displaysize(io::GarishIO) = io.displaysize
Base.in(key_value::Pair, io::GarishIO) = in(key_value, IOContext(io).dict, ===)
Base.haskey(io::GarishIO, key) = haskey(IOContext(io).dict, key)
Expand Down
2 changes: 1 addition & 1 deletion src/pprint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pprint(io::GarishIO, ::MIME"text/plain", @specialize(x::Type)) = print_token(io,
# Struct
function pprint(io::GarishIO, mime::MIME, @nospecialize(x))
if fallback_to_default_show(io, x) && isstructtype(typeof(x))
return pprint_struct(io, mime, x)
pprint_struct(io, mime, x)
elseif io.state.level > 0 # print show inside
show_text_within(io, mime, x)
else # fallback to show unless it is a struct type
Expand Down
99 changes: 54 additions & 45 deletions src/struct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,58 +25,63 @@ end
Print `x` as a struct type with mime type `MIME"text/plain"`.
"""
function pprint_struct(io::GarishIO, mime::MIME"text/plain", @nospecialize(x))
t = typeof(x)
isstructtype(t) || throw(ArgumentError("expect a struct type, got $t"))
pprint_struct_type(io, mime, t); print(io.bland_io, "(")
if !Base.show_circular(io, x)
recur_io = IOContext(io.bland_io, :SHOWN_SET => x)
Comment on lines +28 to +29
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The git diff doesn't reflect the change clearly. Actually I just added these two lines and ...


nf = nfields(x)::Int
nf == 0 && return print(io.bland_io, ")")
t = typeof(x)
isstructtype(t) || throw(ArgumentError("expect a struct type, got $t"))
pprint_struct_type(io, mime, t)
print(io.bland_io, "(")

io.compact || println(io.bland_io)
nf = nfields(x)::Int
nf == 0 && return print(io.bland_io, ")")

# findout fields to print
fields_to_print = Int[]
for i in 1:nf
f = fieldname(t, i)
value = getfield(x, i)
if !io.include_defaults && is_option(x) && value == field_default(t, f)
else
push!(fields_to_print, i)
end
end
io.compact || println(io.bland_io)

within_nextlevel(io) do
for i in fields_to_print
# findout fields to print
fields_to_print = Int[]
for i = 1:nf
f = fieldname(t, i)
value = getfield(x, i)
print_indent(io)
print_token(io, :fieldname, f)
if io.compact
print(io.bland_io, "=")
if !io.include_defaults && is_option(x) && value == field_default(t, f)
else
print(io.bland_io, " = ")
io.state.offset = 3 + length(string(f))
push!(fields_to_print, i)
end
end

if !isdefined(x, f) # print undef as comment color
pprint(io, undef)
else
new_io = GarishIO(io; limit=true)
pprint_field(new_io, mime, value)
end
within_nextlevel(io) do
for i in fields_to_print
f = fieldname(t, i)
value = getfield(x, i)
print_indent(io)
print_token(io, :fieldname, f)
if io.compact
print(io.bland_io, "=")
else
print(io.bland_io, " = ")
io.state.offset = 3 + length(string(f))
end

if !io.compact || i != last(fields_to_print)
print(io.bland_io, ", ")
end
if !isdefined(x, f) # print undef as comment color
pprint(io, undef)
else
new_io = GarishIO(recur_io, io; limit = true)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... and this line

pprint_field(new_io, mime, value)
end

if i != last(fields_to_print)
io.compact || println(io.bland_io)
if !io.compact || i != last(fields_to_print)
print(io.bland_io, ", ")
end

if i != last(fields_to_print)
io.compact || println(io.bland_io)
end
end
end
io.compact || println(io.bland_io)
print_indent(io)
print(io.bland_io, ")")
end
io.compact || println(io.bland_io)
print_indent(io)
print(io.bland_io, ")")
end

function pprint_struct_type(io::GarishIO, mime::MIME"text/plain", @nospecialize(x))
Expand Down Expand Up @@ -121,11 +126,15 @@ end
pprint_field(io::GarishIO, x) = pprint_field(io, MIME"text/plain"(), x)

function pprint_field(io::GarishIO, mime::MIME"text/plain", x)
upperlevel_type = io.state.type
upperlevel_noindent_in_first_line = io.state.noindent_in_first_line
io.state.type = StructField
io.state.noindent_in_first_line = true
pprint(io, mime, x)
io.state.noindent_in_first_line = upperlevel_noindent_in_first_line
io.state.type = upperlevel_type
if !Base.show_circular(io, x)
recur_io = IOContext(io, :SHOWN_SET => x)

upperlevel_type = io.state.type
upperlevel_noindent_in_first_line = io.state.noindent_in_first_line
io.state.type = StructField
io.state.noindent_in_first_line = true
pprint(GarishIO(recur_io, io), mime, x)
io.state.noindent_in_first_line = upperlevel_noindent_in_first_line
io.state.type = upperlevel_type
end
end
12 changes: 12 additions & 0 deletions test/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ struct T5{T}
x::T
end

mutable struct T6
x::Any
end

struct TTTTTTTTTTTTTTTTTTTT{A,B}
x::A
y::B
Expand Down Expand Up @@ -62,6 +66,14 @@ pprint(T5(Dict("a"=>T4(T5([1, 2, 3]), T5([1, 2, 3])), "b"=>2im)))

pprint(T5(Dict("a"=>(1, 2, 3), "b"=>Any)))

recur_struct = T6(nothing)
recur_struct.x = recur_struct
pprint(recur_struct)

recur_struct_list = T6([])
push!(recur_struct_list.x, T5(recur_struct_list))
pprint(recur_struct_list)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

a = TTTTTTTTTTTTTTTTTTTT(1, 2)
b = TTTTTTTTTTTTTTTTTTTT(a, a)
c = TTTTTTTTTTTTTTTTTTTT(b, b)
Expand Down