summaryrefslogtreecommitdiff
path: root/xmake/core/base
diff options
context:
space:
mode:
authorOpportunityLiu <[email protected]>2019-06-19 11:30:06 +0800
committerOpportunityLiu <[email protected]>2019-06-19 11:30:06 +0800
commitd5ad005f6c8a79af48f4c73f0ec87775ab746d0d (patch)
treeeefdd667a032d9732481b61cff3b4d860853ca8f /xmake/core/base
parent5c06711d12932c02ab7b64a25135902fabf00675 (diff)
improve dump
Diffstat (limited to 'xmake/core/base')
-rw-r--r--xmake/core/base/colors.lua12
-rw-r--r--xmake/core/base/dump.lua100
-rw-r--r--xmake/core/base/string.lua4
-rw-r--r--xmake/core/base/table.lua21
-rw-r--r--xmake/core/base/utils.lua5
5 files changed, 123 insertions, 19 deletions
diff --git a/xmake/core/base/colors.lua b/xmake/core/base/colors.lua
index 2223c3f85..33e56d512 100644
--- a/xmake/core/base/colors.lua
+++ b/xmake/core/base/colors.lua
@@ -257,9 +257,9 @@ end
-- translate colors from the string
--
--- @param str the string with colors
--- @param force force to translate all colors?
---
+-- @param str the string with colors
+-- @param patch_reset wrap input string with "${reset}"?
+--
-- 8 colors:
--
-- "${red}hello"
@@ -295,7 +295,7 @@ end
-- "${hello xmake}"
-- "${hello xmake $beer}"
--
-function colors.translate(str)
+function colors.translate(str, patch_reset)
-- check string
if not str then
@@ -306,7 +306,9 @@ function colors.translate(str)
local theme = colors.theme()
-- patch reset
- str = "${reset}" .. str .. "${reset}"
+ if patch_reset ~= false then
+ str = "${reset}" .. str .. "${reset}"
+ end
-- translate color blocks, e.g. ${red}, ${color.xxx}, ${emoji}
str = str:gsub("(%${(.-)})", function(_, word)
diff --git a/xmake/core/base/dump.lua b/xmake/core/base/dump.lua
new file mode 100644
index 000000000..66e0208fc
--- /dev/null
+++ b/xmake/core/base/dump.lua
@@ -0,0 +1,100 @@
+local colors = require("base/colors")
+local table = require("base/table")
+
+local _dump = {}
+
+function _dump._string(str, as_key)
+ local quote = (not as_key) or str:match("%s")
+ if quote then
+ io.write(colors.translate("${cyan}\""))
+ end
+ io.write(colors.translate("${reset}${cyan bright}", false))
+ io.write(str)
+ io.write(colors.translate("${reset}", false))
+ if quote then
+ io.write(colors.translate("${cyan}\""))
+ end
+end
+
+function _dump._keyword(keyword)
+ io.write(colors.translate("${blue}" .. tostring(keyword)))
+end
+
+function _dump._number(num)
+ io.write(colors.translate("${green}" .. num))
+end
+
+function _dump._function(func, as_key)
+ if as_key then
+ return _dump._default(func)
+ end
+ local funcinfo = debug.getinfo(func)
+ local srcinfo = funcinfo.short_src
+ if funcinfo.linedefined >= 0 then
+ srcinfo = srcinfo .. ":" .. funcinfo.linedefined
+ end
+ 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))
+end
+
+function _dump._scalar(value, as_key)
+ if type(value) == "nil" then
+ _dump._keyword("nil")
+ elseif type(value) == "boolean" then
+ _dump._keyword(value)
+ elseif type(value) == "number" then
+ _dump._number(value)
+ elseif type(value) == "string" then
+ _dump._string(value, as_key)
+ elseif type(value) == "function" then
+ _dump._function(value, as_key)
+ else
+ _dump._default(value)
+ end
+end
+
+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)
+ local first_value = true
+ for k, v in pairs(value) do
+ if first_value then
+ io.write("\n")
+ first_value = false
+ else
+ io.write(",\n")
+ end
+ io.write(inner_indent)
+ if not is_arr then
+ _dump._scalar(k, true)
+ io.write(colors.translate("${dim} = "))
+ end
+ if type(v) == "table" then
+ _dump._table(v, "", inner_indent)
+ else
+ _dump._scalar(v)
+ end
+ end
+ if first_value then
+ io.write(colors.translate(" ${dim}}"))
+ else
+ io.write("\n" .. remain_indent .. colors.translate("${dim}}"))
+ end
+end
+
+function _dump.main(value, indent)
+ indent = indent or ""
+
+ if type(value) == "table" then
+ _dump._table(value, indent, indent:gsub(".", " "))
+ else
+ io.write(indent)
+ _dump._scalar(value)
+ end
+end
+
+return _dump.main \ No newline at end of file
diff --git a/xmake/core/base/string.lua b/xmake/core/base/string.lua
index 558344d7f..5242a0a1e 100644
--- a/xmake/core/base/string.lua
+++ b/xmake/core/base/string.lua
@@ -28,11 +28,11 @@ string._dump = string._dump or string.dump
function string._makestr(object, deflate, serialize, level)
if type(object) == "string" then
return serialize and string.format("%q", object) or object
- elseif type(object) == "boolean" or type(object) == "number" then
+ elseif type(object) == "boolean" or type(object) == "number" then
return tostring(object)
elseif not serialize and type(object) == "table" and (getmetatable(object) or {}).__tostring then
return tostring(object)
- elseif type(object) == "table" then
+ elseif type(object) == "table" then
-- make head
local s = ""
diff --git a/xmake/core/base/table.lua b/xmake/core/base/table.lua
index 2d41e79af..6e7311f68 100644
--- a/xmake/core/base/table.lua
+++ b/xmake/core/base/table.lua
@@ -176,22 +176,23 @@ 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 array[1] ~= nil
+ return type(array) == "table" and is_array(array)
end
-- is dictionary?
function table.is_dictionary(dict)
- return type(dict) == "table" and dict[1] == nil
-end
-
--- dump table
-function table.dump(self, deflate)
- local str = string.dump(self, deflate)
- if str then
- io.write(str)
- end
+ return type(dict) == "table" and not is_array(dict)
end
-- unwrap object if be only one
diff --git a/xmake/core/base/utils.lua b/xmake/core/base/utils.lua
index c19987813..314dcaca7 100644
--- a/xmake/core/base/utils.lua
+++ b/xmake/core/base/utils.lua
@@ -27,6 +27,7 @@ local colors = require("base/colors")
local string = require("base/string")
local table = require("base/table")
local log = require("base/log")
+local dump = require("base/dump")
-- print string with newline
function utils._print(...)
@@ -39,8 +40,8 @@ function utils._print(...)
if type(v) == "string" or type(v) == "boolean" or type(v) == "number" then
io.write(tostring(v))
-- dump table
- elseif type(v) == "table" then
- table.dump(v)
+ elseif type(v) == "table" then
+ dump(v)
else
io.write("<" .. tostring(v) .. ">")
end