;;; -*- lexical-binding: t -*- ;; ;; -------------------------------------------------------------------------- (require 'cl-lib) (cl-pushnew "." load-path :test #'string=) (require 'luki-lisp) ;; -------------------------------------------------------------------------- (-> 'bad-blend) (-> 'bad-color) (-> 'bad-elem) (-> 'bad-size) ;; -------------------------------------------------------------------------- (defclass ascii (elem) ((name :initform "ascii") (char-col :initarg :char-col :type list :initform nil) (str-data :initarg :str-data :type list :initform nil))) ;; -------------------------------------------------------------------------- (cl-defmethod bad-ascii-to-drawable ((a ascii)) (~ (data char-col str-data w) a (when (& data char-col) (cl-loop with str-lst = (seq--into-string data) with res-str = str-lst for (cr cl) in char-col do ; e.g. ("#" "red") means all hashes be red (cl-loop with len = (--- cr) with pos = 0 with on = t while on do (let ((res (string-search cr str-lst pos))) (if (! res) (setq on nil) (set-text-properties res (+ res len) `(face (:foreground ,cl)) res-str) (setq pos (1+ res))))) finally do (setf str-data (seq-split res-str w)))))) (cl-defmethod bad-draw-ascii ((a ascii)) (~ (str-data) a (unless str-data (bad-ascii-to-drawable a)) (when str-data (insert (string-join str-data "\n")) (goto-beg)))) ;; -------------------------------------------------------------------------- (cl-defmethod bad-read-data ((a ascii) (strs list)) (~ (w-max h-max w h) a (bad-set-data a (string-to-list (string-join strs))) (let ((fst (--- (1st strs))) (all (--- strs))) (setf w (if w-max (min w-max fst) fst)) (setf h (if h-max (min h-max all) all))))) ;; -------------------------------------------------------------------------- (cl-defmethod bad-read-file ((a ascii) &optional f dir) (~ (name) a (or dir (setq dir "data")) (setq f (file-name-concat dir (or f (concat name ".txt"))))) (with-temp-buffer (insert-file-contents f) (let* ((lines (split-string (buffer-substring-no-properties (point-min) (point-max)) "\n")) (len (apply #'max (mapcar #'length lines)))) (bad-read-data a (mapcar (L (e) (string-pad e len)) lines))))) (cl-defmethod bad-read-string ((a ascii) (str string)) (let* ((lines (split-string str "}\\|\n")) (chrs (apply #'max (mapcar #'length lines))) (padded (mapcar (L (e) (string-pad e chrs)) lines))) (bad-read-data a padded))) ;; -------------------------------------------------------------------------- (defun bad-draw-uxu (&optional buf erase) (i) (when-let* ((bf (or buf (get-buffer-create "*ansi-buffer*")))) (with-current-buffer bf (when erase (erase-buffer)) (let ((uxu (ascii))) (bad-read-file uxu "ascii/uxu") (~ (char-col) uxu (setf char-col '(("#" "#8787af") ("underground experts united" "#00d7d7") ("x" "#000000"))) (bad-draw-ascii uxu))) (pop-to-buffer bf)))) ;; (bad-draw-uxu) ;; -------------------------------------------------------------------------- (defun bad-cat () (cl-loop with cat = (ascii) initially do (bad-read-file cat "snake/cat.txt") for c in '( 10 32 34 37 39 40 41 44 ; [1] 45 46 47 58 59 61 64 79 84 92 94 95 96 97 99 100 101 102 103 105 108 110 111 112 114 115 116 117 126 ) for col = (bad-blend (bad-fg-random) (bad-bg-random)) collect `(,(char-to-string c) ,col) into crs finally do (setf (@ cat char-col) crs) finally return cat)) ;; -------------------------------------------------------------------------- ;; [1] Get the data from the file like this. TODO: automate. ;; (insert "\n" ;; (@f "%s" ;; (cl-delete-duplicates ;; (sort (string-to-list ;; (buffer-substring-no-properties (point-min) (pos-bol))))))) ;; -------------------------------------------------------------------------- (defun list-drop-center (lst) (let* ((len (--- lst)) (half (/ len 2))) `(,@(take half lst) ,@(last lst (if (cl-evenp len) (1- half) half))))) ;; (list-drop-center '()) ; () ;; (list-drop-center '(1)) ; (_) ;; (list-drop-center '(1 2)) ; (1 _) ;; (list-drop-center '(1 2 3)) ; (1 _ 3) ;; (list-drop-center '(1 2 3 4)) ; (1 2 _ 4) ;; (list-drop-center '(1 2 3 4 5)) ; (1 2 _ 4 5) ;; (list-drop-center '(1 2 3 4 5 6)) ; (1 2 3 _ 5 6) ;; (list-drop-center '(1 2 3 4 5 6 7)) ; (1 2 3 _ 5 6 7) ;; -------------------------------------------------------------------------- (<- 'bad-ascii)