Skip to content

Commit 9c8943d

Browse files
authored
NFC: Factor out key-value formatting (#27)
For easier reuse, potentially in progress bars.
1 parent 8b6cdcd commit 9c8943d

File tree

1 file changed

+36
-33
lines changed

1 file changed

+36
-33
lines changed

src/TerminalLogger.jl

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,6 @@ shouldlog(logger::TerminalLogger, level, _module, group, id) =
5353

5454
min_enabled_level(logger::TerminalLogger) = logger.min_level
5555

56-
# Formatting of values in key value pairs
57-
showvalue(io, msg) = show(io, "text/plain", msg)
58-
function showvalue(io, e::Tuple{Exception,Any})
59-
ex,bt = e
60-
showerror(io, ex, bt; backtrace = bt!=nothing)
61-
end
62-
showvalue(io, ex::Exception) = showerror(io, ex)
63-
6456
function default_logcolor(level)
6557
level < Info ? :blue :
6658
level < Warn ? Base.info_color() :
@@ -146,6 +138,35 @@ function format_message(message::AbstractString, prefix_width, io_context)
146138
msglines
147139
end
148140

141+
# Formatting of values in key value pairs
142+
showvalue(io, msg) = show(io, "text/plain", msg)
143+
function showvalue(io, e::Tuple{Exception,Any})
144+
ex,bt = e
145+
showerror(io, ex, bt; backtrace = bt!=nothing)
146+
end
147+
showvalue(io, ex::Exception) = showerror(io, ex)
148+
149+
# Generate a text representation of all key value pairs, split into lines with
150+
# per-line indentation as an integer.
151+
function format_key_value_pairs(kwargs, io_context)
152+
msglines = Tuple{Int,String}[]
153+
valbuf = IOBuffer()
154+
dsize = displaysize(io_context)
155+
rows_per_value = max(1, dsize[1]÷(length(kwargs)+1))
156+
valio = IOContext(valbuf, IOContext(io_context, :displaysize=>(rows_per_value,dsize[2]-3)))
157+
for (key,val) in kwargs
158+
showvalue(valio, val)
159+
vallines = split(String(take!(valbuf)), '\n')
160+
if length(vallines) == 1
161+
push!(msglines, (2,SubString("$key = $(vallines[1])")))
162+
else
163+
push!(msglines, (2,SubString("$key =")))
164+
append!(msglines, ((3,line) for line in vallines))
165+
end
166+
end
167+
msglines
168+
end
169+
149170
function findbar(bartree, id)
150171
if !(bartree isa AbstractArray)
151172
bartree.data.id === id && return bartree
@@ -169,7 +190,7 @@ end
169190

170191
const BAR_MESSAGE_ID = gensym(:BAR_MESSAGE_ID)
171192

172-
function handle_progress(logger, progress)
193+
function handle_progress(logger, progress, kwargs)
173194
node = findbar(logger.bartrees, progress.id)
174195
if node === nothing
175196
# Don't do anything when it's already done:
@@ -244,39 +265,21 @@ function handle_message(logger::TerminalLogger, level, message, _module, group,
244265

245266
progress = asprogress(level, message, _module, group, id, filepath, line; kwargs...)
246267
if progress !== nothing
247-
handle_progress(logger, progress)
268+
handle_progress(logger, progress, kwargs)
248269
return
249270
end
250271

251-
substr(s) = SubString(s, 1, length(s)) # julia 0.6 compat
252-
253272
color,prefix,suffix = logger.meta_formatter(level, _module, group, id, filepath, line)
254273

255274
# Generate a text representation of the message
256275
dsize = displaysize(logger.stream)
257-
msglines = format_message(message, textwidth(prefix),
258-
IOContext(logger.stream, :displaysize=>(dsize[1],dsize[2]-2)))
276+
context = IOContext(logger.stream, :displaysize=>(dsize[1],dsize[2]-2))
277+
msglines = format_message(message, textwidth(prefix), context)
259278
# Add indentation level
260279
msglines = [(0,l) for l in msglines]
261-
# Generate a text representation of all key value pairs, split into lines.
262280
if !isempty(kwargs)
263-
valbuf = IOBuffer()
264-
rows_per_value = max(1, dsize[1]÷(length(kwargs)+1))
265-
valio = IOContext(IOContext(valbuf, logger.stream),
266-
:displaysize=>(rows_per_value,dsize[2]-5))
267-
if logger.show_limited
268-
valio = IOContext(valio, :limit=>true)
269-
end
270-
for (key,val) in kwargs
271-
showvalue(valio, val)
272-
vallines = split(String(take!(valbuf)), '\n')
273-
if length(vallines) == 1
274-
push!(msglines, (2,substr("$key = $(vallines[1])")))
275-
else
276-
push!(msglines, (2,substr("$key =")))
277-
append!(msglines, ((3,line) for line in vallines))
278-
end
279-
end
281+
ctx = logger.show_limited ? IOContext(context, :limit=>true) : context
282+
append!(msglines, format_key_value_pairs(kwargs, ctx))
280283
end
281284

282285
# Format lines as text with appropriate indentation and with a box
@@ -289,7 +292,7 @@ function handle_message(logger::TerminalLogger, level, message, _module, group,
289292
(isempty(suffix) ? 0 : length(suffix)+minsuffixpad)
290293
justify_width = min(logger.right_justify, dsize[2])
291294
if nonpadwidth > justify_width && !isempty(suffix)
292-
push!(msglines, (0,substr("")))
295+
push!(msglines, (0,SubString("")))
293296
minsuffixpad = 0
294297
nonpadwidth = 2 + length(suffix)
295298
end

0 commit comments

Comments
 (0)