diff options
| author | ruki <[email protected]> | 2021-01-28 00:46:05 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-01-28 00:46:05 +0800 |
| commit | d98169322f86c2a95d6b4e62a56918414fa1349d (patch) | |
| tree | 5ea4db29ed63dd0640bc8274ce35f977e3c8e43a | |
| parent | a1728044cfcdd69080fc34c7e51d1cc4917e25a1 (diff) | |
add tty.has_color8
| -rw-r--r-- | xmake/core/base/colors.lua | 21 | ||||
| -rw-r--r-- | xmake/core/base/tty.lua | 57 | ||||
| -rw-r--r-- | xmake/modules/core/tools/clang_cl.lua | 3 | ||||
| -rw-r--r-- | xmake/modules/core/tools/gcc.lua | 3 |
4 files changed, 62 insertions, 22 deletions
diff --git a/xmake/core/base/colors.lua b/xmake/core/base/colors.lua index f327db10d..d1d14ee16 100644 --- a/xmake/core/base/colors.lua +++ b/xmake/core/base/colors.lua @@ -155,25 +155,6 @@ function colors._colorterm() return colorterm end --- support 8 colors? -function colors.color8() - - -- no color? - local colorterm = colors._colorterm() - if colorterm == "nocolor" then - return false - end - - -- has 8 colors? - if colorterm == "color8" or os.subhost() ~= "windows" then - return true - end - - -- this is supported if exists ANSICON envirnoment variable on windows - colors._ANSICON = colors._ANSICON or os.getenv("ANSICON") or "" - return colors._ANSICON ~= "" -end - -- support 256 colors? function colors.color256() @@ -323,7 +304,7 @@ function colors.translate(str, opt) -- not supported? ignore it local nocolors = false - if not colors.color8() and not colors.color256() and not colors.truecolor() then + if not tty.has_color8() and not colors.color256() and not colors.truecolor() then nocolors = true end diff --git a/xmake/core/base/tty.lua b/xmake/core/base/tty.lua index 276a7ea29..2698a907e 100644 --- a/xmake/core/base/tty.lua +++ b/xmake/core/base/tty.lua @@ -38,6 +38,19 @@ function tty._iowrite(...) end end +-- get colorterm setting +-- +-- COLORTERM: color8, color256, truecolor, nocolor +-- +function tty._colorterm() + local colorterm = tty._COLORTERM + if colorterm == nil then + colorterm = os.getenv("XMAKE_COLORTERM") or os.getenv("COLORTERM") or "" + tty._COLORTERM = colorterm + end + return colorterm +end + -- erases from the current cursor position to the end of the current line. function tty.erase_line_to_end() tty._iowrite("\x1b[K") @@ -206,5 +219,49 @@ function tty.has_emoji() return has_emoji end +-- has 8 colors? +function tty.has_color8() + local has_color8 = tty._HAS_COLOR8 + if has_color8 == nil then + + -- detect it from $COLORTERM + if has_color8 == nil then + local colorterm = tty._colorterm() + if colorterm == "nocolor" then + has_color8 = false + elseif colorterm == "color8" or colorterm == "color256" or colorterm == "truecolor" then + has_color8 = true + end + end + + -- detect it from $TERM + local term = tty.term() + if has_color8 == nil then + if term == "vstudio" then + has_color8 = false + elseif term == "xterm" then + has_color8 = true + end + end + + -- detect it from system + if has_color8 == nil then + if os.host() == "windows" then + local winos = require("base/winos") + if winos.version():le("win7") then + has_color8 = false + else + has_color8 = true + end + else + -- alway enabled for unix-like system + has_color8 = true + end + end + tty._HAS_COLOR8 = has_color8 or false + end + return has_color8 +end + -- return module return tty diff --git a/xmake/modules/core/tools/clang_cl.lua b/xmake/modules/core/tools/clang_cl.lua index 26ad13184..0a934576a 100644 --- a/xmake/modules/core/tools/clang_cl.lua +++ b/xmake/modules/core/tools/clang_cl.lua @@ -21,6 +21,7 @@ -- inherit cl inherit("cl") import("core.base.option") +import("core.base.tty") import("core.base.colors") -- init it @@ -65,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 (colors.color8() or colors.color256()) then + if io.isatty() and (tty.has_color8() or colors.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 e945e2926..bbe3f0333 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -20,6 +20,7 @@ -- imports import("core.base.option") +import("core.base.tty") import("core.base.colors") import("core.base.global") import("core.project.config") @@ -360,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 (colors.color8() or colors.color256()) then + if io.isatty() and (tty.has_color8() or colors.color256()) then local theme = colors.theme() if theme and theme:name() ~= "plain" then -- for gcc |
