From 59586ded0da55b383c01b5d6e3538cd9680d79a3 Mon Sep 17 00:00:00 2001 From: Mark Kittisopikul Date: Thu, 26 Mar 2020 03:48:06 -0500 Subject: [PATCH] Add hascolor as an interface for concrete REPLs --- stdlib/REPL/src/REPL.jl | 11 +++++++---- stdlib/REPL/test/repl.jl | 7 ++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/stdlib/REPL/src/REPL.jl b/stdlib/REPL/src/REPL.jl index 3ec85ca9ae3c8..a66d5cee74745 100644 --- a/stdlib/REPL/src/REPL.jl +++ b/stdlib/REPL/src/REPL.jl @@ -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) @@ -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, @@ -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) @@ -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 @@ -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 @@ -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) diff --git a/stdlib/REPL/test/repl.jl b/stdlib/REPL/test/repl.jl index d9a8f3ca26183..279eebaf69678 100644 --- a/stdlib/REPL/test/repl.jl +++ b/stdlib/REPL/test/repl.jl @@ -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) @@ -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