diff options
| author | OpportunityLiu <[email protected]> | 2019-06-19 11:30:06 +0800 |
|---|---|---|
| committer | OpportunityLiu <[email protected]> | 2019-06-19 11:30:06 +0800 |
| commit | d5ad005f6c8a79af48f4c73f0ec87775ab746d0d (patch) | |
| tree | eefdd667a032d9732481b61cff3b4d860853ca8f | |
| parent | 5c06711d12932c02ab7b64a25135902fabf00675 (diff) | |
improve dump
| -rw-r--r-- | core/src/xmake/sandbox/interactive.c | 8 | ||||
| -rw-r--r-- | xmake/core/base/colors.lua | 12 | ||||
| -rw-r--r-- | xmake/core/base/dump.lua | 100 | ||||
| -rw-r--r-- | xmake/core/base/string.lua | 4 | ||||
| -rw-r--r-- | xmake/core/base/table.lua | 21 | ||||
| -rw-r--r-- | xmake/core/base/utils.lua | 5 | ||||
| -rw-r--r-- | xmake/core/project/requireinfo.lua | 2 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua | 33 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/utils.lua | 9 | ||||
| -rw-r--r-- | xmake/plugins/lua/xmake.lua | 2 |
10 files changed, 151 insertions, 45 deletions
diff --git a/core/src/xmake/sandbox/interactive.c b/core/src/xmake/sandbox/interactive.c index 64c67af94..941e38c31 100644 --- a/core/src/xmake/sandbox/interactive.c +++ b/core/src/xmake/sandbox/interactive.c @@ -329,15 +329,15 @@ tb_int_t xm_sandbox_interactive(lua_State* lua) // get results count tb_int_t count = lua_gettop(lua) - top; - /* print errors + /* dump results * * stack: arg1(sandbox_scope) [results] -> ... - * after: arg1(sandbox_scope) print [results] -> ... + * after: arg1(sandbox_scope) interactive_dump [results] -> ... */ - lua_getfield(lua, 1, "print"); // load print() from sandbox_scope + lua_getfield(lua, 1, "interactive_dump"); // load interactive_dump() from sandbox_scope lua_insert(lua, -(count + 1)); if (lua_pcall(lua, count, 0, 0) != 0) - tb_printl(lua_pushfstring(lua, "error calling " LUA_QL("print") " (%s)", lua_tostring(lua, -1))); + tb_printl(lua_pushfstring(lua, "error calling " LUA_QL("interactive_dump") " (%s)", lua_tostring(lua, -1))); } } 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 diff --git a/xmake/core/project/requireinfo.lua b/xmake/core/project/requireinfo.lua index f5116328b..38d3f61cb 100644 --- a/xmake/core/project/requireinfo.lua +++ b/xmake/core/project/requireinfo.lua @@ -81,7 +81,7 @@ end -- dump this requireinfo function requireinfo:dump() - table.dump(self._INFO) + utils.dump(self._INFO) end -- get the require info diff --git a/xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua b/xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua index ad448e4be..bae3f596f 100644 --- a/xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua +++ b/xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua @@ -28,26 +28,19 @@ local try = require("sandbox/modules/try") local catch = require("sandbox/modules/catch") local utils = require("base/utils") local history = require("project/history") +local dump = require("base/dump") -- print variables for interactive mode -function sandbox_core_sandbox._interactive_print(format, ...) - - if type(format) == "string" and format:find("%", 1, true) then - local args = {...} - try - { - function () - utils._print(string.format(format, unpack(args))) - end, - catch - { - function (errors) - utils._print(format, unpack(args)) - end - } - } +function sandbox_core_sandbox._interactive_dump(...) + local values = {...} + if #values == 1 then + dump(values[1], "< ") + io.write("\n") else - utils._print(format, ...) + for i, v in ipairs({...}) do + dump(v, "<" .. (i<10 and (i .. " ") or (i .. " "))) + io.write("\n") + end end end @@ -80,11 +73,11 @@ function sandbox_core_sandbox.interactive() end end - -- register print function for interactive mode - instance._PUBLIC.print = sandbox_core_sandbox._interactive_print + -- register dump function for interactive mode + instance._PUBLIC.interactive_dump = sandbox_core_sandbox._interactive_dump -- enter interactive mode with this new sandbox - sandbox.interactive(instance._PUBLIC) + sandbox.interactive(instance._PUBLIC) -- save repl history if readline is enabled if readline then diff --git a/xmake/core/sandbox/modules/utils.lua b/xmake/core/sandbox/modules/utils.lua index 2d026a600..84d8bf208 100644 --- a/xmake/core/sandbox/modules/utils.lua +++ b/xmake/core/sandbox/modules/utils.lua @@ -28,6 +28,7 @@ local try = require("sandbox/modules/try") local catch = require("sandbox/modules/catch") local vformat = require("sandbox/modules/vformat") local raise = require("sandbox/modules/raise") +local dump = require("base/dump") -- define module local sandbox_utils = sandbox_utils or {} @@ -188,6 +189,14 @@ function sandbox_utils.confirm(opt) return utils.confirm(opt) end +-- dump value +function sandbox_utils.dump(value, indent) + if not option.get("quiet") then + dump(value, indent or "") + io.write("\n") + end +end + -- return module return sandbox_utils diff --git a/xmake/plugins/lua/xmake.lua b/xmake/plugins/lua/xmake.lua index 26067e90e..32c929001 100644 --- a/xmake/plugins/lua/xmake.lua +++ b/xmake/plugins/lua/xmake.lua @@ -81,7 +81,7 @@ task("lua") -- run imported modules (xmake lua core.xxx.xxx) result = import(script, {anonymous = true})(unpack(option.get("arguments") or {})) end - if result ~= nil then print(result) end + if result ~= nil then utils.dump(result) end end else -- enter interactive mode |
