Only use custom show methods for plaintext display

Thanks Lyndon: https://github.com/invenia/PkgTemplates.jl/issues/80#issuecomment-527351832
This commit is contained in:
Chris de Graaf 2019-09-03 18:15:53 +07:00
parent 481d8430e6
commit 2d7c851fca
No known key found for this signature in database
GPG Key ID: 150FFDD9B0073C7B
2 changed files with 20 additions and 12 deletions

View File

@ -3,7 +3,7 @@
# leaves(Plugin)
# end
function Base.show(io::IO, p::T) where T <: Plugin
function Base.show(io::IO, ::MIME"text/plain", p::T) where T <: Plugin
indent = get(io, :indent, 0)
print(io, repeat(' ', indent), T, ":")
foreach(fieldnames(T)) do n
@ -15,7 +15,7 @@ end
show_field(x) = repr(x)
show_field(x::AbstractString) = repr(contractuser(x))
function Base.show(io::IO, t::Template)
function Base.show(io::IO, m::MIME"text/plain", t::Template)
println(io, "Template:")
foreach(fieldnames(Template)) do n
n === :plugins || println(io, repeat(' ', 2), n, ": ", show_field(getfield(t, n)))
@ -24,9 +24,9 @@ function Base.show(io::IO, t::Template)
print(io, " plugins: None")
else
print(io, repeat(' ', 2), "plugins:")
foreach(values(t.plugins)) do p
foreach(sort(collect(values(t.plugins)); by=string)) do p
println(io)
show(IOContext(io, :indent => 4), p)
show(IOContext(io, :indent => 4), m, p)
end
end
end

View File

@ -9,7 +9,7 @@ const LICENSE_DIR = contractuser(PT.LICENSE_DIR)
destination: "README.md"
inline_badges: false
"""
@test sprint(show, Readme()) == rstrip(expected)
@test sprint(show, MIME("text/plain"), Readme()) == rstrip(expected)
end
@testset "Template" begin
@ -17,7 +17,7 @@ const LICENSE_DIR = contractuser(PT.LICENSE_DIR)
Template:
authors: ["$USER"]
develop: true
dir: "$(Pkg.devdir())"
dir: "$(contractuser(Pkg.devdir()))"
git: true
host: "github.com"
julia_version: v"1.0.0"
@ -25,18 +25,26 @@ const LICENSE_DIR = contractuser(PT.LICENSE_DIR)
ssh: false
user: "$USER"
plugins:
Readme:
file: "$DEFAULTS_DIR/README.md"
destination: "README.md"
inline_badges: false
Tests:
file: "$DEFAULTS_DIR/runtests.jl"
Gitignore:
ds_store: true
dev: true
License:
path: "$LICENSE_DIR/MIT"
destination: "LICENSE"
Readme:
file: "$DEFAULTS_DIR/README.md"
destination: "README.md"
inline_badges: false
Tests:
file: "$DEFAULTS_DIR/runtests.jl"
"""
@test sprint(show, MIME("text/plain"), tpl(; authors=USER)) == rstrip(expected)
end
@testset "show as serialization" begin
# Equality is not implemented for Template, so check the string form.
t1 = tpl()
t2 = eval(Meta.parse(sprint(show, t1)))
@test sprint(show, t1) == sprint(show, t2)
end
end