Skip to content

Commit

Permalink
do it in a different way
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Sep 21, 2016
1 parent eaef849 commit 6a48239
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 45 deletions.
3 changes: 2 additions & 1 deletion base/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,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")))
Expand Down
46 changes: 14 additions & 32 deletions base/replutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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(:red, 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
Expand Down Expand Up @@ -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

Expand Down
5 changes: 2 additions & 3 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, ", ")
Expand Down
10 changes: 5 additions & 5 deletions base/stacktraces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions doc/manual/interacting-with-julia.rst
Original file line number Diff line number Diff line change
Expand Up @@ -307,5 +307,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

0 comments on commit 6a48239

Please sign in to comment.