Skip to content

Commit

Permalink
Merge pull request #25228 from stevengj/colortty
Browse files Browse the repository at this point in the history
fix color attribute in TTY and TTYTerminal to be more IOContext-like
  • Loading branch information
StefanKarpinski authored Dec 22, 2017
2 parents 6c94bd8 + 2990e43 commit 84b14ac
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
5 changes: 4 additions & 1 deletion base/repl/Terminals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ else
end

# use cached value of have_color
Base.get(::TTYTerminal, k::Symbol, default) = k === :color ? Base.have_color : default
Base.in(key_value::Pair, t::TTYTerminal) = in(key_value, pipe_writer(t))
Base.haskey(t::TTYTerminal, key) = haskey(pipe_writer(t), key)
Base.getindex(t::TTYTerminal, key) = getindex(pipe_writer(t), key)
Base.get(t::TTYTerminal, key, default) = get(pipe_writer(t), key, default)

Base.peek(t::TTYTerminal) = Base.peek(t.in_stream)

Expand Down
5 changes: 4 additions & 1 deletion base/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,10 @@ function displaysize(io::TTY)
return h, w
end

get(::TTY, k::Symbol, default) = k === :color ? have_color : default
in(key_value::Pair{Symbol,Bool}, ::TTY) = key_value.first === :color && key_value.second === have_color
haskey(::TTY, key::Symbol) = key === :color
getindex(::TTY, key::Symbol) = key === :color ? have_color : throw(KeyError(key))
get(::TTY, key::Symbol, default) = key === :color ? have_color : default

### Libuv callbacks ###

Expand Down
10 changes: 10 additions & 0 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,16 @@ let buf_color = IOBuffer()
@test expected_str == String(take!(buf_color))
end

if STDOUT isa Base.TTY
@test haskey(STDOUT, :color) == true
@test haskey(STDOUT, :bar) == false
@test (:color=>Base.have_color) in STDOUT
@test (:color=>!Base.have_color) STDOUT
@test STDOUT[:color] == get(STDOUT, :color, nothing) == Base.have_color
@test get(STDOUT, :bar, nothing) === nothing
@test_throws KeyError STDOUT[:bar]
end

let
global c_18711 = 0
buf = IOContext(IOBuffer(), :hascontext => true)
Expand Down
11 changes: 9 additions & 2 deletions test/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -725,10 +725,17 @@ let ends_with_semicolon = Base.REPL.ends_with_semicolon
end

# PR #20794, TTYTerminal with other kinds of streams
let term = Base.Terminals.TTYTerminal("dumb",IOBuffer("1+2\n"),IOBuffer(),IOBuffer())
let term = Base.Terminals.TTYTerminal("dumb",IOBuffer("1+2\n"),IOContext(IOBuffer(),:foo=>true),IOBuffer())
r = Base.REPL.BasicREPL(term)
REPL.run_repl(r)
@test String(take!(term.out_stream)) == "julia> 3\n\njulia> \n"
@test String(take!(term.out_stream.io)) == "julia> 3\n\njulia> \n"
@test haskey(term, :foo) == true
@test haskey(term, :bar) == false
@test (:foo=>true) in term
@test (:foo=>false) term
@test term[:foo] == get(term, :foo, nothing) == true
@test get(term, :bar, nothing) === nothing
@test_throws KeyError term[:bar]
end


Expand Down

0 comments on commit 84b14ac

Please sign in to comment.