pest.lisp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. ;; PROMPT_COMMAND='export PS1="$(pest)"'
  2. (defun config-parse (&optional path)
  3. (let ((config-path (if path path (concatenate 'string (uiop:getenv "HOME") "/.config/pest/config.toml"))))
  4. (if (probe-file config-path)
  5. (with-open-file (fh config-path :direction :input)
  6. (let ((file-content (with-output-to-string (out)
  7. (loop for line = (read-line fh nil)
  8. while line
  9. do (format out "~a~%" line)))))
  10. (clop:parse file-content))))))
  11. (defvar *config* NIL)
  12. ;; Colors
  13. (defvar *fg* NIL)
  14. (defvar *bg* NIL)
  15. (defvar *style* NIL)
  16. ;; Battery
  17. (defvar *display-battery* NIL)
  18. ;; Git
  19. (defvar *display-git* NIL)
  20. (defvar *git-string* NIL)
  21. (defun display-git-info ()
  22. (if (probe-file (pathname (concatenate 'string (uiop:getenv "PWD") "/.git")))
  23. ;; (legit:git-rev-parse "HEAD" :short T)))
  24. (concatenate 'string (legit:current-branch ".") "|" (legit:current-commit "." :short T))))
  25. (defun reload-config ()
  26. (setf *config* (config-parse "~/Repos/cl-pest/config.toml"))
  27. (setf *fg* (if *config*
  28. (destructuring-bind (r g b)
  29. (subseq (assoc "fg" (cdr (assoc "colors" *config* :test #'equal)) :test #'equal) (- 4 3))
  30. (chlorophyll:create-rgb-color r g b))
  31. (chlorophyll:create-rgb-color 255 255 255)))
  32. (setf *bg* (if *config*
  33. (destructuring-bind (r g b)
  34. (subseq (assoc "bg" (cdr (assoc "colors" *config* :test #'equal)) :test #'equal) (- 4 3))
  35. (chlorophyll:create-rgb-color r g b))
  36. (chlorophyll:create-rgb-color 0 0 0)))
  37. (setf *style* (chlorophyll:new-style
  38. :bold NIL
  39. :foreground *fg*
  40. :background *bg*))
  41. (setf *git-string* (if (cdr (assoc "display_head" (cdr (assoc "git" *config* :test #'equal)) :test #'equal))
  42. (setf *git-string* (display-git-info)))))
  43. ;; Regex Scanners
  44. ;; TODO $HOME rendered as /home/user as opposed to ~
  45. (defvar *home-scan* (ppcre:create-scanner (concatenate 'string "^" (format NIL "~a" (user-homedir-pathname)))))
  46. (defun pwd ()
  47. (ppcre:regex-replace *home-scan* (uiop:getenv "PWD") "~/"))
  48. (defun main ()
  49. (reload-config)
  50. (format T "~a ~a λ " (chlorophyll:stylize *style* (pwd)) *git-string*))