diff options
| author | ruki <[email protected]> | 2021-01-28 00:48:18 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-01-28 00:48:18 +0800 |
| commit | c5615fbc82554d5e85dbc02da493bb59df2cd492 (patch) | |
| tree | 2287eb98f6c355735c220143ae5657874f6d5db8 | |
| parent | d98169322f86c2a95d6b4e62a56918414fa1349d (diff) | |
add tty.has_color256
| -rw-r--r-- | xmake/core/base/colors.lua | 19 | ||||
| -rw-r--r-- | xmake/core/base/option.lua | 3 | ||||
| -rw-r--r-- | xmake/core/base/tty.lua | 43 | ||||
| -rw-r--r-- | xmake/modules/core/tools/clang_cl.lua | 2 | ||||
| -rw-r--r-- | xmake/modules/core/tools/gcc.lua | 2 |
5 files changed, 50 insertions, 19 deletions
diff --git a/xmake/core/base/colors.lua b/xmake/core/base/colors.lua index d1d14ee16..9eac9b68e 100644 --- a/xmake/core/base/colors.lua +++ b/xmake/core/base/colors.lua @@ -155,19 +155,6 @@ function colors._colorterm() return colorterm end --- support 256 colors? -function colors.color256() - - -- no color? - local colorterm = colors._colorterm() - if colorterm == "nocolor" then - return false - end - - -- has 256 colors? - return colorterm == "color256" or os.subhost() ~= "windows" -end - -- support 24bits true color -- -- There's no reliable way, and ncurses/terminfo's maintainer expressed he has no intent on introducing support. @@ -304,7 +291,7 @@ function colors.translate(str, opt) -- not supported? ignore it local nocolors = false - if not tty.has_color8() and not colors.color256() and not colors.truecolor() then + if not tty.has_color8() and not tty.has_color256() and not colors.truecolor() then nocolors = true end @@ -316,7 +303,7 @@ function colors.translate(str, opt) end -- get keys - local keys = colors.color256() and colors._keys256 or colors._keys8 + local keys = tty.has_color256() and colors._keys256 or colors._keys8 if colors.truecolor() then keys = colors._keys24 end @@ -363,7 +350,7 @@ function colors.translate(str, opt) else code = "38;2;" .. block end - elseif colors.color256() and block:find("#", 1, true) then + elseif tty.has_color256() and block:find("#", 1, true) then if block:startswith("on#") then code = block:gsub("on#", "48;5;") else diff --git a/xmake/core/base/option.lua b/xmake/core/base/option.lua index 126ebfb0a..5255a6a2b 100644 --- a/xmake/core/base/option.lua +++ b/xmake/core/base/option.lua @@ -24,6 +24,7 @@ local option = option or {} -- load modules local cli = require("base/cli") local table = require("base/table") +local tty = require("base/tty") local colors = require("base/colors") local text = require("base/text") @@ -499,7 +500,7 @@ function option.show_logo(logo, opt) -- make rainbow for logo opt = opt or {} - if colors.truecolor() or colors.color256() then + if colors.truecolor() or tty.has_color256() then local lines = {} local seed = opt.seed or 236 for _, line in ipairs(logo:split("\n")) do diff --git a/xmake/core/base/tty.lua b/xmake/core/base/tty.lua index 2698a907e..1ccaedae1 100644 --- a/xmake/core/base/tty.lua +++ b/xmake/core/base/tty.lua @@ -263,5 +263,48 @@ function tty.has_color8() return has_color8 end +-- has 256 colors? +function tty.has_color256() + + local has_color256 = tty._HAS_COLOR256 + if has_color256 == nil then + + -- detect it from $COLORTERM + if has_color256 == nil then + local colorterm = tty._colorterm() + if colorterm == "nocolor" then + has_color256 = false + elseif colorterm == "color256" or colorterm == "truecolor" then + has_color256 = 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("256color", 1, true) then + has_color256 = true + end + end + + -- detect it from system + if has_color256 == nil then + if os.host() == "windows" then + has_color256 = false + elseif os.host() == "linux" or os.host("macosx") then + -- alway enabled for linux/macOS, $TERM maybe xterm, not xterm-256color, but it is supported + has_color256 = true + else + has_color256 = false + end + end + tty._HAS_COLOR256 = has_color256 or false + end + return has_color256 +end + -- return module return tty diff --git a/xmake/modules/core/tools/clang_cl.lua b/xmake/modules/core/tools/clang_cl.lua index 0a934576a..22a28f20c 100644 --- a/xmake/modules/core/tools/clang_cl.lua +++ b/xmake/modules/core/tools/clang_cl.lua @@ -66,7 +66,7 @@ end function _has_color_diagnostics(self) local colors_diagnostics = _g._HAS_COLOR_DIAGNOSTICS if colors_diagnostics == nil then - if io.isatty() and (tty.has_color8() or colors.color256()) then + if io.isatty() and (tty.has_color8() or tty.has_color256()) then local theme = colors.theme() if theme and theme:name() ~= "plain" then -- for clang diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index bbe3f0333..4be53c113 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -361,7 +361,7 @@ end function _has_color_diagnostics(self) local colors_diagnostics = _g._HAS_COLOR_DIAGNOSTICS if colors_diagnostics == nil then - if io.isatty() and (tty.has_color8() or colors.color256()) then + if io.isatty() and (tty.has_color8() or tty.has_color256()) then local theme = colors.theme() if theme and theme:name() ~= "plain" then -- for gcc |
