summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2018-04-20 00:45:43 +0800
committerruki <[email protected]>2018-04-19 22:34:19 +0800
commit23644d9e3acaea9fd773a03f23dd9c2d0b937797 (patch)
treec9c2f4726b1979d69eb89ef95b10fcebb9d8140b
parent5223aac166782be25955bd6f662d02c21d44519e (diff)
add color256
-rw-r--r--CHANGELOG.md2
-rw-r--r--xmake/core/base/colors.lua127
-rw-r--r--xmake/core/main.lua4
3 files changed, 115 insertions, 18 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 696f8cf78..8e1b94a7b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@
* Add `set_tools` and `add_tools` apis to change the toolchains for special target
* Add builtin rules: `mode:debug`, `mode:release`, `mode:profile` and `mode:check`
* Add `is_mode`, `is_arch` and `is_plat` builtin apis in the custom scripts
+* Add color256 codes
### Changes
@@ -452,6 +453,7 @@
* 添加`set_tools`和`add_tools`接口为指定target目标设置编译工具链
* 添加内建规则:`mode:debug`, `mode:release`, `mode:profile`和`mode:check`
* 添加`is_mode`, `is_arch` 和`is_plat`内置接口到自定义脚本域
+* 添加color256代码
### 改进
diff --git a/xmake/core/base/colors.lua b/xmake/core/base/colors.lua
index 06ddef3dc..330ed5bc1 100644
--- a/xmake/core/base/colors.lua
+++ b/xmake/core/base/colors.lua
@@ -28,11 +28,11 @@ local colors = colors or {}
-- load modules
local emoji = emoji or require("base/emoji")
--- the 256 color keys
+-- the color8 keys
--
-- from https://github.com/hoelzro/ansicolors
--
-colors._keys256 =
+colors._keys8 =
{
-- attributes
reset = 0
@@ -66,6 +66,44 @@ colors._keys256 =
, onwhite = 47
}
+-- the color256 keys
+--
+-- from https://github.com/hoelzro/ansicolors
+--
+colors._keys256 =
+{
+ -- attributes
+ reset = 0
+, clear = 0
+, default = 0
+, bright = 1
+, dim = 2
+, underline = 4
+, blink = 5
+, reverse = 7
+, hidden = 8
+
+ -- foreground
+, black = "38;5;0"
+, red = "38;5;1"
+, green = "38;5;2"
+, yellow = "38;5;3"
+, blue = "38;5;4"
+, magenta = "38;5;5"
+, cyan = "38;5;6"
+, white = "38;5;7"
+
+ -- background
+, onblack = "48;5;0"
+, onred = "48;5;1"
+, ongreen = "48;5;2"
+, onyellow = "48;5;3"
+, onblue = "48;5;4"
+, onmagenta = "48;5;5"
+, oncyan = "48;5;6"
+, onwhite = "48;5;7"
+}
+
-- the 24bits color keys
--
-- from https://github.com/hoelzro/ansicolors
@@ -107,11 +145,11 @@ colors._keys24 =
-- the escape string
colors._escape = string.char(27) .. '[%sm'
--- support 256 colors?
+-- support 8 colors?
--
--- COLORTERM: color256, truecolor, nocolor
+-- COLORTERM: color8, color256, truecolor, nocolor
--
-function colors.has256()
+function colors.color8()
-- get $COLORTERM
colors._COLORTERM = colors._COLORTERM or os.getenv("COLORTERM") or ""
@@ -121,8 +159,8 @@ function colors.has256()
return false
end
- -- has 256 color?
- if colors._COLORTERM == "color256" or os.host() ~= "windows" then
+ -- has 8 colors?
+ if colors._COLORTERM == "color8" or os.host() ~= "windows" then
return true
end
@@ -131,6 +169,24 @@ function colors.has256()
return colors._ANSICON
end
+-- support 256 colors?
+--
+-- COLORTERM: color8, color256, truecolor, nocolor
+--
+function colors.color256()
+
+ -- get $COLORTERM
+ colors._COLORTERM = colors._COLORTERM or os.getenv("COLORTERM") or ""
+
+ -- no color?
+ if colors._COLORTERM == "nocolor" then
+ return false
+ end
+
+ -- has 256 colors?
+ return colors._COLORTERM == "color256" or os.host() ~= "windows"
+end
+
-- support 24bits true color
--
-- There's no reliable way, and ncurses/terminfo's maintainer expressed he has no intent on introducing support.
@@ -155,7 +211,7 @@ function colors.truecolor()
return colors._COLORTERM:find("truecolor", 1, true) or colors._COLORTERM:find("24bit", 1, true)
end
--- make rainbow color code by the index of characters
+-- make rainbow truecolor code by the index of characters
--
-- @param index the index of characters
-- @param seed the seed, 0-255, default: random
@@ -163,7 +219,7 @@ end
-- @param spread the spread, default: 3.0
--
--
-function colors.rainbow(index, seed, freq, spread)
+function colors.rainbow24(index, seed, freq, spread)
-- init values
seed = seed
@@ -180,12 +236,35 @@ function colors.rainbow(index, seed, freq, spread)
return string.format("%d;%d;%d", red, green, blue)
end
+-- make rainbow color256 code by the index of characters (16-256)
+--
+-- @param index the index of characters
+-- @param seed the seed, 0-255, default: random
+-- @param freq the frequency, default: 0.1
+-- @param spread the spread, default: 3.0
+--
+--
+function colors.rainbow256(index, seed, freq, spread)
+
+ -- init values
+ seed = seed
+ freq = freq or 0.1
+ spread = spread or 3.0
+ index = seed + index / spread
+
+ -- make color code
+ local code = (freq * index) % 240 + 18
+
+ -- make code
+ return string.format("#%d", code)
+end
+
-- translate colors from the string
--
-- @param str the string with colors
-- @param force force to translate all colors?
--
--- colors:
+-- 8 colors:
--
-- "${red}hello"
-- "${onred}hello${clear} xmake"
@@ -194,6 +273,13 @@ end
-- "${blink red}hello"
-- "${reverse red}hello xmake"
--
+-- 256 colors:
+--
+-- "${#255}hello"
+-- "${on#255}hello${clear} xmake"
+-- "${bright #255; underline}hello"
+-- "${bright on#255 #10}hello${clear} xmake"
+--
-- true colors:
--
-- "${255;0;0}hello"
@@ -219,7 +305,7 @@ function colors.translate(str)
str = string.gsub(str, "(%${(.-)})", function(_, word)
-- not supported? ignore it
- if not colors.has256() and not colors.truecolor() then
+ if not colors.color8() and not colors.color256() and not colors.truecolor() then
return ""
end
@@ -230,7 +316,7 @@ function colors.translate(str)
end
-- get keys
- local keys = colors._keys256
+ local keys = colors.color256() and colors._keys256 or colors._keys8
if colors.truecolor() then
keys = colors._keys24
end
@@ -241,11 +327,20 @@ function colors.translate(str)
-- get the color code
local code = keys[key]
- if not code and key:find(";", 1, true) and colors.truecolor() then
- if key:startswith("on;") then
- code = key:gsub("on;", "48;2;")
+ if not code then
+ if colors.truecolor() and key:find(";", 1, true) then
+ if key:startswith("on;") then
+ code = key:gsub("on;", "48;2;")
+ else
+ code = "38;2;" .. key
+ end
+ elseif colors.color256() and key:find("#", 1, true) then
+ if key:startswith("on#") then
+ code = key:gsub("on#", "48;5;")
+ else
+ code = key:gsub("#", "38;5;")
+ end
else
- code = "38;2;" .. key
end
end
assert(code, "unknown color: " .. key)
diff --git a/xmake/core/main.lua b/xmake/core/main.lua
index 9eec272ca..bfaed0e5d 100644
--- a/xmake/core/main.lua
+++ b/xmake/core/main.lua
@@ -70,14 +70,14 @@ function main._show_logo()
]]
-- make rainbow for logo
- if colors.truecolor() then
+ if colors.truecolor() or colors.color256() then
local lines = {}
local seed = 236
for _, line in ipairs(logo:split("\n")) do
local i = 0
local line2 = ""
line:gsub(".", function (c)
- local code = colors.rainbow(i, seed)
+ local code = colors.truecolor() and colors.rainbow24(i, seed) or colors.rainbow256(i, seed)
line2 = string.format("%s${%s}%s", line2, code, c)
i = i + 1
end)