diff --git a/src/DeepDiffs.jl b/src/DeepDiffs.jl index f28b047..f534f7d 100644 --- a/src/DeepDiffs.jl +++ b/src/DeepDiffs.jl @@ -13,6 +13,13 @@ function fieldequal(x::T, y::T) where T true end +# Determine whether color is supported by the given stream +@static if VERSION >= v"0.7.0-DEV.3077" + hascolor(io::IO) = get(IOContext(io), :color, false) +else + hascolor(io::IO) = Base.have_color +end + """ diff = deepdiff(obj1, obj2) diff --git a/src/arrays.jl b/src/arrays.jl index 6473ac6..4f182e6 100644 --- a/src/arrays.jl +++ b/src/arrays.jl @@ -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 hascolor(io) printstyled(io, v, color=color) else print(io, prefix, v) diff --git a/src/strings.jl b/src/strings.jl index b232c5e..a92d7af 100644 --- a/src/strings.jl +++ b/src/strings.jl @@ -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 !hascolor(io) # check to see if we need to close a block if laststate == :removed && state != :removed print(io, "-}") @@ -85,7 +85,7 @@ function Base.show(io::IO, diff::StringDiff) end laststate = state end - if !Base.have_color + if !hascolor(io) if laststate == :removed print(io, "-}") elseif laststate == :added diff --git a/test/display.jl b/test/display.jl index 24f2014..dffd4cc 100644 --- a/test/display.jl +++ b/test/display.jl @@ -1,4 +1,24 @@ @testset "Display tests" begin + # Return a stream with color set as specified. On 0.6 this requires setting + # a global flag, and the :color property in the IOContext has no effect. + function setcolor(yn::Bool) + @static if VERSION < v"0.7.0-DEV.3077" + eval(Base, :(have_color = $yn)) + end + IOContext(IOBuffer(), :color=>yn) + end + + # Capture the original state of the global flag + orig_color = Base.have_color + + function resetcolor() + @static if VERSION < v"0.7.0-DEV.3077" + global orig_color + eval(Base, :(have_color = $orig_color)) + end + nothing + end + # check dictionary print output. This is a little complicated because # the ordering isn't specified. To work around this we just split # up both into lines and make sure they have the same lines in some ordering. @@ -10,34 +30,33 @@ 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 = setcolor(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 = setcolor(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)) + resetcolor() end @testset "Dict diffs print correctly" begin @@ -76,8 +95,7 @@ ) @testset "Color Diffs" begin - eval(Base, :(have_color=true)) - buf = IOBuffer() + buf = setcolor(true) display(TextDisplay(buf), d) expected = """ Dict( @@ -101,11 +119,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 = setcolor(false) display(TextDisplay(buf), d) expected = """ Dict( @@ -126,19 +143,19 @@ ), + :e => "e", )""" - checkdictprint(String(take!(buf)), expected) + checkdictprint(String(take!(buf.io)), expected) end - eval(Base, :(have_color=$orig_color)) + + resetcolor() 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 = setcolor(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-}\"" + resetcolor() end @testset "Multi-line strings display correctly" begin @@ -154,9 +171,8 @@ multiline output""" diff = deepdiff(s1, s2) - buf = IOBuffer() @testset "Color Display" begin - eval(Base, :(have_color=true)) + buf = setcolor(true) expected = """ \"\"\" differences can @@ -166,12 +182,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 = setcolor(false) display(TextDisplay(buf), diff) - @test String(take!(buf)) == """ + @test String(take!(buf.io)) == """ \"\"\" differences can - be hard to find @@ -180,6 +196,6 @@ multiline output\"\"\"""" end - eval(Base, :(have_color=$orig_color)) + resetcolor() end end