summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOpportunityLiu <[email protected]>2019-07-16 11:19:57 +0800
committerOpportunityLiu <[email protected]>2019-07-16 11:19:57 +0800
commitaba70a1655798c7441464fd03fb1867b723974d4 (patch)
treef801b16d668d2b49548fb48369ea82fc98e9f7cc
parentd6be8e7effeca32a035ce89f1d02c28501b6a839 (diff)
improve dump
-rw-r--r--xmake/core/base/dump.lua41
-rw-r--r--xmake/core/base/utils.lua8
-rw-r--r--xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua6
3 files changed, 40 insertions, 15 deletions
diff --git a/xmake/core/base/dump.lua b/xmake/core/base/dump.lua
index cfa25918b..42ec2df17 100644
--- a/xmake/core/base/dump.lua
+++ b/xmake/core/base/dump.lua
@@ -23,7 +23,6 @@ local dump = dump or {}
-- load modules
local colors = require("base/colors")
-local table = require("base/table")
-- format string with theme colors
function dump._format(fmtkey, fmtdefault, ...)
@@ -37,7 +36,12 @@ end
-- translate string with theme formats
function dump._translate(str)
- return colors.translate(str, { patch_reset = false, ignore_unknown = true })
+ 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
@@ -205,10 +209,26 @@ function dump._print_metatable(value, metatable, inner_indent, printed_set, prin
return has_record
end
+-- returns printed_set, is_first_level
+function dump._init_printed_set(printed_set)
+ local first_level = not printed_set
+ if type(printed_set) ~= "table" then
+ printed_set = { len = 0 }
+ end
+ return printed_set, first_level
+end
+
-- print udata
-function dump._print_udata(value, first_indent, remain_indent)
+function dump._print_udata(value, first_indent, remain_indent, printed_set)
+ local first_level
+ printed_set, first_level = dump._init_printed_set(printed_set)
io.write(first_indent)
+
+ if not first_level then
+ return dump._print_udata_scalar(value)
+ end
+
local metatable = getmetatable(value)
local inner_indent = remain_indent .. " "
@@ -216,7 +236,7 @@ function dump._print_udata(value, first_indent, remain_indent)
io.write(dump._translate("${reset}${color.dump.udata}[${reset}"))
-- print metatable
- local no_value = not dump._print_metatable(value, metatable, inner_indent, { len = 0 }, false)
+ local no_value = not dump._print_metatable(value, metatable, inner_indent, printed_set, false)
-- print close brackets
if no_value then
@@ -229,7 +249,8 @@ end
-- print table
function dump._print_table(value, first_indent, remain_indent, printed_set)
- local first_level = not printed_set
+ local first_level
+ printed_set, first_level = dump._init_printed_set(printed_set)
io.write(first_indent)
local metatable = getmetatable(value)
local tostringmethod = metatable and rawget(metatable, "__tostring")
@@ -239,7 +260,7 @@ function dump._print_table(value, first_indent, remain_indent, printed_set)
return dump._print_table_scalar(strrep)
end
end
- printed_set = printed_set or { len = 0 }
+
local inner_indent = remain_indent .. " "
local first_value = true
@@ -260,7 +281,7 @@ function dump._print_table(value, first_indent, remain_indent, printed_set)
end
-- print array items
- local is_arr = table.is_array(value) and (table.maxn(value) < 2 * #value)
+ local is_arr = (value[1] ~= nil) and (table.maxn(value) < 2 * #value)
if is_arr then
for i = 1,table.maxn(value) do
print_newline()
@@ -306,12 +327,12 @@ function dump._print_table(value, first_indent, remain_indent, printed_set)
end
-- print value
-function dump._print(value, indent)
+function dump._print(value, indent, verbose)
indent = tostring(indent or "")
if type(value) == "table" then
- dump._print_table(value, indent, indent:gsub(".", " "), nil)
+ dump._print_table(value, indent, indent:gsub(".", " "), not verbose)
elseif type(value) == "userdata" then
- dump._print_udata(value, indent, indent:gsub(".", " "))
+ dump._print_udata(value, indent, indent:gsub(".", " "), not verbose)
else
io.write(indent)
dump._print_scalar(value)
diff --git a/xmake/core/base/utils.lua b/xmake/core/base/utils.lua
index d8f1ec7c2..5b0c68a5e 100644
--- a/xmake/core/base/utils.lua
+++ b/xmake/core/base/utils.lua
@@ -35,8 +35,10 @@ function utils.dump(...)
return ...
end
+ local diagnosis = option.get("diagnosis")
+
-- show caller info
- if option.get("diagnosis") then
+ if diagnosis then
local info = debug.getinfo(2)
local line = info.currentline
if not line or line < 0 then line = info.linedefined end
@@ -58,11 +60,11 @@ function utils.dump(...)
end
if values_count == 1 then
- dump(values[1], indent or "")
+ dump(values[1], indent or "", diagnosis)
io.write("\n")
else
for i = 1, values_count do
- dump(values[i], indent or string.format("%2d: ", i))
+ dump(values[i], indent or string.format("%2d: ", i), diagnosis)
io.write("\n")
end
end
diff --git a/xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua b/xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua
index 5aa55a0c6..a6f19f93d 100644
--- a/xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua
+++ b/xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua
@@ -30,14 +30,16 @@ local utils = require("base/utils")
local table = require("base/table")
local history = require("project/history")
local dump = require("base/dump")
+local option = require("base/option")
-- print variables for interactive mode
function sandbox_core_sandbox._interactive_dump(...)
+ local diagnosis = option.get("diagnosis")
local values = table.pack(...)
-- do not use #values since nil might be included
local n = values.n
if n <= 1 then
- dump(values[1], "< ")
+ dump(values[1], "< ", diagnosis)
io.write("\n")
else
local fmt = "< %d: "
@@ -50,7 +52,7 @@ function sandbox_core_sandbox._interactive_dump(...)
fmt = "< %2d: "
end
for i = 1, n do
- dump(values[i], string.format(fmt, i))
+ dump(values[i], string.format(fmt, i), diagnosis)
io.write("\n")
end
end