Skip to content

Commit

Permalink
introduce JULIA_ERROR_COLOR to control how errors are printed
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Dec 11, 2016
1 parent 579cfcb commit 2fc856d
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion base/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function ip_matches_func(ip, func::Symbol)
end

function display_error(io::IO, er, bt)
Base.with_output_color(:red, io) do io
Base.with_output_color(Base.error_color(), io) do io
print(io, "ERROR: ")
# remove REPL-related frames from interactive printing
eval_ind = findlast(addr->ip_matches_func(addr, :eval), bt)
Expand Down
4 changes: 3 additions & 1 deletion base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ text_colors

have_color = false
default_color_warn = :red
default_color_error = :red
default_color_info = :cyan
if is_windows()
default_color_input = :normal
Expand All @@ -57,6 +58,7 @@ function repl_color(key, default)
haskey(text_colors, c_conv) ? c_conv : default
end

error_color() = repl_color("JULIA_ERROR_COLOR", default_color_error)
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)]
Expand Down Expand Up @@ -101,7 +103,7 @@ end

display_error(er) = display_error(er, [])
function display_error(er, bt)
with_output_color(:red, STDERR) do io
with_output_color(Base.error_color(), STDERR) do io
print(io, "ERROR: ")
showerror(io, er, bt)
println(io)
Expand Down
2 changes: 1 addition & 1 deletion base/pkg/entry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ function status(io::IO, pkg::AbstractString, ver::VersionNumber, fix::Bool)
LibGit2.isdirty(prepo) && push!(attrs,"dirty")
isempty(attrs) || print(io, " (",join(attrs,", "),")")
catch err
print_with_color(:red, io, " broken-repo (unregistered)")
print_with_color(Base.error_color(), io, " broken-repo (unregistered)")
finally
finalize(prepo)
end
Expand Down
6 changes: 3 additions & 3 deletions base/replutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ function show_method_candidates(io::IO, ex::MethodError, kwargs::Vector=Any[])
t_in === Union{} && special && i == 1 && break
if t_in === Union{}
if Base.have_color
Base.with_output_color(:red, buf) do buf
Base.with_output_color(Base.error_color(), buf) do buf
print(buf, "::$sigstr")
end
else
Expand Down Expand Up @@ -503,7 +503,7 @@ function show_method_candidates(io::IO, ex::MethodError, kwargs::Vector=Any[])
print(buf, ", ")
end
if Base.have_color
Base.with_output_color(:red, buf) do buf
Base.with_output_color(Base.error_color(), buf) do buf
print(buf, "::$sigstr")
end
else
Expand All @@ -529,7 +529,7 @@ function show_method_candidates(io::IO, ex::MethodError, kwargs::Vector=Any[])
end
end
if !isempty(unexpected)
Base.with_output_color(:red, buf) do buf
Base.with_output_color(Base.error_color(), buf) do buf
plur = length(unexpected) > 1 ? "s" : ""
print(buf, " got unsupported keyword argument$plur \"", join(unexpected, "\", \""), "\"")
end
Expand Down
2 changes: 1 addition & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ function show_expr_type(io::IO, ty, emph)
end
end

emphasize(io, str::AbstractString) = have_color ? print_with_color(:red, io, str) : print(io, uppercase(str))
emphasize(io, str::AbstractString) = have_color ? print_with_color(Base.error_color(), io, str) : print(io, uppercase(str))

show_linenumber(io::IO, line) = print(io," # line ",line,':')
show_linenumber(io::IO, line, file) = print(io," # ", file,", line ",line,':')
Expand Down
2 changes: 1 addition & 1 deletion base/task.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ function task_done_hook(t::Task)
if !suppress_excp_printing(t)
let bt = t.backtrace
# run a new task to print the error for us
@schedule with_output_color(:red, STDERR) do io
@schedule with_output_color(Base.error_color(), STDERR) do io
print(io, "ERROR (unhandled task failure): ")
showerror(io, result, bt)
println(io)
Expand Down
6 changes: 3 additions & 3 deletions test/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ end
pos_stable(x) = x > 0 ? x : zero(x)
pos_unstable(x) = x > 0 ? x : 0

tag = Base.have_color ? Base.text_colors[:red] : "UNION"
tag = Base.have_color ? Base.error_color() : "UNION"
@test warntype_hastag(pos_unstable, Tuple{Float64}, tag)
@test !warntype_hastag(pos_stable, Tuple{Float64}, tag)

Expand All @@ -80,13 +80,13 @@ end
Base.getindex(A::Stable, i) = A.A[i]
Base.getindex(A::Unstable, i) = A.A[i]

tag = Base.have_color ? Base.text_colors[:red] : "ARRAY{FLOAT64,N}"
tag = Base.have_color ? Base.error_color() : "ARRAY{FLOAT64,N}"
@test warntype_hastag(getindex, Tuple{Unstable{Float64},Int}, tag)
@test !warntype_hastag(getindex, Tuple{Stable{Float64,2},Int}, tag)
@test warntype_hastag(getindex, Tuple{Stable{Float64},Int}, tag)

# Make sure emphasis is not used for other functions
tag = Base.have_color ? Base.text_colors[:red] : "ANY"
tag = Base.have_color ? Base.error_color() : "ANY"
iob = IOBuffer()
show(iob, expand(:(x->x^2)))
str = String(take!(iob))
Expand Down

0 comments on commit 2fc856d

Please sign in to comment.