diff --git a/base/REPL.jl b/base/REPL.jl index 0200638a5023f..ae804cd32d0f1 100644 --- a/base/REPL.jl +++ b/base/REPL.jl @@ -111,12 +111,13 @@ function ip_matches_func(ip, func::Symbol) end function display_error(io::IO, er, bt) + print_with_color(:red, io, "ERROR: ") # remove REPL-related frames from interactive printing eval_ind = findlast(addr->Base.REPL.ip_matches_func(addr, :eval), bt) if eval_ind != 0 bt = bt[1:eval_ind-1] end - showerror(IOContext(io, :REPLError => true), er, bt) + showerror(IOContext(io, :hascolor => true), er, bt) end immutable REPLDisplay{R<:AbstractREPL} <: Display diff --git a/base/client.jl b/base/client.jl index a973a6ce6dbef..2507e88a16ab7 100644 --- a/base/client.jl +++ b/base/client.jl @@ -63,8 +63,8 @@ warn_color() = repl_color("JULIA_WARN_COLOR", default_color_warn) info_color() = repl_color("JULIA_INFO_COLOR", default_color_info) input_color() = text_colors[repl_color("JULIA_INPUT_COLOR", default_color_input)] answer_color() = text_colors[repl_color("JULIA_ANSWER_COLOR", default_color_answer)] -bt_linfo_color() = repl_color("JULIA_BT_LINFO_COLOR", :bold) -bt_funcdef_color() = repl_color("JULIA_BT_FUNCTION_COLOR", :bold) +stackframe_linfo_color() = repl_color("JULIA_STACKFRAME_LINFO_COLOR", :bold) +stackframe_function_color() = repl_color("JULIA_STACKFRAME_FUNCTION_COLOR", :bold) function repl_cmd(cmd, out) shell = shell_split(get(ENV,"JULIA_SHELL",get(ENV,"SHELL","/bin/sh"))) diff --git a/base/replutil.jl b/base/replutil.jl index 40f839dda174e..45bce24aed213 100644 --- a/base/replutil.jl +++ b/base/replutil.jl @@ -193,25 +193,13 @@ function showerror(io::IO, ex::TypeError) end function showerror(io::IO, ex, bt; backtrace=true) - try - Base.with_output_color(:red, io) do io + try + with_output_color(get(io, :hascolor, false) ? :red : :nothing, io) do io showerror(io, ex) end finally if backtrace - if get(io, :REPLError, false) - buffer_bt = IOBuffer() - show_backtrace(IOContext(buffer_bt, io), bt) - bt_str = takebuf_string(buffer_bt) - if !isempty(bt_str) - print(io, "\nStacktrace:") - print(io, bt_str) - end - else - Base.with_output_color(:red, io) do io - show_backtrace(io, bt) - end - end + show_backtrace(io, bt) end end end @@ -575,28 +563,22 @@ function show_method_candidates(io::IO, ex::MethodError, kwargs::Vector=Any[]) end end -function show_trace_entry(io, frame, n) - !get(io, :REPLError, false) && println(io) - show(io, frame, full_path=true) +function show_trace_entry(io, frame, n; prefix = " in ") + print(io, "\n") + show(io, frame, full_path=true; prefix = prefix) n > 1 && print(io, " (repeats ", n, " times)") end function show_backtrace(io::IO, t::Vector) - if get(io, :REPLError, false) - n_frames = 0 - stack_counter = 0 - process_backtrace((a,b) -> n_frames += 1, t) - process_entry = (last_frame, n) -> begin - println(io) - stack_counter += 1 - n_spaces_align = ndigits(n_frames) - ndigits(stack_counter) + 1 - print(io, " "^n_spaces_align, "[", stack_counter, "]", ) - show_trace_entry(io, last_frame, n) - end - else - process_entry = (last_frame, n) -> show_trace_entry(io, last_frame, n) + n_frames = 0 + frame_counter = 0 + process_backtrace((a,b) -> n_frames += 1, t) + n_frames != 0 && print(io, "\n\nStacktrace:") + process_entry = (last_frame, n) -> begin + frame_counter += 1 + n_spaces_align = ndigits(n_frames) - ndigits(frame_counter) + 1 + show_trace_entry(io, last_frame, n, prefix = string(" "^n_spaces_align, "[", frame_counter, "] ")) end - process_backtrace(process_entry, t) end diff --git a/base/show.jl b/base/show.jl index 3c056a3621477..106cc3c483a21 100644 --- a/base/show.jl +++ b/base/show.jl @@ -1028,10 +1028,9 @@ end function show_lambda_types(io::IO, li::Core.MethodInstance) # print a method signature tuple for a lambda definition - isreplerror = get(io, :REPLError, false) local sig returned_from_do = false - Base.with_output_color(isreplerror ? Base.bt_funcdef_color() : :nothing, io) do io + Base.with_output_color(get(io, :hascolor, false) ? stackframe_function_color() : :nothing, io) do io if li.specTypes === Tuple print(io, li.def.name, "(...)") returned_from_do = true @@ -1052,7 +1051,7 @@ function show_lambda_types(io::IO, li::Core.MethodInstance) end returned_from_do && return first = true - print_style = isreplerror ? :bold : :nothing + print_style = get(io, :hascolor, false) ? :bold : :nothing print_with_color(print_style, io, "(") for i = 2:length(sig) # fixme (iter): `eachindex` with offset? first || print(io, ", ") diff --git a/base/stacktraces.jl b/base/stacktraces.jl index 82c1f514a40cb..f1b4c7f275fb7 100644 --- a/base/stacktraces.jl +++ b/base/stacktraces.jl @@ -188,7 +188,7 @@ function show_spec_linfo(io::IO, frame::StackFrame) if frame.func === empty_sym @printf(io, "ip:%#x", frame.pointer) else - print_with_color(get(io, :REPLError, false) ? Base.bt_funcdef_color() : :nothing, io, string(frame.func)) + print_with_color(get(io, :hascolor, false) ? Base.stackframe_function_color() : :nothing, io, string(frame.func)) end else linfo = get(frame.linfo) @@ -200,14 +200,14 @@ function show_spec_linfo(io::IO, frame::StackFrame) end end -function show(io::IO, frame::StackFrame; full_path::Bool=false) - isreplerror = get(io, :REPLError, false) - print(io, isreplerror ? " ─ " : " in ") +function show(io::IO, frame::StackFrame; full_path::Bool=false, + prefix = " in ") + print(io, prefix) show_spec_linfo(io, frame) if frame.file !== empty_sym file_info = full_path ? string(frame.file) : basename(string(frame.file)) print(io, " at ") - Base.with_output_color(isreplerror ? Base.bt_linfo_color() : :nothing, io) do io + Base.with_output_color(get(io, :hascolor, false) ? Base.stackframe_linfo_color() : :nothing, io) do io print(io, file_info, ":") if frame.line >= 0 print(io, frame.line) diff --git a/doc/manual/interacting-with-julia.rst b/doc/manual/interacting-with-julia.rst index 16e35b614ddcd..1f5658eb319ea 100644 --- a/doc/manual/interacting-with-julia.rst +++ b/doc/manual/interacting-with-julia.rst @@ -311,5 +311,5 @@ informational messages in cyan you can add the following to your ``juliarc.jl`` The backtrace that is printed in the REPL when an error is thrown can also be customized. The color of the function name and the file and line info for each stack frame can be changed with environment variables as:: - ENV["JULIA_BT_FUNCTION_COLOR"] = :green # Color of function names - ENV["JULIA_BT_LINFO_COLOR"] = :yellow # Color of file name and line number + ENV["JULIA_STACKFRAME_FUNCTION_COLOR"] = :green # Color of function names + ENV["JULIA_STACKFRAME_LINFO_COLOR"] = :yellow # Color of file name and line number