Skip to content
This repository was archived by the owner on Nov 20, 2025. It is now read-only.

Commit 76ea8a4

Browse files
committed
Improve ETA display and align progress percentage in progress bar
1 parent f9d102f commit 76ea8a4

2 files changed

Lines changed: 30 additions & 15 deletions

File tree

src/progressbar.jl

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,33 +55,35 @@ end
5555
function calculate_eta_text(current::Int, max::Int, start_time::Float64, is_complete::Bool, show_eta::Bool)::Tuple{String, String}
5656
# Early return if ETA is disabled or no progress yet
5757
if !show_eta || current == 0 || start_time == 0
58-
return @sprintf("%d%%", round(Int, calculate_percentage(current, max))), ""
58+
return @sprintf("%4d%%", round(Int, calculate_percentage(current, max))), ""
5959
end
6060

6161
elapsed = time() - start_time
6262

6363
if is_complete
64-
# Completed: only show total time, no progress percentage
65-
return "", " Time: $(format_time(elapsed))"
64+
# Completed: show 100% and total time
65+
return " 100%", " Time: $(format_time(elapsed))"
6666
elseif elapsed > 0
67-
# In progress: show progress percentage and ETA
68-
perc_text = @sprintf("%d%%", round(Int, calculate_percentage(current, max)))
67+
# In progress: show progress percentage and ETA (right-aligned to match "100%")
68+
perc_text = @sprintf("%4d%%", round(Int, calculate_percentage(current, max)))
6969
rate = current / elapsed
7070
remaining = max - current
7171
eta_seconds = remaining / rate
7272
return perc_text, " ETA: $(format_time(eta_seconds))"
7373
else
7474
# Edge case: no time elapsed yet
75-
return @sprintf("%d%%", round(Int, calculate_percentage(current, max))), ""
75+
return @sprintf("%4d%%", round(Int, calculate_percentage(current, max))), ""
7676
end
7777
end
7878

7979
# Render the progress bar string
8080
function render_progress_bar(io::IO, p::MiniProgressBar, perc::Float64,
8181
progress_text::String, eta_text::String, termwidth::Int)::Nothing
8282
# Calculate available width for progress bar
83+
# Use fixed width for progress text (4 chars for "100%" + 2 spaces = 6 total)
84+
progress_width = isempty(progress_text) ? 0 : 6
8385
max_progress_width = max(0, min(termwidth - textwidth(p.header) -
84-
textwidth(progress_text) - textwidth(eta_text) - PROGRESS_BAR_PADDING, p.width))
86+
progress_width - textwidth(eta_text) - PROGRESS_BAR_PADDING, p.width))
8587

8688
# Calculate filled and empty portions
8789
filled_width = max_progress_width * perc / 100
@@ -94,7 +96,13 @@ function render_progress_bar(io::IO, p::MiniProgressBar, perc::Float64,
9496
print(io, ANSI_CLEAR_LINE)
9597
printstyled(io, "[ Info:"; color = :cyan, bold = true)
9698
print(io, " ")
97-
print(io, p.header, " ")
99+
print(io, p.header)
100+
101+
# Print progress text before the bar (right-aligned percentage with 1 space before and after)
102+
if !isempty(progress_text)
103+
# Print 1 space, then right-align the percentage in 4 characters
104+
print(io, " ", lpad(strip(progress_text), 4), " ")
105+
end
98106

99107
# Draw filled portion
100108
printstyled(io, ""^n_filled; color = p.color)
@@ -110,9 +118,7 @@ function render_progress_bar(io::IO, p::MiniProgressBar, perc::Float64,
110118
printstyled(io, empty_char^(n_left - 1 + !hascolor); color = :light_black)
111119
end
112120

113-
# Print progress text and ETA
114-
printstyled(io, " "; color = :light_black)
115-
print(io, progress_text)
121+
# Print ETA after the bar
116122
!isempty(eta_text) && print(io, eta_text)
117123

118124
return nothing

test/test.jl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using SimulationLogging
2-
2+
function main()
33
prog = MiniProgressBar(
44
max = 100,
55
header = "processing",
@@ -11,13 +11,22 @@ prog = MiniProgressBar(
1111
start_progress(stdout, prog)
1212

1313
# Update progress in your loop
14-
for i in 1:100
15-
prog.current = i
14+
#for i in 1:100
15+
16+
t = 1
17+
tol = 100
18+
while t<tol
19+
prog.current = t
1620
show_progress(stdout, prog)
1721

1822
# Your computation here
1923
sleep(0.05)
24+
t += 1
2025
end
2126

2227
# Finish and show final result
23-
end_progress(stdout, prog)
28+
prog.current = 100
29+
show_progress(stdout, prog)
30+
end_progress(stdout, prog)
31+
end
32+
main()

0 commit comments

Comments
 (0)