Skip to content

Commit

Permalink
feat: improve pretty printing for lists
Browse files Browse the repository at this point in the history
  • Loading branch information
PraneethJain authored and barche committed Jul 22, 2024
1 parent f8b8ad6 commit 17e1d59
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
24 changes: 13 additions & 11 deletions src/StdLib/StdForwardList.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,25 @@ _forward_list_iteration_tuple(v::StdForwardList, state::StdForwardListIterator)
Base.iterate(v::StdForwardList) = _forward_list_iteration_tuple(v, iteratorbegin(v))
Base.iterate(v::StdForwardList, state::StdForwardListIterator) = (state != iteratorend(v)) ? _forward_list_iteration_tuple(v, iterator_next(state)) : nothing

function Base.show(io::IO, ::MIME"text/plain", container::StdForwardList)
print(io, "StdForwardList[")
function Base.show(io::IO, ::MIME"text/plain", container::StdForwardList{T}) where {T}
print(io, "StdForwardList{", T, "}")

iterator = iterate(container)
for i in 1:5
if iterator === nothing
break
end
if iterator === nothing
print(io, "()")
return
end

print(io, ":")
count = 0
while iterator !== nothing && count < 10
item, state = iterator
i > 1 && print(io, ", ")
print(io, item)
print(io, "\n ", item)
iterator = iterate(container, state)
count += 1
end

if iterator !== nothing
print(io, ", ...")
print(io, "\n")
end

print(io, "]")
end
19 changes: 10 additions & 9 deletions src/StdLib/StdList.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ _list_iteration_tuple(v::StdList, state::StdListIterator) = (state == iteratoren
Base.iterate(v::StdList) = _list_iteration_tuple(v, iteratorbegin(v))
Base.iterate(v::StdList, state::StdListIterator) = (state != iteratorend(v)) ? _list_iteration_tuple(v, iterator_next(state)) : nothing

function Base.show(io::IO, ::MIME"text/plain", container::StdList)
print(io, "StdList[")
function Base.show(io::IO, ::MIME"text/plain", container::StdList{T}) where {T}
n = length(container)
print(io, "StdList{", T, "} with ", n, " element", n == 1 ? "" : "s")

for (i, item) in enumerate(Iterators.take(container, 5))
i > 1 && print(io, ", ")
print(io, item)
n == 0 && return
print(io, ":")
for item in Iterators.take(container, 10)
print(io, "\n ", item)
end
if n > 10
print(io, "\n")
end

length(container) > 5 && print(io, ", ...")

print(io, "]")
end

0 comments on commit 17e1d59

Please sign in to comment.