1
0
Fork 0
forked from fun/fun

Added some progress bar foo to Console class. (0.37.48)

This commit is contained in:
Johannes Findeisen 2026-01-13 03:18:37 +01:00
commit 44cf629016
5 changed files with 147 additions and 7 deletions

View file

@ -12,6 +12,7 @@
*/
#include <strings.fun>
#include <io/process.fun>
class Console()
// Print a prompt and read a line (no trailing newline)
@ -48,5 +49,65 @@ class Console()
return 0
// otherwise loop again
fun progress(this, length)
return length
// Internal: best-effort terminal columns via `tput cols` (fallback to 80)
fun term_cols(this)
// Use builtin proc_run directly to avoid any method dispatch quirks
r = proc_run("tput cols")
if (r["code"] == 0)
s = str_trim(r["out"]) // trim trailing newline
if (len(s) > 0)
n = to_number(s)
if (n > 0)
return n
// Fallback if tput not available or not a TTY
return 80
// Draw/update a full-width progress bar on a single console line.
// - current: Number (0..total)
// - total: Number (>0)
// - label: Optional text shown left of the bar
// Returns the integer percent [0..100]. Adds a newline automatically at 100%.
fun progress(this, current, total, label)
cur = to_number(current)
tot = to_number(total)
if (tot <= 0)
tot = 1
pct = to_number((cur * 100) / tot)
if (pct < 0)
pct = 0
if (pct > 100)
pct = 100
cols = this.term_cols()
lbl = to_string(label)
if (len(lbl) > 0)
prefix = lbl + " "
else
prefix = ""
perc_str = to_string(pct) + "%"
// We render: [====....] plus percent; keep the whole line within terminal width
// Compute bar width: terminal cols minus prefix, brackets, space, and percent.
fixed = len(prefix) + 2 + 1 + len(perc_str) // [] + space + percent
bar_w = cols - fixed
if (bar_w < 10)
bar_w = 10 // minimal bar width for visibility
filled = to_number((bar_w * pct) / 100)
if (filled < 0)
filled = 0
if (filled > bar_w)
filled = bar_w
bar = "[" + str_repeat("=", filled) + str_repeat(" ", bar_w - filled) + "]"
line = prefix + bar + " " + perc_str
// In-place update: CR + full-width content (we already fit to cols), no newline
echo("\r" + line)
// When done, end the line cleanly
if (cur >= tot)
print("")
return pct