guile-termenv/termenv/unix.scm

30 lines
1.2 KiB
Scheme

(define-module (termenv unix)
#:export (color-profile))
(define (color-profile port)
"Get the best color profile supported by the given output port"
(cond
((not (isatty? port)) 'ascii)
((equal? "true" (getenv "GOOGLE_CLOUD_SHELL")) 'true-color)
(else (let ((term (or (getenv "TERM") ""))
(color-term (or (getenv "COLORTERM") "")))
(cond
((or (string-ci=? color-term "24bit")
(string-ci=? color-term "truecolor"))
;; tmux supports TrueColor, screen only ANSI256
(if (and (string-prefix-ci? "screen" term)
(not (string-ci=? "tmux" (or (getenv "TERM_PROGRAM") ""))))
'ansi256
'true-color))
((or (string-ci=? color-term "yes")
(string-ci=? color-term "true")) 'ansi256)
((or (string-ci=? term "xterm-kitty")
(string-ci=? term "wezterm")
(string-ci=? term "xterm-ghostty"))
'true-color)
((string-ci=? term "linux") 'ansi)
((string-contains-ci term "256color") 'ansi256)
((string-contains-ci term "color") 'ansi)
((string-contains-ci term "ansi") 'ansi)
(else 'ascii))))))