Skip to content

Commit c4be257

Browse files
KristofferCIanButterworth
authored andcommitted
make progressbar adapt to size of terminal (#2437)
(cherry picked from commit 8139716)
1 parent 2154fae commit c4be257

File tree

1 file changed

+11
-26
lines changed

1 file changed

+11
-26
lines changed

src/MiniProgressBars.jl

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,6 @@ end
2121
const NONINTERACTIVE_TIME_GRANULARITY = Ref(2.0)
2222
const PROGRESS_BAR_PERCENTAGE_GRANULARITY = Ref(0.1)
2323

24-
function pretend_cursor()
25-
io = stderr
26-
bar = MiniProgressBar(; indent=2, header = "Progress", color = Base.info_color(),
27-
percentage=false, always_reprint=true)
28-
bar.max = 40
29-
start_progress(io, bar)
30-
sleep(0.5)
31-
for i in 1:40
32-
bar.current = i
33-
print_progress_bottom(io)
34-
println("Downloading ... $i")
35-
show_progress(io, bar)
36-
x = randstring(7)
37-
sleep(0.01)
38-
end
39-
end_progress(io, bar)
40-
print("Hello!")
41-
end
42-
4324
function start_progress(io::IO, _::MiniProgressBar)
4425
ansi_disablecursor = "\e[?25l"
4526
print(io, ansi_disablecursor)
@@ -67,18 +48,22 @@ function show_progress(io::IO, p::MiniProgressBar)
6748
end
6849
p.prev = p.current
6950
p.has_shown = true
70-
n_filled = ceil(Int, p.width * perc / 100)
71-
n_left = p.width - n_filled
51+
52+
progress_text = if p.percentage
53+
@sprintf "%2.1f %%" perc
54+
else
55+
sprint(p.current, "/", p.max)
56+
end
57+
58+
max_progress_width = max(0, min(displaysize(io)[2] - textwidth(p.header) - textwidth(progress_text) - 10 , p.width))
59+
n_filled = ceil(Int, max_progress_width * perc / 100)
60+
n_left = max_progress_width - n_filled
7261
print(io, " "^p.indent)
7362
printstyled(io, p.header, color=p.color, bold=true)
7463
print(io, " [")
7564
print(io, "="^n_filled, ">")
7665
print(io, " "^n_left, "] ", )
77-
if p.percentage
78-
@printf io "%2.1f %%" perc
79-
else
80-
print(io, p.current, "/", p.max)
81-
end
66+
print(io, progress_text)
8267
print(io, "\r")
8368
end
8469

0 commit comments

Comments
 (0)