Skip to content

Commit 1cd34de

Browse files
fix TermLogger and ProgressLogging compatibility (#263)
1 parent 25e154d commit 1cd34de

File tree

7 files changed

+59
-39
lines changed

7 files changed

+59
-39
lines changed

src/_tables.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,8 @@ function assert_table_arguments(
111111
# check footer
112112
if !isnothing(footer)
113113
isa(footer, Function) ||
114-
(length(footer)) != N_cols && push!(
115-
problems,
116-
"Got footer with length $(length(footer)), expected $N_cols",
117-
)
114+
(length(footer)) != N_cols &&
115+
push!(problems, "Got footer with length $(length(footer)), expected $N_cols")
118116
(!isa(footer_style, String) && length(footer_style) != N_cols) && push!(
119117
problems,
120118
"Got footer_style with length $(length(footer_style)), expected $N_cols",

src/logs.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Logs
22

3-
using ProgressLogging: asprogress
3+
using ProgressLogging: asprogress, ProgressLevel
44
using InteractiveUtils
55
using Dates: Dates
66
using Logging
@@ -78,7 +78,7 @@ function TermLogger(io::IO, theme::Theme = TERM_THEME[])
7878
end
7979

8080
# set logger beavior
81-
Logging.min_enabled_level(logger::TermLogger) = Logging.Info
81+
Logging.min_enabled_level(logger::TermLogger) = ProgressLevel
8282

8383
Logging.shouldlog(logger::TermLogger, level, _module, group, id) = true
8484
Logging.catch_exceptions(logger::TermLogger) = true
@@ -146,10 +146,12 @@ function handle_progress(logger::TermLogger, prog)
146146

147147
# render
148148
if all(map(j -> j.finished, pbar.jobs))
149-
map(j -> removejob!(pbar, j), pbar.jobs)
149+
while length(pbar.jobs) > 0
150+
removejob!(pbar, first(pbar.jobs))
151+
end
150152
stop!(pbar)
151153
else
152-
render(pbar)
154+
render(pbar, logger.io)
153155
end
154156
end
155157

@@ -322,8 +324,10 @@ function Logging.handle_message(
322324
_progress = asprogress(lvl, msg, _mod, group, id, file, line; kwargs...)
323325
isnothing(_progress) || return handle_progress(logger, _progress)
324326

325-
# generate log
327+
# generate log
326328
logged = sprint(print_log_message, logger, lvl, msg, _mod, file, line, kwargs)
329+
write(logger.io, logged)
330+
flush(logger.io)
327331

328332
# restore stdout
329333
NOCOLOR[] && (logged = cleantext(logged))

src/markdown.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,10 @@ function parse_md(tb::Markdown.Table; width = console_width())::String
330330
)
331331
end
332332

333-
parse_md(link::Markdown.Link; kwargs...)::String =
334-
"{white bold}$(parse_md(link.text; inline=true)){/white bold} {dim}($(link.url)){/dim}"
333+
parse_md(
334+
link::Markdown.Link;
335+
kwargs...,
336+
)::String = "{white bold}$(parse_md(link.text; inline=true)){/white bold} {dim}($(link.url)){/dim}"
335337

336338
"""
337339
function parse_md(ad::Markdown.Admonition; width = console_width(), kwargs...)::String

src/progress.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ function update!(job::ProgressJob; i = nothing)
186186
if !isnothing(job.N) && job.i job.N
187187
stop!(job)
188188
else
189-
job.i += something(i, 1)
189+
job.i = something(i, 1 + job.i)
190190
end
191191
return nothing
192192
end
@@ -465,22 +465,22 @@ function render(job::ProgressJob)::String
465465
end
466466

467467
"""
468-
render(job::ProgressJob, pbar::ProgressBar)::String
468+
render(pbar::ProgressBar, [io=stdout])::String
469469
470470
Render a `ProgressBar`.
471471
472472
When a progress bar is first rendered, this function uses
473473
ANSI codes to change the scrolling region of the terminal
474474
window to create a space at the bottom where the bar's visuals
475-
can be displayed. This allows for thext printed to `stdout` to
475+
can be displayed. This allows for text printed to `stdout` to
476476
still be visualized. On subsequent calls, this function
477477
ensures that the height of the reserved space matches the
478478
number of running jobs.
479479
480-
All fo this requires a bit of careful work in moving the
480+
All of this requires a bit of careful work in moving the
481481
cursor around and doing ANSI magic.
482482
"""
483-
function render(pbar::ProgressBar)
483+
function render(pbar::ProgressBar, io = stdout)
484484
# check if running
485485
pbar.running || return nothing
486486

@@ -539,7 +539,8 @@ function render(pbar::ProgressBar)
539539

540540
# restore position and write
541541
move_to_line(iob, pbar.renderstatus.scrollline)
542-
return print(String(take!(iob)))
542+
write(io, String(take!(iob)))
543+
return flush(io)
543544
end
544545

545546
# ---------------------------------------------------------------------------- #

src/renderables.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ abstract type AbstractRenderable end
3535

3636
Measure(renderable::AbstractRenderable) = renderable.measure
3737

38-
info(r::AbstractRenderable)::String =
39-
"\e[38;5;117m$(typeof(r)) <: AbstractRenderable\e[0m \e[2m(h:$(r.measure.h), w:$(r.measure.w))\e[0m"
38+
info(
39+
r::AbstractRenderable,
40+
)::String = "\e[38;5;117m$(typeof(r)) <: AbstractRenderable\e[0m \e[2m(h:$(r.measure.h), w:$(r.measure.w))\e[0m"
4041

4142
"""
4243
Base.string(r::AbstractRenderable)::String

src/repr.jl

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,15 @@ function termshow(io::IO, obj::AbstractDict; kwargs...)
113113

114114
# prepare text renderables
115115
_keys = RenderableText.(short_string.(keys(obj)); style = theme.repr_accent * " bold")
116-
ktypes =
117-
RenderableText.(
118-
map(k -> "{{" * short_string(typeof(k)) * "}}", collect(keys(obj)));
119-
style = theme.repr_type * " dim",
120-
)
116+
ktypes = RenderableText.(
117+
map(k -> "{{" * short_string(typeof(k)) * "}}", collect(keys(obj)));
118+
style = theme.repr_type * " dim",
119+
)
121120
vals = RenderableText.(short_string.(values(obj)); style = theme.repr_values * " bold")
122-
vtypes =
123-
RenderableText.(
124-
map(k -> "{{" * short_string(typeof(k)) * "}}", collect(values(obj)));
125-
style = theme.repr_type * " dim",
126-
)
121+
vtypes = RenderableText.(
122+
map(k -> "{{" * short_string(typeof(k)) * "}}", collect(values(obj)));
123+
style = theme.repr_type * " dim",
124+
)
127125

128126
content = OrderedDict(
129127
:type => ktypes,

test/15_test_progress.jl

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Term.Progress:
55
CompletedColumn, SeparatorColumn, ProgressColumn, DescriptionColumn, TextColumn
66

77
using ProgressLogging
8+
import ProgressLogging.Logging.global_logger
89

910
@testset "\e[34mProgress - jobs" begin
1011
pbar = ProgressBar()
@@ -27,7 +28,7 @@ using ProgressLogging
2728
update!(j1)
2829
@test j1.i == 1
2930
update!(j1; i = 10)
30-
@test j1.i == 11
31+
@test j1.i == 10 # ? In what situation would we want to += i?
3132

3233
@test getjob(pbar, j1.id).id == j1.id
3334

@@ -142,12 +143,27 @@ end
142143
end
143144
end
144145
end
145-
# @testset "\e[34mProgress ProgressLogging" begin
146-
# install_term_logger()
147-
148-
# @test_nothrow begin
149-
# @progress "inner... $i" for j in 1:10
150-
# sleep(0.01)
151-
# end
152-
# end
153-
# end
146+
@testset "\e[34mProgress ProgressLogging" begin
147+
install_term_logger()
148+
149+
@progress "loop" for j in 1:10
150+
sleep(0.01)
151+
end
152+
end
153+
@testset "\e[34mProgress ProgressLogging custom io" begin
154+
buffer = IOBuffer()
155+
io = IOContext(buffer, :displaysize => (30, 1000), :color => false)
156+
157+
logger = Term.Logs.TermLogger(io, Term.TERM_THEME[])
158+
global_logger(logger)
159+
160+
@info "logger message"
161+
out = String(take!(buffer))
162+
@test occursin("logger message", out)
163+
164+
@progress "loop" for j in 1:10
165+
sleep(0.1)
166+
end
167+
out = String(take!(buffer))
168+
@test occursin("90%", out) # Check it runs to completion
169+
end

0 commit comments

Comments
 (0)