summaryrefslogtreecommitdiff
path: root/xmake/core/base/tty.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2021-01-28 00:50:42 +0800
committerruki <[email protected]>2021-01-28 00:50:42 +0800
commit86dfac55cc4a5651bf07888875f504fa1e2de477 (patch)
treed85a2bc99b141254999572f21c2c7d752e36e537 /xmake/core/base/tty.lua
parentc5615fbc82554d5e85dbc02da493bb59df2cd492 (diff)
add tty.has_color24
Diffstat (limited to 'xmake/core/base/tty.lua')
-rw-r--r--xmake/core/base/tty.lua55
1 files changed, 52 insertions, 3 deletions
diff --git a/xmake/core/base/tty.lua b/xmake/core/base/tty.lua
index 1ccaedae1..fd7de2175 100644
--- a/xmake/core/base/tty.lua
+++ b/xmake/core/base/tty.lua
@@ -40,7 +40,7 @@ end
-- get colorterm setting
--
--- COLORTERM: color8, color256, truecolor, nocolor
+-- COLORTERM: 8color, 256color, truecolor, nocolor
--
function tty._colorterm()
local colorterm = tty._COLORTERM
@@ -229,7 +229,9 @@ function tty.has_color8()
local colorterm = tty._colorterm()
if colorterm == "nocolor" then
has_color8 = false
- elseif colorterm == "color8" or colorterm == "color256" or colorterm == "truecolor" then
+ elseif colorterm and colorterm:find("8color", 1, true) then
+ has_color8 = true
+ elseif tty.has_color256() or tty.has_color24() then
has_color8 = true
end
end
@@ -274,7 +276,9 @@ function tty.has_color256()
local colorterm = tty._colorterm()
if colorterm == "nocolor" then
has_color256 = false
- elseif colorterm == "color256" or colorterm == "truecolor" then
+ elseif colorterm and colorterm:find("256color", 1, true) then
+ has_color256 = true
+ elseif tty.has_color24() then
has_color256 = true
end
end
@@ -306,5 +310,50 @@ function tty.has_color256()
return has_color256
end
+-- has 24bits true color?
+--
+-- There's no reliable way, and ncurses/terminfo's maintainer expressed he has no intent on introducing support.
+-- S-Lang author added a check for $COLORTERM containing either "truecolor" or "24bit" (case sensitive).
+-- In turn, VTE, Konsole and iTerm2 set this variable to "truecolor" (it's been there in VTE for a while,
+-- it's relatively new and maybe still git-only in Konsole and iTerm2).
+--
+-- This is obviously not a reliable method, and is not forwarded via sudo, ssh etc. However, whenever it errs,
+-- it errs on the safe side: does not advertise support whereas it's actually supported.
+-- App developers can freely choose to check for this same variable, or introduce their own method
+-- (e.g. an option in their config file), whichever matches better the overall design of the given app.
+-- Checking $COLORTERM is recommended though, since that would lead to a more unique desktop experience
+-- where the user has to set one variable only and it takes effect across all the apps, rather than something
+-- separately for each app.
+--
+function tty.has_color24()
+
+ local has_color24 = tty._HAS_COLOR24
+ if has_color24 == nil then
+
+ -- detect it from $COLORTERM
+ if has_color24 == nil then
+ local colorterm = tty._colorterm()
+ if colorterm == "nocolor" then
+ has_color24 = false
+ elseif colorterm and (colorterm:find("truecolor", 1, true) or colorterm:find("24bit", 1, true)) then
+ has_color24 = true
+ end
+ end
+
+ -- detect it from $TERM
+ local term = tty.term()
+ local term_env = os.getenv("TERM")
+ if has_color256 == nil then
+ if term == "vstudio" then
+ has_color256 = false
+ elseif term_env and (term_env:find("truecolor", 1, true) or term_env:find("24bit", 1, true)) then
+ has_color256 = true
+ end
+ end
+ tty._HAS_COLOR24 = has_color24 or false
+ end
+ return has_color24
+end
+
-- return module
return tty