summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOpportunityLiu <[email protected]>2020-01-22 18:18:09 +0800
committerOpportunityLiu <[email protected]>2020-01-22 18:18:09 +0800
commit2fdf5dafd9c7e99ee9493f1c39c39f19b3e50d19 (patch)
treecea20b7d85e984204379f0caa2144aa5ac07d8a3
parent18aa8366b6c0f17f001dffe5dbcc494b0cc8e7dc (diff)
improve dump and todisplay
-rw-r--r--xmake/core/base/dump.lua92
-rw-r--r--xmake/core/base/todisplay.lua162
-rw-r--r--xmake/core/sandbox/modules/todisplay.lua2
3 files changed, 168 insertions, 88 deletions
diff --git a/xmake/core/base/dump.lua b/xmake/core/base/dump.lua
index dbfdc2e3f..4e3494662 100644
--- a/xmake/core/base/dump.lua
+++ b/xmake/core/base/dump.lua
@@ -22,86 +22,52 @@
local dump = dump or {}
-- load modules
-local colors = require("base/colors")
local todisplay = require("base/todisplay")
--- format string with theme colors
-function dump._format(fmtkey, fmtdefault, ...)
- local theme = colors.theme()
- if theme then
- return colors.translate(string.format(theme:get(fmtkey), ...), { patch_reset = false, ignore_unknown = true })
- else
- return string.format(fmtdefault, ...)
- end
-end
-
--- translate string with theme formats
-function dump._translate(str)
- local theme = colors.theme()
- if theme then
- return colors.translate(str, { patch_reset = false, ignore_unknown = true })
- else
- return colors.ignore(str)
- end
-end
-
-- print string
function dump._print_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(dump._translate([[${reset}${color.dump.string_quote}"${reset}${color.dump.string}]]), str, dump._translate([[${reset}${color.dump.string_quote}"${reset}]]))
- else
- io.write(dump._translate("${reset}${color.dump.string}"), str, dump._translate("${reset}"))
- end
+ io.write(todisplay._print_string(str, as_key))
end
-- print keyword
function dump._print_keyword(keyword)
- io.write(dump._translate("${reset}${color.dump.keyword}"), tostring(keyword), dump._translate("${reset}"))
-end
-
--- print number
-function dump._print_number(num)
- io.write(dump._translate(todisplay(num)))
+ io.write(todisplay._print_keyword(keyword))
end
-- print function
function dump._print_function(func, as_key)
if as_key then
- io.write(dump._translate("${reset}${color.dump.function}"), dump._format("text.dump.default_format", "%s", func), dump._translate("${reset}"))
+ io.write(todisplay._translate("${reset}${color.dump.function}"),
+ todisplay._format("text.dump.default_format", "%s", func),
+ todisplay._translate("${reset}"))
else
- io.write(dump._translate(todisplay(func)))
+ io.write(todisplay._print_function(func))
end
end
--- print value with default format
-function dump._print_default_scalar(value)
- io.write(dump._translate(todisplay(value)))
-end
-
-- print scalar value
function dump._print_scalar(value, as_key)
- if type(value) == "nil" or type(value) == "boolean" then
- dump._print_keyword(value)
- elseif type(value) == "number" then
- dump._print_number(value)
- elseif type(value) == "string" then
+ if type(value) == "string" then
dump._print_string(value, as_key)
elseif type(value) == "function" then
dump._print_function(value, as_key)
else
- dump._print_default_scalar(value)
+ io.write(todisplay._print_scalar(value))
end
end
-- print anchor
function dump._print_anchor(printed_set_value)
- io.write(dump._translate("${color.dump.anchor}"), dump._format("text.dump.anchor", "&%s", printed_set_value.id), dump._translate("${reset}"))
+ io.write(todisplay._translate("${color.dump.anchor}"),
+ todisplay._format("text.dump.anchor", "&%s", printed_set_value.id),
+ todisplay._translate("${reset}"))
end
-- print reference
function dump._print_reference(printed_set_value)
- io.write(dump._translate("${color.dump.reference}"), dump._format("text.dump.reference", "*%s", printed_set_value.id), dump._translate("${reset}"))
+ io.write(todisplay._translate("${color.dump.reference}"),
+ todisplay._format("text.dump.reference", "*%s", printed_set_value.id),
+ todisplay._translate("${reset}"))
end
-- print anchor and store to printed_set
@@ -135,12 +101,16 @@ function dump._print_metatable(value, metatable, inner_indent, printed_set, prin
io.write("\n", inner_indent)
local funcname = k:sub(3)
dump._print_keyword(funcname)
- io.write(dump._translate("${reset} ${dim}=${reset} "))
- if funcname == "tostring" or funcname == "len" then
+ io.write(todisplay._translate("${reset} ${dim}=${reset} "))
+ if funcname == "tostring" or funcname == "len" or funcname == "todisplay" then
local ok, result = pcall(v, value, value)
if ok then
- dump._print_scalar(result)
- io.write(" (evaluated)")
+ if funcname == "todisplay" and type(result) == "string" then
+ io.write(todisplay._translate(result))
+ else
+ dump._print_scalar(result)
+ end
+ io.write(todisplay._translate("${dim} (evaluated)${reset}"))
else
dump._print_scalar(v)
end
@@ -172,7 +142,7 @@ function dump._print_metatable(value, metatable, inner_indent, printed_set, prin
dump._print_keyword("(")
dump._print_scalar(k, true)
dump._print_keyword(")")
- io.write(dump._translate("${reset} ${dim}=${reset} "))
+ io.write(todisplay._translate("${reset} ${dim}=${reset} "))
if v and printed_set.refs[v] then
dump._print_reference(printed_set.refs[v])
else
@@ -231,16 +201,16 @@ function dump._print_udata(value, first_indent, remain_indent, printed_set)
local inner_indent = remain_indent .. " "
-- print open brackets
- io.write(dump._translate("${reset}${color.dump.udata}[${reset}"))
+ io.write(todisplay._translate("${reset}${color.dump.udata}[${reset}"))
-- print metatable
local no_value = not dump._print_metatable(value, metatable, inner_indent, printed_set, false)
-- print close brackets
if no_value then
- io.write(dump._translate(" ${color.dump.udata}]${reset}"))
+ io.write(todisplay._translate(" ${color.dump.udata}]${reset}"))
else
- io.write("\b \n", remain_indent, dump._translate("${reset}${color.dump.udata}]${reset}"))
+ io.write("\b \n", remain_indent, todisplay._translate("${reset}${color.dump.udata}]${reset}"))
end
end
@@ -253,14 +223,14 @@ function dump._print_table(value, first_indent, remain_indent, printed_set)
local metatable = debug.getmetatable(value)
local tostringmethod = metatable and (rawget(metatable, "__todisplay") or rawget(metatable, "__tostring"))
if not first_level and tostringmethod then
- return dump._print_default_scalar(value)
+ return dump._print_scalar(value)
end
local inner_indent = remain_indent .. " "
local first_value = true
-- print open brackets
- io.write(dump._translate("${reset}${color.dump.table}{${reset}"))
+ io.write(todisplay._translate("${reset}${color.dump.table}{${reset}"))
local function print_newline()
if first_value then
@@ -299,7 +269,7 @@ function dump._print_table(value, first_indent, remain_indent, printed_set)
if not is_arr or type(k) ~= "number" then
print_newline()
dump._print_scalar(k, true)
- io.write(dump._translate("${reset} ${dim}=${reset} "))
+ io.write(todisplay._translate("${reset} ${dim}=${reset} "))
if type(v) == "table" then
if printed_set.refs[v] then
dump._print_reference(printed_set.refs[v])
@@ -315,9 +285,9 @@ function dump._print_table(value, first_indent, remain_indent, printed_set)
-- print close brackets
if first_value then
- io.write(dump._translate(" ${color.dump.table}}${reset}"))
+ io.write(todisplay._translate(" ${color.dump.table}}${reset}"))
else
- io.write("\b \n", remain_indent, dump._translate("${reset}${color.dump.table}}${reset}"))
+ io.write("\b \n", remain_indent, todisplay._translate("${reset}${color.dump.table}}${reset}"))
end
end
diff --git a/xmake/core/base/todisplay.lua b/xmake/core/base/todisplay.lua
index ed821e2f2..92af86975 100644
--- a/xmake/core/base/todisplay.lua
+++ b/xmake/core/base/todisplay.lua
@@ -23,26 +23,58 @@ local todisplay = todisplay or {}
-- load modules
local colors = require("base/colors")
+local math = require("base/math")
+
+
+function todisplay._reset()
+ local reset = todisplay._RESET
+ if not reset then
+ reset = colors.translate("${reset}")
+ todisplay._RESET = reset
+ end
+ return reset
+end
-- format string with theme colors
function todisplay._format(fmtkey, fmtdefault, ...)
local theme = colors.theme()
- return string.format((theme and theme:get(fmtkey)) or fmtdefault, ...)
+ if theme then
+ return colors.translate(string.format(theme:get(fmtkey), ...), { patch_reset = false, ignore_unknown = true })
+ else
+ return string.format(fmtdefault, ...)
+ end
+end
+
+-- translate string with theme formats
+function todisplay._translate(str)
+ local theme = colors.theme()
+ if theme then
+ return colors.translate(str, { patch_reset = false, ignore_unknown = true })
+ else
+ return colors.ignore(str)
+ end
end
-- print keyword
function todisplay._print_keyword(keyword)
- return string.format("${reset}${color.dump.keyword}%s${reset}", keyword)
+ return todisplay._translate("${reset}${color.dump.keyword}") .. tostring(keyword) .. todisplay._reset()
end
-- print string
-function todisplay._print_string(str)
- return string.format([[${reset}${color.dump.string_quote}"${reset}${color.dump.string}%s${reset}${color.dump.string_quote}"${reset}]], str)
+function todisplay._print_string(str, as_key)
+ local quote = (not as_key) or (not str:match("^[a-zA-Z_][a-zA-Z0-9_]*$"))
+ if quote then
+ return todisplay._translate([[${reset}${color.dump.string_quote}"${reset}${color.dump.string}]])
+ .. str
+ .. todisplay._translate([[${reset}${color.dump.string_quote}"${reset}]])
+ else
+ return todisplay._translate("${reset}${color.dump.string}") .. str .. todisplay._reset()
+ end
end
-- print number
function todisplay._print_number(num)
- return string.format("${reset}${color.dump.number}%s${reset}", num)
+ return todisplay._translate("${reset}${color.dump.number}") .. tostring(num) .. todisplay._reset()
end
-- print function
@@ -53,33 +85,43 @@ function todisplay._print_function(func)
srcinfo = srcinfo .. ":" .. funcinfo.linedefined
end
local funcname = funcinfo.name and (funcinfo.name .. " ") or ""
- return string.format("${reset}${color.dump.function}function ${bright}%s${reset}${dim}%s${reset}", funcname, srcinfo)
+ return todisplay._translate("${reset}${color.dump.function}function ${bright}")
+ .. funcname
+ .. todisplay._translate("${reset}${dim}")
+ .. srcinfo .. todisplay._reset()
end
--- print value with default format
-function todisplay._print_default_scalar(value, style, formatkey)
+function todisplay._get_tostr_method(value)
local metatable = debug.getmetatable(value)
if metatable then
local __todisplay = rawget(metatable, "__todisplay")
local __tostring = rawget(metatable, "__tostring")
- if __todisplay then
- local ok, str = pcall(__todisplay, value)
- if ok then
- value = str
- -- disable format
- formatkey = nil
- end
- elseif __tostring then
- local ok, str = pcall(__tostring, value)
- if ok then
- value = str
- end
+ return __todisplay, __tostring
+ end
+ return nil, nil
+end
+
+-- print value with default format
+function todisplay._print_default_scalar(value, style, formatkey)
+ local __todisplay, __tostring = todisplay._get_tostr_method(value)
+ if __todisplay then
+ local ok, str = pcall(__todisplay, value)
+ if ok then
+ value = todisplay._translate(str)
+ -- disable format
+ formatkey = nil
+ end
+ elseif __tostring then
+ local ok, str = pcall(__tostring, value)
+ if ok then
+ value = str
end
end
if formatkey then
value = todisplay._format(formatkey, "%s", value)
end
- return string.format("${reset}%s%s${reset}", style, value)
+ local reset = todisplay._reset()
+ return reset .. todisplay._translate(style) .. value .. reset
end
-- print udata value with scalar format
@@ -88,12 +130,70 @@ function todisplay._print_udata_scalar(value)
end
-- print table value with scalar format
-function todisplay._print_table_scalar(value)
- return todisplay._print_default_scalar(value, "${color.dump.table}", "text.dump.table_format")
+function todisplay._print_table_scalar(value, expand)
+ if debug.getmetatable(value) or not expand then
+ return todisplay._print_default_scalar(value, "${color.dump.table}", "text.dump.table_format")
+ end
+ local pvalues = {}
+ local has_string_key = false
+ local as, ae = math.huge, -math.huge
+
+ local k, v
+ for i = 1, 10 do
+ k, v = next(value, k)
+ if k == nil then break end
+
+ if type(k) == "string" then
+ pvalues[k] = todisplay._print_scalar(v, false)
+ has_string_key = true
+ elseif type(k) == "number" and math.isint(k) then
+ pvalues[k] = todisplay._print_scalar(v, false)
+ as = math.min(k, as)
+ ae = math.max(k, ae)
+ else
+ -- no common table or array
+ return todisplay._print_default_scalar(value, "${color.dump.table}", "text.dump.table_format")
+ end
+ end
+ if k ~= nil then
+ -- to large
+ return todisplay._print_default_scalar(value, "${color.dump.table}", "text.dump.table_format")
+ end
+
+ local results = {}
+ local is_arr = as >= 1 and ae <= 20
+ if is_arr then
+ local nilstr
+ for i = 1, ae do
+ local pv = pvalues[i]
+ if pv == nil then
+ if not nilstr then
+ nilstr = todisplay._print_keyword("nil")
+ end
+ pv = nilstr
+ end
+ results[i] = pv
+ end
+ end
+ if has_string_key then
+ for pk, pv in pairs(pvalues) do
+ if type(pk) == "string" then
+ local pkstr = todisplay._print_string(pk, true)
+ table.insert(results, pkstr .. " = " .. pv)
+ elseif not is_arr then
+ local pkstr = todisplay._print_number(pk)
+ table.insert(results, pkstr .. " = " .. pv)
+ end
+ end
+ end
+ if #results == 0 then
+ return todisplay._translate("${reset}${color.dump.table}") .. "{ }" .. todisplay._reset()
+ end
+ return todisplay._translate("${reset}${color.dump.table}") .. "{ " .. table.concat(results, ", ") .." }" .. todisplay._reset()
end
-- print scalar value
-function todisplay._print_scalar(value)
+function todisplay._print_scalar(value, expand)
if type(value) == "nil" or type(value) == "boolean" then
return todisplay._print_keyword(value)
elseif type(value) == "number" then
@@ -105,10 +205,20 @@ function todisplay._print_scalar(value)
elseif type(value) == "userdata" then
return todisplay._print_udata_scalar(value)
elseif type(value) == "table" then
- return todisplay._print_table_scalar(value)
+ return todisplay._print_table_scalar(value, expand)
else
return todisplay._print_default_scalar(value, "${color.dump.default}", "text.dump.default_format")
end
end
-return todisplay._print_scalar
+function todisplay.print(value)
+ return todisplay._print_scalar(value, true)
+end
+
+setmetatable(todisplay, {
+ __call = function (_, ...)
+ return todisplay.print(...)
+ end
+})
+
+return todisplay
diff --git a/xmake/core/sandbox/modules/todisplay.lua b/xmake/core/sandbox/modules/todisplay.lua
index 97824c9bc..e0ecf30cf 100644
--- a/xmake/core/sandbox/modules/todisplay.lua
+++ b/xmake/core/sandbox/modules/todisplay.lua
@@ -19,5 +19,5 @@
--
-- load module
-return require("base/todisplay")
+return require("base/todisplay").print