Skip to content

Instantly share code, notes, and snippets.

@dgulino
Last active October 18, 2025 22:00
Show Gist options
  • Select an option

  • Save dgulino/4a80dd47a26e094b1a41b446257415bd to your computer and use it in GitHub Desktop.

Select an option

Save dgulino/4a80dd47a26e094b1a41b446257415bd to your computer and use it in GitHub Desktop.
Common Lisp command line number grapher
#!/usr/bin/env sbcl --script
;#!/usr/bin/env sbcl --noinform --load /Users/drewgulino/quicklisp/setup.lisp
;developed on SBCL 2.5.9
;usage
;./random_number.lisp |./ngraph.lisp
;246 ██████████████████████████████▊
;217 ███████████████████████████▏
;994 ██████████████████████████████████████████████████████████████▏
;100 ██████▎
(require :uiop)
(defparameter *max-num* 1)
(defparameter *screen-width* 80)
(defparameter *screen-height* 0)
;(defun screen-dimensions ()
; (croatoan:with-screen (s)
; (let ((height (croatoan:screen-height s))
; (width (croatoan:screen-width s)))
; (format t "The terminal screen is ~D rows high and ~D columns wide." height width)))
; )
(defun clear-screen ()
(format t "~C[2J" #\Esc)
)
(defun italics ()
(format t "~C[3m" #\Esc)
)
(defun white ()
(format t "~C[38;5;15m" #\Esc)
)
(defun red ()
(format t "~C[38;5;9m" #\Esc)
)
(defun green ()
(format t "~C[38;5;10m" #\Esc)
)
(defun normal ()
(format t "~C[0" #\Esc)
)
(defun screen-size ()
(uiop:run-program '("stty" "size")
:output '(:string :stripped t)
:error-output '(:string :stripped t)
;:output (lambda (stream)
; (cons (read stream) (read stream)))
:input :interactive
)
)
;(defun run-prog ()
;(let* ((p (sb-ext:run-program "/bin/stty" '()
; :input :stream
; :output *standard-output*
; :wait nil))
; (s (sb-ext:process-input p)))
; (format s "foo bar baz~%")
; (finish-output s)
; (sb-ext:process-wait p)
; (sb-ext:process-close p))
;)
(defun pixel-width ()
(* 8 *screen-width*))
(defun scale-num (num)
(round num (ceiling *max-num* (pixel-width))
))
(defun num-to-block (num)
(let((code (- 9616 num))
)
(format t "~a" (code-char code))
)
)
(defun print-graph (i)
(let (
(num (scale-num i)))
(multiple-value-bind (blocks remainder) (floor num 8)
(format t "~3A " i)
;(format t "~A ~A ~A ~A " num *max-num* blocks remainder)
(loop while (> blocks 0)
do
(decf blocks)
(format t "~A" #\FULL_BLOCK)
;(format t "~A" #\U+2588)
)
(if (> remainder 0)
(num-to-block remainder)
)
(format t "~%")
)
)
)
(defun main ()
(loop for line = (read-line *standard-input* nil :eof)
until (eq line :eof)
do (
let* (
(i (parse-integer line))
)
(if (> i *max-num*)
(progn
(setf *max-num* i)
(red)
(print-graph i)
)
(progn
(white)
(print-graph i)
)
)
)
)
(normal)
)
(progn
;(format t "~a" (screen-size))
;(force-output)
;(screen-dimensions)
(main)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment