Skip to content

Instantly share code, notes, and snippets.

@jdz
Last active November 21, 2019 10:02
Show Gist options
  • Save jdz/e42f035569dcee07d25fa83df28472f1 to your computer and use it in GitHub Desktop.
Save jdz/e42f035569dcee07d25fa83df28472f1 to your computer and use it in GitHub Desktop.
Turns out there was a bug...
;; Alternative graphics: "⣀⣄⣤⣦⣶⣷⣿" or "⠆⡇⡷⣿"
(defun bar (stream width n max &optional (graphics "▏▎▍▌▋▊▉█"))
"Draw an N/MAX bar no wider than WIDTH using charecters from GRAPHICS."
(declare (type number width n max)
(type string graphics))
(assert (<= 1 (length graphics))
(graphics)
"Invalid graphics provided: ~S" graphics)
(let* ((nchars (length graphics))
(inc (/ 1 nchars 2)))
(multiple-value-bind (full part)
(truncate (* width (/ n max)))
(loop with char = (char graphics (1- nchars))
repeat full
do (write-char char stream))
(multiple-value-bind (index rem)
(truncate (* nchars (- part inc)))
(when (<= 0 rem)
(write-char (char graphics index) stream)))))
nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment