term.lisp 1.1 KB

123456789101112131415161718192021222324252627
  1. (import lua/os os)
  2. (import lua/package package)
  3. (defun coloured-ansi (col msg)
  4. "Colour a string MSG coloured with COL, using ANSI escape codes"
  5. :hidden
  6. (.. "\27[" col "m" msg "\27[0m"))
  7. (define coloured?
  8. "Constant defining whether the current terminal has colour support"
  9. (with (term-ty (string/lower (or (and os/getenv (os/getenv "TERM")) "")))
  10. (cond
  11. ;; If the terminal is dumb, then emit plain text.
  12. [(= term-ty "dumb") false]
  13. ;; If we're running under xterm then we're OK. This covers Git Bash for Windows.
  14. [(string/find term-ty "xterm") true]
  15. ;; If '/' is the path separator then we're probably on a UNIX system.
  16. [(and package/config (= (string/char-at package/config 1) "/")) true]
  17. ;; If we have ANSICON defined then we're in a windows prompt which supports ANSI.
  18. [(and os/getenv (/= (os/getenv "ANSICON") nil)) true]
  19. ;; Stick to plain text: better safe than sorry.
  20. [else false])))
  21. (define coloured
  22. "Colour a string MSG using COL if supported under the current terminal"
  23. (if coloured? coloured-ansi (lambda (_ msg) msg)))