;;
;; Charms.Quill
;;
;; An example program in Quill using the :charms library.
;;
;; Prints random characters to the screen, optionally with
;; argument, where the characters of the argument are scattered
;; on screen.
;;
;; @burtons
;; AGPL
;; 2026

(define :variable letters "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789*+.-")

(define :function random-char ()
  (aref letters
        (random (length letters))))

(define :function main (arguments)
  (if (> (length arguments) 1)
    (setf letters (second arguments)))
  (charms:with-curses ()
    (charms:disable-echoing)
    (charms:enable-raw-input)
    (charms:enable-non-blocking-mode
     charms:*standard-window*)

    (loop
      (multiple-value-bind (width height)
          (charms:window-dimensions charms:*standard-window*)

        (charms:clear-window charms:*standard-window*)

        ;; Leave the final row and column alone.
        (dotimes (y height)
          (dotimes (x width)
            (charms:write-char-at-point
             charms:*standard-window*
             (random-char)
             x
             y)))

        (charms:refresh-window charms:*standard-window*))

      (let ((key (charms:get-char
                  charms:*standard-window*
                  :ignore-error t)))
        (when (member key '(#\q #\Q))
          (return)))

      (sleep (/ 1.0 30.0))
  )))