Skip to content

Commit

Permalink
Merge branch 'mkitti-TTY-STDOUT-color' into TTY-STDOUT-color
Browse files Browse the repository at this point in the history
  • Loading branch information
karan0299 committed Mar 27, 2020
2 parents 0ba98e6 + f980216 commit 2b8b583
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 39 deletions.
2 changes: 1 addition & 1 deletion base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,7 @@ function create_expr_cache(input::String, output::String, concrete_deps::typeof(
io = open(pipeline(`$(julia_cmd()) -O0
--output-ji $output --output-incremental=yes
--startup-file=no --history-file=no --warn-overwrite=yes
--color=$(have_color === true ? "yes" : "no")
--color=$(have_color === nothing ? "auto" : have_color ? "yes" : "no")
--eval $code_object`, stderr=stderr),
"w", stdout)
in = io.in
Expand Down
27 changes: 14 additions & 13 deletions base/ttyhascolor.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
function ttyhascolor()
term_type = get(ENV, "TERM","")
startswith(term_type, "xterm") && return true
try
@static if Sys.KERNEL === :FreeBSD
return success(`tput AF 0`)
else
return success(`tput setaf 0`)
if Sys.iswindows()
ttyhascolor(term_type = nothing) = true
else
function ttyhascolor(term_type = get(ENV, "TERM", ""))
startswith(term_type, "xterm") && return true
try
@static if Sys.KERNEL === :FreeBSD
return success(`tput AF 0`)
else
return success(`tput setaf 0`)
end
catch e
return false
end
catch e
return false
end
end
function get_have_color()
Expand All @@ -19,6 +22,4 @@ end
in(key_value::Pair{Symbol,Bool}, ::TTY) = key_value.first === :color && key_value.second === get_have_color()
haskey(::TTY, key::Symbol) = key === :color
getindex(::TTY, key::Symbol) = key === :color ? get_have_color() : throw(KeyError(key))
get(::TTY, key::Symbol, default) = key === :color ? get_have_color() : default


get(::TTY, key::Symbol, default) = key === :color ? get_have_color() : default
11 changes: 7 additions & 4 deletions stdlib/REPL/src/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ mutable struct BasicREPL <: AbstractREPL
end

outstream(r::BasicREPL) = r.terminal
hascolor(r::BasicREPL) = hascolor(r.terminal)

function run_frontend(repl::BasicREPL, backend::REPLBackendRef)
d = REPLDisplay(repl)
Expand Down Expand Up @@ -374,13 +375,14 @@ mutable struct LineEditREPL <: AbstractREPL
interface::ModalInterface
backendref::REPLBackendRef
LineEditREPL(t,hascolor,prompt_color,input_color,answer_color,shell_color,help_color,history_file,in_shell,in_help,envcolors) =
new(t,true,prompt_color,input_color,answer_color,shell_color,help_color,history_file,in_shell,
new(t,hascolor,prompt_color,input_color,answer_color,shell_color,help_color,history_file,in_shell,
in_help,envcolors,false,nothing, Options(), nothing)
end
outstream(r::LineEditREPL) = r.t
specialdisplay(r::LineEditREPL) = r.specialdisplay
specialdisplay(r::AbstractREPL) = nothing
terminal(r::LineEditREPL) = r.t
hascolor(r::LineEditREPL) = r.hascolor

LineEditREPL(t::TextTerminal, hascolor::Bool, envcolors::Bool=false) =
LineEditREPL(t, hascolor,
Expand Down Expand Up @@ -759,7 +761,7 @@ function respond(f, repl, main; pass_empty = false, suppress_on_semicolon = true
response = (catch_stack(), true)
end
hide_output = suppress_on_semicolon && ends_with_semicolon(line)
print_response(repl, response, !hide_output, Base.have_color)
print_response(repl, response, !hide_output, hascolor(repl))
end
prepare_next(repl)
reset_state(s)
Expand Down Expand Up @@ -906,7 +908,7 @@ function setup_interface(
end
hist_from_file(hp, f, hist_path)
catch
print_response(repl, (catch_stack(),true), true, Base.have_color)
print_response(repl, (catch_stack(),true), true, hascolor(repl))
println(outstream(repl))
@info "Disabling history file for this session"
repl.history_file = false
Expand Down Expand Up @@ -1104,6 +1106,7 @@ StreamREPL(stream::IO) = StreamREPL(stream, Base.text_colors[:green], Base.input
run_repl(stream::IO) = run_repl(StreamREPL(stream))

outstream(s::StreamREPL) = s.stream
hascolor(s::StreamREPL) = get(s.stream, :color, false)

answer_color(r::LineEditREPL) = r.envcolors ? Base.answer_color() : r.answer_color
answer_color(r::StreamREPL) = r.answer_color
Expand Down Expand Up @@ -1162,7 +1165,7 @@ function ends_with_semicolon(line::AbstractString)
end

function run_frontend(repl::StreamREPL, backend::REPLBackendRef)
have_color = get(repl.stream, :color, false)
have_color = hascolor(repl)
Base.banner(repl.stream)
d = REPLDisplay(repl)
dopushdisplay = !in(d,Base.Multimedia.displays)
Expand Down
17 changes: 1 addition & 16 deletions stdlib/REPL/src/Terminals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,7 @@ beep(t::UnixTerminal) = write(t.err_stream,"\x7")

Base.displaysize(t::UnixTerminal) = displaysize(t.out_stream)

if Sys.iswindows()
hascolor(t::TTYTerminal) = true
else
function hascolor(t::TTYTerminal)
startswith(t.term_type, "xterm") && return true
try
@static if Sys.KERNEL === :FreeBSD
return success(`tput AF 0`)
else
return success(`tput setaf 0`)
end
catch
return false
end
end
end
hascolor(t::TTYTerminal) = Base.ttyhascolor(t.term_type)

# use cached value of have_color
Base.in(key_value::Pair, t::TTYTerminal) = in(key_value, pipe_writer(t))
Expand Down
7 changes: 2 additions & 5 deletions stdlib/REPL/test/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,11 @@ function fake_repl(@nospecialize(f); options::REPL.Options=REPL.Options(confirm_
Base.link_pipe!(output, reader_supports_async=true, writer_supports_async=true)
Base.link_pipe!(err, reader_supports_async=true, writer_supports_async=true)

repl = REPL.LineEditREPL(FakeTerminal(input.out, output.in, err.in), true)
repl = REPL.LineEditREPL(FakeTerminal(input.out, output.in, err.in, options.hascolor), options.hascolor)
repl.options = options

hard_kill = kill_timer(900) # Your debugging session starts now. You have 15 minutes. Go.
# @eval Base old_have_color = have_color
# @eval Base have_color = false
f(input.in, output.out, repl)
# @eval Base have_color = old_have_color
t = @async begin
close(input.in)
close(output.in)
Expand Down Expand Up @@ -93,7 +90,7 @@ end
# in the mix. If verification needs to be done, keep it to the bare minimum. Basically
# this should make sure nothing crashes without depending on how exactly the control
# characters are being used.
fake_repl() do stdin_write, stdout_read, repl
fake_repl(options = REPL.Options(confirm_exit=false,hascolor=false)) do stdin_write, stdout_read, repl
repl.specialdisplay = REPL.REPLDisplay(repl)
repl.history_file = false

Expand Down

0 comments on commit 2b8b583

Please sign in to comment.