|
21 | 21 | const NONINTERACTIVE_TIME_GRANULARITY = Ref(2.0) |
22 | 22 | const PROGRESS_BAR_PERCENTAGE_GRANULARITY = Ref(0.1) |
23 | 23 |
|
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 | | - |
43 | 24 | function start_progress(io::IO, _::MiniProgressBar) |
44 | 25 | ansi_disablecursor = "\e[?25l" |
45 | 26 | print(io, ansi_disablecursor) |
@@ -67,18 +48,22 @@ function show_progress(io::IO, p::MiniProgressBar) |
67 | 48 | end |
68 | 49 | p.prev = p.current |
69 | 50 | 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 |
72 | 61 | print(io, " "^p.indent) |
73 | 62 | printstyled(io, p.header, color=p.color, bold=true) |
74 | 63 | print(io, " [") |
75 | 64 | print(io, "="^n_filled, ">") |
76 | 65 | 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) |
82 | 67 | print(io, "\r") |
83 | 68 | end |
84 | 69 |
|
|
0 commit comments