diff options
| author | OpportunityLiu <[email protected]> | 2019-06-19 12:56:50 +0800 |
|---|---|---|
| committer | OpportunityLiu <[email protected]> | 2019-06-19 12:56:50 +0800 |
| commit | 8dedbdc46bf2b5414d3d65a2056546b5f6cecd79 (patch) | |
| tree | 0cf2de32db0d65185e19b3c88e5783ca8e672003 | |
| parent | ec47d59df254531303c0885b1a4d8c5d35deb42c (diff) | |
fix
| -rw-r--r-- | xmake/core/base/colors.lua | 11 | ||||
| -rw-r--r-- | xmake/core/base/dump.lua | 75 | ||||
| -rw-r--r-- | xmake/core/base/table.lua | 26 |
3 files changed, 71 insertions, 41 deletions
diff --git a/xmake/core/base/colors.lua b/xmake/core/base/colors.lua index 33e56d512..e3b31a0bd 100644 --- a/xmake/core/base/colors.lua +++ b/xmake/core/base/colors.lua @@ -258,7 +258,8 @@ end -- translate colors from the string -- -- @param str the string with colors --- @param patch_reset wrap input string with "${reset}"? +-- @param opt options +-- patch_reset: wrap str with `"${reset}"`? -- -- 8 colors: -- @@ -295,23 +296,25 @@ end -- "${hello xmake}" -- "${hello xmake $beer}" -- -function colors.translate(str, patch_reset) +function colors.translate(str, opt) -- check string if not str then return nil end + opt = opt or {} + -- get theme local theme = colors.theme() -- patch reset - if patch_reset ~= false then + if opt.patch_reset ~= false then str = "${reset}" .. str .. "${reset}" end -- translate color blocks, e.g. ${red}, ${color.xxx}, ${emoji} - str = str:gsub("(%${(.-)})", function(_, word) + str = str:gsub("(%${(.-)})", function(_, word) -- not supported? ignore it local nocolors = false diff --git a/xmake/core/base/dump.lua b/xmake/core/base/dump.lua index 303888ff0..43a6f95f5 100644 --- a/xmake/core/base/dump.lua +++ b/xmake/core/base/dump.lua @@ -1,32 +1,55 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed 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 - 2019, TBOOX Open Source Group. +-- +-- @author ruki +-- @file utils.lua +-- + +-- define module +local dump = dump or {} + +-- load modules local colors = require("base/colors") local table = require("base/table") -local _dump = {} -function _dump._string(str, as_key) +function dump._string(str, as_key) local quote = (not as_key) or (not str:match("^[a-zA-Z_][a-zA-Z0-9_]*$")) if quote then io.write(colors.translate("${cyan}\"")) end - io.write(colors.translate("${reset}${cyan bright}", false)) + io.write(colors.translate("${reset}${cyan bright}", { patch_reset = false })) io.write(str) - io.write(colors.translate("${reset}", false)) + io.write(colors.translate("${reset}", { patch_reset = false })) if quote then io.write(colors.translate("${cyan}\"")) end end -function _dump._keyword(keyword) +function dump._keyword(keyword) io.write(colors.translate("${blue}" .. tostring(keyword))) end -function _dump._number(num) +function dump._number(num) io.write(colors.translate("${green}" .. num)) end -function _dump._function(func, as_key) +function dump._function(func, as_key) if as_key then - return _dump._default(func) + return dump._default(func) end local funcinfo = debug.getinfo(func) local srcinfo = funcinfo.short_src @@ -36,27 +59,29 @@ function _dump._function(func, as_key) io.write(colors.translate("${red}function ${bright}" .. (funcinfo.name or "") .. "${reset}${dim}" .. srcinfo)) end -function _dump._default(value) - io.write(colors.translate("${dim}<", false) .. tostring(value) .. colors.translate(">${reset}", false)) +function dump._default(value) + io.write(colors.translate("${dim}<", { patch_reset = false })) + io.write(tostring(value)) + io.write(colors.translate(">${reset}", { patch_reset = false })) end -function _dump._scalar(value, as_key) +function dump._scalar(value, as_key) if type(value) == "nil" then - _dump._keyword("nil") + dump._keyword("nil") elseif type(value) == "boolean" then - _dump._keyword(value) + dump._keyword(value) elseif type(value) == "number" then - _dump._number(value) + dump._number(value) elseif type(value) == "string" then - _dump._string(value, as_key) + dump._string(value, as_key) elseif type(value) == "function" then - _dump._function(value, as_key) + dump._function(value, as_key) else - _dump._default(value) + dump._default(value) end end -function _dump._table(value, first_indent, remain_indent) +function dump._table(value, first_indent, remain_indent) io.write(first_indent .. colors.translate("${dim}{")) local inner_indent = remain_indent .. " " local is_arr = table.is_array(value) @@ -70,13 +95,13 @@ function _dump._table(value, first_indent, remain_indent) end io.write(inner_indent) if not is_arr then - _dump._scalar(k, true) + dump._scalar(k, true) io.write(colors.translate("${dim} = ")) end if type(v) == "table" then - _dump._table(v, "", inner_indent) + dump._table(v, "", inner_indent) else - _dump._scalar(v) + dump._scalar(v) end end if first_value then @@ -86,15 +111,15 @@ function _dump._table(value, first_indent, remain_indent) end end -function _dump.main(value, indent) +function dump._print(value, indent) indent = indent or "" if type(value) == "table" then - _dump._table(value, indent, indent:gsub(".", " ")) + dump._table(value, indent, indent:gsub(".", " ")) else io.write(indent) - _dump._scalar(value) + dump._scalar(value) end end -return _dump.main
\ No newline at end of file +return dump._print
\ No newline at end of file diff --git a/xmake/core/base/table.lua b/xmake/core/base/table.lua index 6e7311f68..fab299da1 100644 --- a/xmake/core/base/table.lua +++ b/xmake/core/base/table.lua @@ -24,7 +24,7 @@ local table = table or {} -- clear the table function table.clear(self) for k in next, self do - rawset(self, k, nil) + rawset(self, k, nil) end end @@ -176,23 +176,25 @@ function table.slice(self, first, last, step) return sliced end -local function is_array(table) - local i = 0 - for _ in pairs(table) do - i = i + 1 - if table[i] == nil then return false end - end - return true -end - -- is array? function table.is_array(array) - return type(array) == "table" and is_array(array) + if type(array) == "table" then + local i = 0 + for _ in pairs(array) do + i = i + 1 + if array[i] == nil then + return false + end + end + return true + else + return false + end end -- is dictionary? function table.is_dictionary(dict) - return type(dict) == "table" and not is_array(dict) + return type(dict) == "table" and not table.is_array(dict) end -- unwrap object if be only one |
