Add progress bars

This commit is contained in:
Dimitri Lozeve 2022-09-10 21:06:37 +02:00
parent 2a8dab805e
commit ddd9bf0b29
3 changed files with 46 additions and 0 deletions

View file

@ -6,5 +6,6 @@
'("fancy/format" '("fancy/format"
"fancy/table" "fancy/table"
"fancy/rule" "fancy/rule"
"fancy/progress"
"fancy/spinner") "fancy/spinner")
optimize: #t debug: 'src) optimize: #t debug: 'src)

View file

@ -8,6 +8,7 @@
:dlozeve/fancy/format :dlozeve/fancy/format
:dlozeve/fancy/table :dlozeve/fancy/table
:dlozeve/fancy/rule :dlozeve/fancy/rule
:dlozeve/fancy/progress
:dlozeve/fancy/spinner) :dlozeve/fancy/spinner)
(def (main) (def (main)
@ -30,6 +31,11 @@ culpa qui officia deserunt mollit anim id est laborum."))
(display (table-footer tab)) (display (table-footer tab))
(displayln) (displayln)
(displayln) (displayln)
(def pbar (progress-bar 100 "[green]Progress: " percent: #f style: 'line))
(for ((i (in-range 100)))
(display (progress pbar (1+ i)))
(thread-sleep! 0.05))
(displayln)
(for ((i (in-range 20))) (for ((i (in-range 20)))
(display (spinner i "[yellow]Waiting:" (format "(computing ~d/20)" (1+ i)) (display (spinner i "[yellow]Waiting:" (format "(computing ~d/20)" (1+ i))
style: 'dots)) style: 'dots))

39
fancy/progress.ss Normal file
View file

@ -0,0 +1,39 @@
(export (struct-out progress-bar)
progress)
(import :std/misc/string
:std/format
:dlozeve/fancy/format)
(def +progress-styles+
'((ascii . (#\[ #\= #\> #\ #\]))
(line . (#\┝ #\━ #\━ #\ #\┥))
(double . (#\╞ #\═ #\═ #\ #\╡))
(block . (#\│ #\█ #\█ #\ #\│))
(halfblock . (#\╻ #\▄ #\▄ #\ #\╻))
(dots . (#\⣿ #\⠶ #\⠶ #\ #\⣿))))
(defstruct progress-bar (total description length style percent)
constructor: :init!)
(defmethod {:init! progress-bar}
(lambda (self total (description #f) length: (length 80) style: (style 'line) percent: (percent #f))
(struct-instance-init! self total description length style percent)))
(def (progress pbar n)
(with ((progress-bar total description length style percent) pbar)
(let* ((n (min total (max 0 n)))
(n-done (inexact->exact (floor (* (- length 3) (/ n total)))))
(n-remaining (inexact->exact (ceiling (* (- length 3) (/ (- total n) total)))))
(progress-chars (assgetq style +progress-styles+))
(bar (format "~c~a~c~a~c"
(list-ref progress-chars 0)
(make-string n-done (list-ref progress-chars 1))
(list-ref progress-chars 2)
(make-string n-remaining (list-ref progress-chars 3))
(list-ref progress-chars 4)))
(progress (if percent
(format "~3,0F%" (* 100 (/ n total)))
(format "(~d/~d)" n total))))
(str (cursor-up 1)
(parse-markup description) bar " " progress "\n"))))