Skip to content

Commit 6a48239

Browse files
committed
do it in a different way
1 parent eaef849 commit 6a48239

File tree

6 files changed

+27
-45
lines changed

6 files changed

+27
-45
lines changed

base/REPL.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,13 @@ function ip_matches_func(ip, func::Symbol)
111111
end
112112

113113
function display_error(io::IO, er, bt)
114+
print_with_color(:red, io, "ERROR: ")
114115
# remove REPL-related frames from interactive printing
115116
eval_ind = findlast(addr->Base.REPL.ip_matches_func(addr, :eval), bt)
116117
if eval_ind != 0
117118
bt = bt[1:eval_ind-1]
118119
end
119-
showerror(IOContext(io, :REPLError => true), er, bt)
120+
showerror(IOContext(io, :hascolor => true), er, bt)
120121
end
121122

122123
immutable REPLDisplay{R<:AbstractREPL} <: Display

base/client.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ warn_color() = repl_color("JULIA_WARN_COLOR", default_color_warn)
5757
info_color() = repl_color("JULIA_INFO_COLOR", default_color_info)
5858
input_color() = text_colors[repl_color("JULIA_INPUT_COLOR", default_color_input)]
5959
answer_color() = text_colors[repl_color("JULIA_ANSWER_COLOR", default_color_answer)]
60-
bt_linfo_color() = repl_color("JULIA_BT_LINFO_COLOR", :bold)
61-
bt_funcdef_color() = repl_color("JULIA_BT_FUNCTION_COLOR", :bold)
60+
stackframe_linfo_color() = repl_color("JULIA_STACKFRAME_LINFO_COLOR", :bold)
61+
stackframe_function_color() = repl_color("JULIA_STACKFRAME_FUNCTION_COLOR", :bold)
6262

6363
function repl_cmd(cmd, out)
6464
shell = shell_split(get(ENV,"JULIA_SHELL",get(ENV,"SHELL","/bin/sh")))

base/replutil.jl

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -193,25 +193,13 @@ function showerror(io::IO, ex::TypeError)
193193
end
194194

195195
function showerror(io::IO, ex, bt; backtrace=true)
196-
try
197-
Base.with_output_color(:red, io) do io
196+
try
197+
with_output_color(:red, io) do io
198198
showerror(io, ex)
199199
end
200200
finally
201201
if backtrace
202-
if get(io, :REPLError, false)
203-
buffer_bt = IOBuffer()
204-
show_backtrace(IOContext(buffer_bt, io), bt)
205-
bt_str = takebuf_string(buffer_bt)
206-
if !isempty(bt_str)
207-
print(io, "\nStacktrace:")
208-
print(io, bt_str)
209-
end
210-
else
211-
Base.with_output_color(:red, io) do io
212-
show_backtrace(io, bt)
213-
end
214-
end
202+
show_backtrace(io, bt)
215203
end
216204
end
217205
end
@@ -575,28 +563,22 @@ function show_method_candidates(io::IO, ex::MethodError, kwargs::Vector=Any[])
575563
end
576564
end
577565

578-
function show_trace_entry(io, frame, n)
579-
!get(io, :REPLError, false) && println(io)
580-
show(io, frame, full_path=true)
566+
function show_trace_entry(io, frame, n; prefix = " in ")
567+
print(io, "\n")
568+
show(io, frame, full_path=true; prefix = prefix)
581569
n > 1 && print(io, " (repeats ", n, " times)")
582570
end
583571

584572
function show_backtrace(io::IO, t::Vector)
585-
if get(io, :REPLError, false)
586-
n_frames = 0
587-
stack_counter = 0
588-
process_backtrace((a,b) -> n_frames += 1, t)
589-
process_entry = (last_frame, n) -> begin
590-
println(io)
591-
stack_counter += 1
592-
n_spaces_align = ndigits(n_frames) - ndigits(stack_counter) + 1
593-
print(io, " "^n_spaces_align, "[", stack_counter, "]", )
594-
show_trace_entry(io, last_frame, n)
595-
end
596-
else
597-
process_entry = (last_frame, n) -> show_trace_entry(io, last_frame, n)
573+
n_frames = 0
574+
frame_counter = 0
575+
process_backtrace((a,b) -> n_frames += 1, t)
576+
n_frames != 0 && print(io, "\n\nStacktrace:")
577+
process_entry = (last_frame, n) -> begin
578+
frame_counter += 1
579+
n_spaces_align = ndigits(n_frames) - ndigits(frame_counter) + 1
580+
show_trace_entry(io, last_frame, n, prefix = string(" "^n_spaces_align, "[", frame_counter, "] "))
598581
end
599-
600582
process_backtrace(process_entry, t)
601583
end
602584

base/show.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,10 +1028,9 @@ end
10281028

10291029
function show_lambda_types(io::IO, li::Core.MethodInstance)
10301030
# print a method signature tuple for a lambda definition
1031-
isreplerror = get(io, :REPLError, false)
10321031
local sig
10331032
returned_from_do = false
1034-
Base.with_output_color(isreplerror ? Base.bt_funcdef_color() : :nothing, io) do io
1033+
Base.with_output_color(get(io, :hascolor, false) ? stackframe_function_color() : :nothing, io) do io
10351034
if li.specTypes === Tuple
10361035
print(io, li.def.name, "(...)")
10371036
returned_from_do = true
@@ -1052,7 +1051,7 @@ function show_lambda_types(io::IO, li::Core.MethodInstance)
10521051
end
10531052
returned_from_do && return
10541053
first = true
1055-
print_style = isreplerror ? :bold : :nothing
1054+
print_style = get(io, :hascolor, false) ? :bold : :nothing
10561055
print_with_color(print_style, io, "(")
10571056
for i = 2:length(sig) # fixme (iter): `eachindex` with offset?
10581057
first || print(io, ", ")

base/stacktraces.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ function show_spec_linfo(io::IO, frame::StackFrame)
188188
if frame.func === empty_sym
189189
@printf(io, "ip:%#x", frame.pointer)
190190
else
191-
print_with_color(get(io, :REPLError, false) ? Base.bt_funcdef_color() : :nothing, io, string(frame.func))
191+
print_with_color(get(io, :hascolor, false) ? Base.stackframe_function_color() : :nothing, io, string(frame.func))
192192
end
193193
else
194194
linfo = get(frame.linfo)
@@ -200,14 +200,14 @@ function show_spec_linfo(io::IO, frame::StackFrame)
200200
end
201201
end
202202

203-
function show(io::IO, frame::StackFrame; full_path::Bool=false)
204-
isreplerror = get(io, :REPLError, false)
205-
print(io, isreplerror ? "" : " in ")
203+
function show(io::IO, frame::StackFrame; full_path::Bool=false,
204+
prefix = " in ")
205+
print(io, prefix)
206206
show_spec_linfo(io, frame)
207207
if frame.file !== empty_sym
208208
file_info = full_path ? string(frame.file) : basename(string(frame.file))
209209
print(io, " at ")
210-
Base.with_output_color(isreplerror ? Base.bt_linfo_color() : :nothing, io) do io
210+
Base.with_output_color(get(io, :hascolor, false) ? Base.stackframe_linfo_color() : :nothing, io) do io
211211
print(io, file_info, ":")
212212
if frame.line >= 0
213213
print(io, frame.line)

doc/manual/interacting-with-julia.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,5 +307,5 @@ informational messages in cyan you can add the following to your ``juliarc.jl``
307307

308308
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::
309309

310-
ENV["JULIA_BT_FUNCTION_COLOR"] = :green # Color of function names
311-
ENV["JULIA_BT_LINFO_COLOR"] = :yellow # Color of file name and line number
310+
ENV["JULIA_STACKFRAME_FUNCTION_COLOR"] = :green # Color of function names
311+
ENV["JULIA_STACKFRAME_LINFO_COLOR"] = :yellow # Color of file name and line number

0 commit comments

Comments
 (0)