Skip to content

Commit

Permalink
Deprecate print_with_color (JuliaLang#25522)
Browse files Browse the repository at this point in the history
This deprecates print_with_color in favor of printstyled, which accepts
the color as a keyword argument.
  • Loading branch information
ararslan committed Jan 27, 2018
1 parent 911dc19 commit 9877594
Show file tree
Hide file tree
Showing 19 changed files with 88 additions and 90 deletions.
2 changes: 1 addition & 1 deletion DISTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ for result in eachrow(results)
else
continue
end
print_with_color(color, result[:package], ": Release ", a, " -> Backport ", b, "\n")
printstyled(result[:package], ": Release ", a, " -> Backport ", b, "\n", color=color)
end
```

Expand Down
2 changes: 1 addition & 1 deletion base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ function display_error(io::IO, er, bt)
io = redirect(io, log_error_to, st[1])
end
end
print_with_color(Base.error_color(), io, "ERROR: "; bold = true)
printstyled(io, "ERROR: "; bold=true, color=Base.error_color())
# remove REPL-related frames from interactive printing
eval_ind = findlast(addr->ip_matches_func(addr, :eval), bt)
if eval_ind !== nothing
Expand Down
10 changes: 6 additions & 4 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1023,8 +1023,8 @@ function info(io::IO, msg...; prefix="INFO: ")
depwarn("`info()` is deprecated, use `@info` instead.", :info)
buf = IOBuffer()
iob = redirect(IOContext(buf, io), log_info_to, :info)
print_with_color(info_color(), iob, prefix; bold = true)
println_with_color(info_color(), iob, chomp(string(msg...)))
printstyled(iob, prefix; bold=true, color=info_color())
printstyled(iob, chomp(string(msg...)), '\n', color=info_color())
print(io, String(take!(buf)))
return
end
Expand Down Expand Up @@ -1061,8 +1061,8 @@ function warn(io::IO, msg...;
end
buf = IOBuffer()
iob = redirect(IOContext(buf, io), log_warn_to, :warn)
print_with_color(warn_color(), iob, prefix; bold = true)
print_with_color(warn_color(), iob, str)
printstyled(iob, prefix; bold=true, color=warn_color())
printstyled(iob, str, color=warn_color())
if bt !== nothing
show_backtrace(iob, bt)
end
Expand Down Expand Up @@ -1410,6 +1410,8 @@ end
@deprecate PipeBuffer(data, maxsize) PipeBuffer(data, maxsize = maxsize)
@deprecate unsafe_wrap(T, pointer, dims, own) unsafe_wrap(T, pointer, dims, own = own)

@deprecate print_with_color(color, args...; kwargs...) printstyled(args...; kwargs..., color=color)

# END 0.7 deprecations

# BEGIN 1.0 deprecations
Expand Down
10 changes: 5 additions & 5 deletions base/docs/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ repl_search(s) = repl_search(STDOUT, s)

function repl_corrections(io::IO, s)
print(io, "Couldn't find ")
print_with_color(:cyan, io, s, '\n')
printstyled(io, s, '\n', color=:cyan)
print_correction(io, s)
end
repl_corrections(s) = repl_corrections(STDOUT, s)
Expand All @@ -163,13 +163,13 @@ function repl_latex(io::IO, s::String)
latex = symbol_latex(s)
if !isempty(latex)
print(io, "\"")
print_with_color(:cyan, io, s)
printstyled(io, s, color=:cyan)
print(io, "\" can be typed by ")
print_with_color(:cyan, io, latex, "<tab>")
printstyled(io, latex, "<tab>", color=:cyan)
println(io, '\n')
elseif any(c -> haskey(symbols_latex, string(c)), s)
print(io, "\"")
print_with_color(:cyan, io, s)
printstyled(io, s, color=:cyan)
print(io, "\" can be typed by ")
with_output_color(:cyan, io) do io
for c in s
Expand Down Expand Up @@ -310,7 +310,7 @@ function printmatch(io::IO, word, match)
is, _ = bestmatch(word, match)
for (i, char) = enumerate(match)
if i in is
print_with_color(:bold, io, char)
printstyled(io, char, bold=true)
else
print(io, char)
end
Expand Down
2 changes: 1 addition & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,8 @@ export
prevind,
print,
print_shortest,
print_with_color,
println,
printstyled,
repeat,
replace,
replace!,
Expand Down
4 changes: 2 additions & 2 deletions base/markdown/IPython/IPython.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ latex(io::IO, tex::LaTeX) =
latexinline(io::IO, tex::LaTeX) =
print(io, '$', tex.formula, '$')

term(io::IO, tex::LaTeX, cols) = print_with_color(:magenta, io, tex.formula, '\n')
terminline(io::IO, tex::LaTeX) = print_with_color(:magenta, io, tex.formula)
term(io::IO, tex::LaTeX, cols) = printstyled(io, tex.formula, '\n', color=:magenta)
terminline(io::IO, tex::LaTeX) = printstyled(io, tex.formula, color=:magenta)
12 changes: 6 additions & 6 deletions base/markdown/render/terminal/render.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ function term(io::IO, md::Admonition, columns)
elseif lowercase(md.title) == "tip"
col = :green
end
print_with_color(col, io, " "^margin, ""; bold = true)
print_with_color(col, io, isempty(md.title) ? md.category : md.title; bold = true)
print_with_color(col, io, "\n", " "^margin, "", "\n"; bold = true)
printstyled(io, " "^margin, ""; color=col, bold=true)
printstyled(io, isempty(md.title) ? md.category : md.title; color=col, bold=true)
printstyled(io, "\n", " "^margin, "", "\n"; color=col, bold=true)
s = sprint(term, md.content, columns - 10; context=io)
for line in split(rstrip(s), "\n")
print_with_color(col, io, " "^margin, ""; bold = true)
printstyled(io, " "^margin, ""; color=col, bold=true)
println(io, line)
end
end

function term(io::IO, f::Footnote, columns)
print(io, " "^margin, "")
print_with_color(:bold, io, "[^$(f.id)]")
printstyled(io, "[^$(f.id)]", bold=true)
println(io, "\n", " "^margin, "")
s = sprint(term, f.text, columns - 10; context=io)
for line in split(rstrip(s), "\n")
Expand Down Expand Up @@ -153,7 +153,7 @@ function terminline(io::IO, md::Link)
end

function terminline(io::IO, code::Code)
print_with_color(:cyan, io, code.code)
printstyled(io, code.code, color=:cyan)
end

terminline(io::IO, x) = show(io, MIME"text/plain"(), x)
Expand Down
6 changes: 3 additions & 3 deletions base/pkg/entry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ 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(Base.error_color(), io, " broken-repo (unregistered)")
printstyled(io, " broken-repo (unregistered)", color=Base.error_color())
finally
close(prepo)
end
else
print_with_color(Base.warn_color(), io, "non-repo (unregistered)")
printstyled(io, "non-repo (unregistered)", color=Base.warn_color())
end
println(io)
end
Expand Down Expand Up @@ -745,7 +745,7 @@ struct PkgTestError <: Exception
end

function Base.showerror(io::IO, ex::PkgTestError, bt; backtrace=true)
print_with_color(Base.error_color(), io, ex.msg)
printstyled(io, ex.msg, color=Base.error_color())
end

function test(pkgs::Vector{AbstractString}; coverage::Bool=false)
Expand Down
2 changes: 0 additions & 2 deletions base/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,6 @@ precompile(Tuple{typeof(Base.uvfinalize), Base.PipeEndpoint})
precompile(Tuple{typeof(Base.uvfinalize), Base.Process})
precompile(Tuple{getfield(Base.Filesystem, Symbol("#kw##rm")), Array{Any, 1}, typeof(Base.Filesystem.rm), String})
precompile(Tuple{getfield(Base, Symbol("#kw##info")), Array{Any, 1}, typeof(Base.info), Base.IOStream, String})
precompile(Tuple{getfield(Base, Symbol("#kw##print_with_color")), Array{Any, 1}, typeof(Base.print_with_color), Symbol, Base.IOStream, String})
precompile(Tuple{getfield(Base, Symbol("#kw##readbytes!")), Array{Any, 1}, typeof(Base.readbytes!), Base.IOStream, Array{UInt8, 1}, Int32})
precompile(Tuple{getfield(Base, Symbol("#kw##spawn")), Array{Any, 1}, typeof(Base.spawn), Base.Cmd, Tuple{Base.Pipe, Base.TTY, Base.IOStream}})
precompile(Tuple{getfield(Base, Symbol("#kw##systemerror")), Array{Any, 1}, typeof(Base.systemerror), Symbol, Bool})
Expand Down Expand Up @@ -1175,7 +1174,6 @@ precompile(Tuple{typeof(Base.precompilableerror), Base.LoadError, Bool})
precompile(Tuple{typeof(Base.precompilableerror), Base.PrecompilableError, Bool})
precompile(Tuple{typeof(Base.__precompile__)})
precompile(Tuple{typeof(Base.__precompile__), Bool})
precompile(Tuple{typeof(Base.println_with_color), Symbol, Base.IOStream, Base.SubString{String}})
precompile(Tuple{typeof(Base.push!), Array{Tuple{String, Float64}, 1}, Tuple{String, Float64}})
precompile(Tuple{typeof(Base.put!), Base.Channel{Any}, Tuple{Nothing, Nothing}})
precompile(Tuple{typeof(Base.put_buffered), Base.Channel{Any}, Tuple{Nothing, Nothing}})
Expand Down
12 changes: 6 additions & 6 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,12 @@ The following properties are in common use:
```jldoctest
julia> io = IOBuffer();
julia> print_with_color(:red, IOContext(io, :color => true), "string")
julia> printstyled(IOContext(io, :color => true), "string", color=:red)
julia> String(take!(io))
"\e[31mstring\e[39m"
julia> print_with_color(:red, io, "string")
julia> printstyled(io, "string", color=:red)
julia> String(take!(io))
"string"
Expand Down Expand Up @@ -907,7 +907,7 @@ end
is_expected_union(u::Union) = u.a == Nothing || u.b == Nothing || u.a == Missing || u.b == Missing

emphasize(io, str::AbstractString, col = Base.error_color()) = get(io, :color, false) ?
print_with_color(col, io, str; bold = true) :
printstyled(io, str; color=col, bold=true) :
print(io, uppercase(str))

show_linenumber(io::IO, line) = print(io, "#= line ", line, " =#")
Expand Down Expand Up @@ -1509,7 +1509,7 @@ function show_tuple_as_call(io::IO, name::Symbol, sig::Type)
# print a method signature tuple for a lambda definition
color = get(io, :color, false) && get(io, :backtrace, false) ? stackframe_function_color() : :nothing
if sig === Tuple
Base.print_with_color(color, io, name, "(...)")
Base.printstyled(io, name, "(...)", color=color)
return
end
sig = unwrap_unionall(sig).parameters
Expand All @@ -1529,13 +1529,13 @@ function show_tuple_as_call(io::IO, name::Symbol, sig::Type)
end
first = true
print_style = get(io, :color, false) && get(io, :backtrace, false) ? :bold : :nothing
print_with_color(print_style, io, "(")
printstyled(io, "(", color=print_style)
for i = 2:length(sig) # fixme (iter): `eachindex` with offset?
first || print(io, ", ")
first = false
print(io, "::", sig[i])
end
print_with_color(print_style, io, ")")
printstyled(io, ")", color=print_style)
nothing
end

Expand Down
5 changes: 4 additions & 1 deletion base/stacktraces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,10 @@ function show_spec_linfo(io::IO, frame::StackFrame)
elseif frame.func === top_level_scope_sym
print(io, "top-level scope")
else
print_with_color(get(io, :color, false) && get(io, :backtrace, false) ? Base.stackframe_function_color() : :nothing, io, string(frame.func))
color = get(io, :color, false) && get(io, :backtrace, false) ?
Base.stackframe_function_color() :
:nothing
printstyled(io, string(frame.func), color=color)
end
elseif frame.linfo isa Core.MethodInstance
if isa(frame.linfo.def, Method)
Expand Down
16 changes: 6 additions & 10 deletions base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -344,22 +344,18 @@ function with_output_color(f::Function, color::Union{Int, Symbol}, io::IO, args.
end

"""
print_with_color(color::Union{Symbol, Int}, [io], xs...; bold::Bool = false)
printstyled([io], xs...; bold::Bool=false, color::Union{Symbol,Int}=:normal)
Print `xs` in a color specified as a symbol.
Print `xs` in a color specified as a symbol or integer, optionally in bold.
`color` may take any of the values $(Base.available_text_colors_docstring)
or an integer between 0 and 255 inclusive. Note that not all terminals support 256 colors.
If the keyword `bold` is given as `true`, the result will be printed in bold.
"""
print_with_color(color::Union{Int, Symbol}, io::IO, msg...; bold::Bool = false) =
with_output_color(print, color, io, msg...; bold = bold)
print_with_color(color::Union{Int, Symbol}, msg...; bold::Bool = false) =
print_with_color(color, STDOUT, msg...; bold = bold)
println_with_color(color::Union{Int, Symbol}, io::IO, msg...; bold::Bool = false) =
with_output_color(println, color, io, msg...; bold = bold)
println_with_color(color::Union{Int, Symbol}, msg...; bold::Bool = false) =
println_with_color(color, STDOUT, msg...; bold = bold)
printstyled(io::IO, msg...; bold::Bool=false, color::Union{Int,Symbol}=:normal) =
with_output_color(print, color, io, msg...; bold=bold)
printstyled(msg...; bold::Bool=false, color::Union{Int,Symbol}=:normal) =
printstyled(STDOUT, msg...; bold=bold, color=color)

function julia_cmd(julia=joinpath(Sys.BINDIR, julia_exename()))
opts = JLOptions()
Expand Down
2 changes: 1 addition & 1 deletion doc/src/base/io-network.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Base.showcompact
Base.summary
Base.print
Base.println
Base.print_with_color
Base.printstyled
Base.sprint
Base.showerror
Base.dump
Expand Down
10 changes: 5 additions & 5 deletions stdlib/Logging/src/ConsoleLogger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Message formatting can be controlled by setting keyword arguments:
* `meta_formatter` is a function which takes the log event metadata
`(level, _module, group, id, file, line)` and returns a color (as would be
passed to print_with_color), prefix and suffix for the log message. The
passed to printstyled), prefix and suffix for the log message. The
default is to prefix with the log level and a suffix containing the module,
file and line location.
* `show_limited` limits the printing of large data structures to something
Expand Down Expand Up @@ -126,7 +126,7 @@ function handle_message(logger::ConsoleLogger, level, message, _module, group, i
nonpadwidth = 2 + (isempty(prefix) || length(msglines) > 1 ? 0 : length(prefix)+1) +
msglines[end].indent + termlength(msglines[end].msg) +
(isempty(suffix) ? 0 : length(suffix)+minsuffixpad)
justify_width = min(logger.right_justify, dsize[2])
justify_width = min(logger.right_justify, dsize[2])
if nonpadwidth > justify_width && !isempty(suffix)
push!(msglines, (indent=0,msg=SubString("")))
minsuffixpad = 0
Expand All @@ -137,15 +137,15 @@ function handle_message(logger::ConsoleLogger, level, message, _module, group, i
i == 1 ? "" :
i < length(msglines) ? "" :
""
print_with_color(color, iob, boxstr, bold=true)
printstyled(iob, boxstr, bold=true, color=color)
if i == 1 && !isempty(prefix)
print_with_color(color, iob, prefix, " ", bold=true)
printstyled(iob, prefix, " ", bold=true, color=color)
end
print(iob, " "^indent, msg)
if i == length(msglines) && !isempty(suffix)
npad = max(0, justify_width - nonpadwidth) + minsuffixpad
print(iob, " "^npad)
print_with_color(:light_black, iob, suffix)
printstyled(iob, suffix, color=:light_black)
end
println(iob)
end
Expand Down
3 changes: 1 addition & 2 deletions stdlib/REPL/src/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ function _precompile_()
precompile(Tuple{typeof(Base.print), Base.IOContext{REPL.Terminals.TTYTerminal}, String, Module})
precompile(Tuple{typeof(Base.print), Base.IOContext{REPL.Terminals.TTYTerminal}, String, Symbol, String, Int32})
precompile(Tuple{typeof(Base.show_tuple_as_call), Base.IOContext{REPL.Terminals.TTYTerminal}, Symbol, Type})
precompile(Tuple{typeof(Base.print_with_color), Symbol, Base.IOContext{REPL.Terminals.TTYTerminal}, String})
precompile(Tuple{typeof(Base.print_with_color), Int64, Base.IOContext{REPL.Terminals.TTYTerminal}, String})
precompile(Tuple{typeof(getfield(Base, Symbol("#kw##printstyled"))), Array{Any,1}, typeof(Base.printstyled), Base.IOContext{REPL.Terminals.TTYTerminal}, String})
precompile(Tuple{typeof(Base.show), Base.IOContext{REPL.Terminals.TTYTerminal}, Core.MethodInstance})
precompile(Tuple{typeof(Base.StackTraces.show_spec_linfo), Base.IOContext{REPL.Terminals.TTYTerminal}, Base.StackTraces.StackFrame})
precompile(Tuple{getfield(Base, Symbol("#kw##show")), Array{Any, 1}, typeof(Base.show), Base.IOContext{REPL.Terminals.TTYTerminal}, Base.StackTraces.StackFrame})
Expand Down
Loading

0 comments on commit 9877594

Please sign in to comment.