diff options
| author | ruki <[email protected]> | 2021-01-28 00:50:42 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-01-28 00:50:42 +0800 |
| commit | 86dfac55cc4a5651bf07888875f504fa1e2de477 (patch) | |
| tree | d85a2bc99b141254999572f21c2c7d752e36e537 | |
| parent | c5615fbc82554d5e85dbc02da493bb59df2cd492 (diff) | |
add tty.has_color24
| -rw-r--r-- | xmake/core/base/colors.lua | 41 | ||||
| -rw-r--r-- | xmake/core/base/option.lua | 4 | ||||
| -rw-r--r-- | xmake/core/base/tty.lua | 55 |
3 files changed, 57 insertions, 43 deletions
diff --git a/xmake/core/base/colors.lua b/xmake/core/base/colors.lua index 9eac9b68e..8cfb2893e 100644 --- a/xmake/core/base/colors.lua +++ b/xmake/core/base/colors.lua @@ -142,41 +142,6 @@ colors._keys24 = -- the escape string colors._ESC = '\x1b[%sm' --- get colorterm setting --- --- COLORTERM: color8, color256, truecolor, nocolor --- -function colors._colorterm() - local colorterm = colors._COLORTERM - if colorterm == nil then - colorterm = os.getenv("XMAKE_COLORTERM") or os.getenv("COLORTERM") or "" - colors._COLORTERM = colorterm - end - return colorterm -end - --- support 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 colors.truecolor() - - -- support true color? - local colorterm = colors._colorterm() - return colorterm:find("truecolor", 1, true) or colorterm:find("24bit", 1, true) -end - -- make rainbow truecolor code by the index of characters -- -- @param index the index of characters @@ -291,7 +256,7 @@ function colors.translate(str, opt) -- not supported? ignore it local nocolors = false - if not tty.has_color8() and not tty.has_color256() and not colors.truecolor() then + if not tty.has_color8() and not tty.has_color256() and not tty.has_color24() then nocolors = true end @@ -304,7 +269,7 @@ function colors.translate(str, opt) -- get keys local keys = tty.has_color256() and colors._keys256 or colors._keys8 - if colors.truecolor() then + if tty.has_color24() then keys = colors._keys24 end @@ -344,7 +309,7 @@ function colors.translate(str, opt) -- get the color code local code = keys[block] if not code then - if colors.truecolor() and block:find(";", 1, true) then + if tty.has_color24() and block:find(";", 1, true) then if block:startswith("on;") then code = block:gsub("on;", "48;2;") else diff --git a/xmake/core/base/option.lua b/xmake/core/base/option.lua index 5255a6a2b..977a39a40 100644 --- a/xmake/core/base/option.lua +++ b/xmake/core/base/option.lua @@ -500,14 +500,14 @@ function option.show_logo(logo, opt) -- make rainbow for logo opt = opt or {} - if colors.truecolor() or tty.has_color256() then + if tty.has_color24() or tty.has_color256() then local lines = {} local seed = opt.seed or 236 for _, line in ipairs(logo:split("\n")) do local i = 0 local line2 = "" line:gsub(".", function (c) - local code = colors.truecolor() and colors.rainbow24(i, seed) or colors.rainbow256(i, seed) + local code = tty.has_color24() and colors.rainbow24(i, seed) or colors.rainbow256(i, seed) line2 = string.format("%s${bright %s}%s", line2, code, c) i = i + 1 end) 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 |
