diff options
| author | ruki <[email protected]> | 2017-08-18 10:11:48 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-08-18 10:11:48 +0800 |
| commit | e9a58ed80873622d8de67ff8fad2b3bec868c7e5 (patch) | |
| tree | 051d6456059b89dbdafcef8a286f430b0e206132 | |
| parent | 628308885eb6b647256e6c56307579ee89607704 (diff) | |
support true colors
| -rw-r--r-- | CHANGELOG.md | 8 | ||||
| -rw-r--r-- | docs/manual.md | 12 | ||||
| -rw-r--r-- | docs/zh/manual.md | 12 | ||||
| -rw-r--r-- | xmake/core/base/colors.lua | 97 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/colors.lua | 26 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/task.lua | 6 |
6 files changed, 143 insertions, 18 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index cca25d9f4..aa3067dd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## master (unreleased) +### Changes + +* Support 24bits truecolors for `cprint()` + ## v2.1.6 ### Changes @@ -338,6 +342,10 @@ ## master (开发中) +### 改进 + +* 改进`cprint()`,支持24位真彩色输出 + ## v2.1.6 ### 改进 diff --git a/docs/manual.md b/docs/manual.md index 24cca0afe..b794a88b5 100644 --- a/docs/manual.md +++ b/docs/manual.md @@ -3978,6 +3978,18 @@ xmake会同时支持这两种写法,内部会去自动智能检测,选择输 所有的emoji表情,以及xmake里面对应的key,都可以通过[emoji符号](http://www.emoji-cheat-sheet.com/)里面找到。。 +2.1.7版本支持24位真彩色输出,如果终端支持的话: + +```lua +import("core.base.colors") +if colors.truecolor() then + cprint("${255;0;0}hello") + cprint("${on;255;0;0}hello${clear} xmake") + cprint("${bright 255;0;0 underline}hello") + cprint("${bright on;255;0;0 0;255;0}hello${clear} xmake") +end +``` + ##### cprintf ###### 无换行彩色打印终端日志 diff --git a/docs/zh/manual.md b/docs/zh/manual.md index 11cb547cb..f20a94079 100644 --- a/docs/zh/manual.md +++ b/docs/zh/manual.md @@ -3995,6 +3995,18 @@ xmake会同时支持这两种写法,内部会去自动智能检测,选择输 所有的emoji表情,以及xmake里面对应的key,都可以通过[emoji符号](http://www.emoji-cheat-sheet.com/)里面找到。。 +2.1.7版本支持24位真彩色输出,如果终端支持的话: + +```lua +import("core.base.colors") +if colors.truecolor() then + cprint("${255;0;0}hello") + cprint("${on;255;0;0}hello${clear} xmake") + cprint("${bright 255;0;0 underline}hello") + cprint("${bright on;255;0;0 0;255;0}hello${clear} xmake") +end +``` + ##### cprintf ###### 无换行彩色打印终端日志 diff --git a/xmake/core/base/colors.lua b/xmake/core/base/colors.lua index c5046315d..a48dc6df7 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 color keys +-- the 256 color keys -- -- from https://github.com/hoelzro/ansicolors -- -colors.keys = +colors._keys256 = { -- attributes reset = 0 @@ -66,14 +66,52 @@ colors.keys = , onwhite = 47 } +-- the 24bits color keys +-- +-- from https://github.com/hoelzro/ansicolors +-- +colors._keys24 = +{ + -- attributes + reset = 0 +, clear = 0 +, default = 0 +, bright = 1 +, dim = 2 +, underline = 4 +, blink = 5 +, reverse = 7 +, hidden = 8 + + -- foreground +, black = "38;2;0;0;0" +, red = "38;2;255;0;0" +, green = "38;2;0;255;0" +, yellow = "38;2;255;255;0" +, blue = "38;2;0;0;255" +, magenta = "38;2;255;0;255" +, cyan = "38;2;0;255;255" +, white = "38;2;255;255;255" + + -- background +, onblack = "48;2;0;0;0" +, onred = "48;2;255;0;0" +, ongreen = "48;2;0;255;0" +, onyellow = "48;2;255;255;0" +, onblue = "48;2;0;0;255" +, onmagenta = "48;2;255;0;255" +, oncyan = "48;2;0;255;255" +, onwhite = "48;2;255;255;255" +} + -- the escape string -colors.escape = string.char(27) .. '[%sm' +colors._escape = string.char(27) .. '[%sm' --- is supported? -function colors.supported() +-- support 256 colors? +function colors.has256() -- this is supported if be not windows - if xmake._HOST ~= "windows" then + if os.host() ~= "windows" then return true end @@ -81,6 +119,15 @@ function colors.supported() return os.getenv("ANSICON") end +-- support true 24bits colors +function colors.truecolor() + + -- this is supported if be not windows + if os.host() ~= "windows" then +-- return true + end +end + -- translate colors from the string -- -- colors: @@ -92,6 +139,13 @@ end -- "${blink red}hello" -- "${reverse red}hello xmake" -- +-- true colors: +-- +-- "${255;0;0}hello" +-- "${on;255;0;0}hello${clear} xmake" +-- "${bright 255;0;0 underline}hello" +-- "${bright on;255;0;0 0;255;0}hello${clear} xmake" +-- -- emoji: -- -- "${beer}hello${beer}world" @@ -110,7 +164,7 @@ function colors.translate(str) str = string.gsub(str, "(%${(.-)})", function(_, word) -- not supported? ignore it - if not colors.supported() then + if not colors.has256() and not colors.truecolor() then return "" end @@ -120,20 +174,33 @@ function colors.translate(str) return emoji_str end + -- get keys + local keys = colors._keys256 + if colors.truecolor() then + keys = colors._keys24 + end + -- make color buffer local buffer = {} - for key in word:gmatch("%w+") do - - -- key to number - local number = colors.keys[key] - assert(number, "unknown color: " .. key) + for _, key in ipairs(word:split("%s+")) do + + -- 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;") + else + code = "38;2;" .. key + end + end + assert(code, "unknown color: " .. key) - -- save this number - table.insert(buffer, number) + -- save this code + table.insert(buffer, code) end -- format the color buffer - return colors.escape:format(table.concat(buffer, ";")) + return colors._escape:format(table.concat(buffer, ";")) end) -- ok diff --git a/xmake/core/sandbox/modules/import/core/base/colors.lua b/xmake/core/sandbox/modules/import/core/base/colors.lua new file mode 100644 index 000000000..13d7e65cb --- /dev/null +++ b/xmake/core/sandbox/modules/import/core/base/colors.lua @@ -0,0 +1,26 @@ +--!The Make-like Build Utility based on Lua +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015 - 2017, TBOOX Open Source Group. +-- +-- @author ruki +-- @file colors.lua +-- + +-- return module +return require("base/colors") diff --git a/xmake/core/sandbox/modules/import/core/base/task.lua b/xmake/core/sandbox/modules/import/core/base/task.lua index f8008b3a4..f3f7dbcb8 100644 --- a/xmake/core/sandbox/modules/import/core/base/task.lua +++ b/xmake/core/sandbox/modules/import/core/base/task.lua @@ -23,7 +23,7 @@ -- -- define module -local sandbox_core_project_task = sandbox_core_project_task or {} +local sandbox_core_base_task = sandbox_core_base_task or {} -- load modules local os = require("base/os") @@ -35,7 +35,7 @@ local task = require("base/task") local raise = require("sandbox/modules/raise") -- run the given task -function sandbox_core_project_task.run(taskname, options, ...) +function sandbox_core_base_task.run(taskname, options, ...) -- init options options = table.wrap(options) @@ -66,4 +66,4 @@ function sandbox_core_project_task.run(taskname, options, ...) end -- return module -return sandbox_core_project_task +return sandbox_core_base_task |
