Skip to content

Commit

Permalink
Use IOContexts instead of Base.have_color
Browse files Browse the repository at this point in the history
Whether an IO object supports color is a property of the object (or
rather its context); Base.have_color refers only to whether Julia was
launched with --color=yes.
  • Loading branch information
ararslan committed May 19, 2018
1 parent d75d12b commit 5cd6a14
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ end

# prefix is printed if we're not using color
function printitem(io, v, color=:normal, prefix="")
if Base.have_color
if get(IOContext(io), :color, false)
printstyled(io, v, color=color)
else
print(io, prefix, v)
Expand Down
4 changes: 2 additions & 2 deletions src/strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function Base.show(io::IO, diff::StringDiff)

print(io, "\"")
visitall(diff.diff) do idx, state, last
if !Base.have_color
if !get(IOContext(io), :color, false)
# check to see if we need to close a block
if laststate == :removed && state != :removed
print(io, "-}")
Expand All @@ -85,7 +85,7 @@ function Base.show(io::IO, diff::StringDiff)
end
laststate = state
end
if !Base.have_color
if !get(IOContext(io), :color, false)
if laststate == :removed
print(io, "-}")
elseif laststate == :added
Expand Down
43 changes: 16 additions & 27 deletions test/display.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,30 @@
explines = sort(split(expected, "\n"))
@test outlines == explines
end
orig_color = Base.have_color
@testset "Array diffs print correctly" begin
d1 = deepdiff([1, 2, 7, 3], [2, 3, 4, 1, 2, 3, 5])
d2 = deepdiff([1], [2])

buf = IOBuffer()
eval(Base, :(have_color=true))
buf = IOContext(IOBuffer(), :color=>true)
expected1 = """
[2, 3, 4, 1, 2, 7, 3, 5]"""
expected2 = """[1, 2]"""
@testset "Color Diffs" begin
display(TextDisplay(buf), d1)
@test String(take!(buf)) == expected1
@test String(take!(buf.io)) == expected1
display(TextDisplay(buf), d2)
@test String(take!(buf)) == expected2
@test String(take!(buf.io)) == expected2
end

eval(Base, :(have_color=false))
buf = IOContext(IOBuffer(), :color=>false)
@testset "No-Color Diffs" begin
display(TextDisplay(buf), d1)
@test String(take!(buf)) == """
@test String(take!(buf.io)) == """
[(+)2, (+)3, (+)4, 1, 2, (-)7, 3, (+)5]"""
display(TextDisplay(buf), d2)
@test String(take!(buf)) == """
@test String(take!(buf.io)) == """
[(-)1, (+)2]"""
end

eval(Base, :(have_color=$orig_color))
end

@testset "Dict diffs print correctly" begin
Expand Down Expand Up @@ -76,8 +72,7 @@
)

@testset "Color Diffs" begin
eval(Base, :(have_color=true))
buf = IOBuffer()
buf = IOContext(IOBuffer(), :color=>true)
display(TextDisplay(buf), d)
expected = """
Dict(
Expand All @@ -101,11 +96,10 @@
# This test is broken because the specifics of how the ANSI color
# codes are printed change based on the order, which changes with
# different julia versions.
@test_skip String(take!(buf)) == expected
@test_skip String(take!(buf.io)) == expected
end
@testset "No-Color Diffs" begin
eval(Base, :(have_color=false))
buf = IOBuffer()
buf = IOContext(IOBuffer(), :color=>false)
display(TextDisplay(buf), d)
expected = """
Dict(
Expand All @@ -126,19 +120,16 @@
),
+ :e => "e",
)"""
checkdictprint(String(take!(buf)), expected)
checkdictprint(String(take!(buf.io)), expected)
end
eval(Base, :(have_color=$orig_color))
end

@testset "single-line strings display correctly" begin
# this test is just to handle some cases that don't get exercised elsewhere
diff = deepdiff("abc", "adb")
buf = IOBuffer()
eval(Base, :(have_color=false))
buf = IOContext(IOBuffer(), :color=>false)
display(TextDisplay(buf), diff)
@test String(take!(buf)) == "\"a{+d+}b{-c-}\""
eval(Base, :(have_color=true))
@test String(take!(buf.io)) == "\"a{+d+}b{-c-}\""
end

@testset "Multi-line strings display correctly" begin
Expand All @@ -154,9 +145,8 @@
multiline
output"""
diff = deepdiff(s1, s2)
buf = IOBuffer()
@testset "Color Display" begin
eval(Base, :(have_color=true))
buf = IOContext(IOBuffer(), :color=>true)
expected = """
\"\"\"
differences can
Expand All @@ -166,12 +156,12 @@
multiline
output\"\"\""""
display(TextDisplay(buf), diff)
@test String(take!(buf)) == expected
@test String(take!(buf.io)) == expected
end
@testset "No-Color Display" begin
eval(Base, :(have_color=false))
buf = IOContext(IOBuffer(), :color=>false)
display(TextDisplay(buf), diff)
@test String(take!(buf)) == """
@test String(take!(buf.io)) == """
\"\"\"
differences can
- be hard to find
Expand All @@ -180,6 +170,5 @@
multiline
output\"\"\""""
end
eval(Base, :(have_color=$orig_color))
end
end

0 comments on commit 5cd6a14

Please sign in to comment.