From ac51ef0c40b15d0cefe5d378426f1385d0dde0a8 Mon Sep 17 00:00:00 2001 From: KristofferC Date: Mon, 14 Sep 2020 13:49:28 +0200 Subject: [PATCH 1/2] correctly overload show instead of print --- src/crayon.jl | 2 +- src/crayon_wrapper.jl | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/crayon.jl b/src/crayon.jl index add3288..86311f7 100644 --- a/src/crayon.jl +++ b/src/crayon.jl @@ -121,7 +121,7 @@ function Base.print(io::IO, x::Crayon) end end -function Base.show(io::IO, x::Crayon) +function Base.show(io::IO, ::MIME"text/plain", x::Crayon) if anyactive(x) print(io, x) print(io, ESCAPED_CSI) diff --git a/src/crayon_wrapper.jl b/src/crayon_wrapper.jl index 17522ed..81be79d 100644 --- a/src/crayon_wrapper.jl +++ b/src/crayon_wrapper.jl @@ -10,14 +10,14 @@ function (c::Crayon)(args::Union{CrayonWrapper,AbstractString}...) CrayonWrapper(c, typefix.(collect(args))) end -Base.show(io::IO, cw::CrayonWrapper) = _show(io, cw, CrayonStack(incremental = true)) +Base.print(io::IO, cw::CrayonWrapper) = _print(io, cw, CrayonStack(incremental = true)) -_show(io::IO, str::String, stack::CrayonStack) = print(io, str) +_print(io::IO, str::String, stack::CrayonStack) = print(io, str) -function _show(io::IO, cw::CrayonWrapper, stack::CrayonStack) +function _print(io::IO, cw::CrayonWrapper, stack::CrayonStack) print(io, push!(stack, cw.c)) for obj in cw.v - _show(io, obj, stack) + _print(io, obj, stack) end length(stack.crayons) > 1 && print(io, pop!(stack)) return From 8f5de9976de065c773b163393a4dd238585d7753 Mon Sep 17 00:00:00 2001 From: KristofferC Date: Mon, 14 Sep 2020 16:48:54 +0200 Subject: [PATCH 2/2] add a show method for CrayonWrapper --- src/crayon_stack.jl | 1 + src/crayon_wrapper.jl | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/crayon_stack.jl b/src/crayon_stack.jl index 1c54341..dd30d91 100644 --- a/src/crayon_stack.jl +++ b/src/crayon_stack.jl @@ -5,6 +5,7 @@ struct CrayonStack end Base.print(io::IO, cs::CrayonStack) = print(io, cs.crayons[end]) +Base.show(io::IO, ::MIME"text/plain", cs::CrayonStack) = show(io, MIME("text/plain"), cs.crayons[end]) function CrayonStack(; incremental::Bool = false) CrayonStack(incremental, [Crayon(ANSIColor(0x9, COLORS_16, !incremental), diff --git a/src/crayon_wrapper.jl b/src/crayon_wrapper.jl index 81be79d..629fe54 100644 --- a/src/crayon_wrapper.jl +++ b/src/crayon_wrapper.jl @@ -11,9 +11,7 @@ function (c::Crayon)(args::Union{CrayonWrapper,AbstractString}...) end Base.print(io::IO, cw::CrayonWrapper) = _print(io, cw, CrayonStack(incremental = true)) - _print(io::IO, str::String, stack::CrayonStack) = print(io, str) - function _print(io::IO, cw::CrayonWrapper, stack::CrayonStack) print(io, push!(stack, cw.c)) for obj in cw.v @@ -23,5 +21,17 @@ function _print(io::IO, cw::CrayonWrapper, stack::CrayonStack) return end +Base.show(io::IO, ::MIME"text/plain", cw::CrayonWrapper) = _show(io, cw, CrayonStack(incremental = true)) +_show(io::IO, str::String, stack::CrayonStack) = print(io, str) +function _show(io::IO, cw::CrayonWrapper, stack::CrayonStack) + show(io, MIME("text/plain"), cw.c) + print(io, push!(stack, cw.c)) + for obj in cw.v + _show(io, obj, stack) + end + length(stack.crayons) > 1 && show(io, MIME("text/plain"), pop!(stack)) + return +end + Base.:*(c::Crayon, cw::CrayonWrapper) = CrayonWrapper(c * cw.c, cw.v) Base.:*(cw::CrayonWrapper, c::Crayon) = CrayonWrapper(cw.c * c, cw.v)