diff options
| author | ruki <[email protected]> | 2019-06-19 16:00:57 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2019-06-19 16:00:57 +0800 |
| commit | 526556809d220564e6565fbe2c6deec723c9f343 (patch) | |
| tree | 8746466d5a31961a116bae3bf640c1fe32412305 | |
| parent | 5c06711d12932c02ab7b64a25135902fabf00675 (diff) | |
| parent | d9a5e75f51b12370a6ff8e15ad0c37f23ce3edb8 (diff) | |
Merge pull request #458 from OpportunityLiu/dev
improve dump
| -rw-r--r-- | core/src/xmake/sandbox/interactive.c | 8 | ||||
| -rw-r--r-- | xmake/core/base/colors.lua | 17 | ||||
| -rw-r--r-- | xmake/core/base/dump.lua | 128 | ||||
| -rw-r--r-- | xmake/core/base/string.lua | 4 | ||||
| -rw-r--r-- | xmake/core/base/table.lua | 10 | ||||
| -rw-r--r-- | xmake/core/base/utils.lua | 14 | ||||
| -rw-r--r-- | xmake/core/project/requireinfo.lua | 2 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua | 37 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/utils.lua | 7 | ||||
| -rw-r--r-- | xmake/plugins/lua/xmake.lua | 2 | ||||
| -rw-r--r-- | xmake/themes/dark/xmake.lua | 9 | ||||
| -rw-r--r-- | xmake/themes/default/xmake.lua | 9 | ||||
| -rw-r--r-- | xmake/themes/emoji/xmake.lua | 9 | ||||
| -rw-r--r-- | xmake/themes/plain/xmake.lua | 9 |
14 files changed, 217 insertions, 48 deletions
diff --git a/core/src/xmake/sandbox/interactive.c b/core/src/xmake/sandbox/interactive.c index 64c67af94..6fc643520 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..e3b31a0bd 100644 --- a/xmake/core/base/colors.lua +++ b/xmake/core/base/colors.lua @@ -257,9 +257,10 @@ 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 opt options +-- patch_reset: wrap str with `"${reset}"`? +-- -- 8 colors: -- -- "${red}hello" @@ -295,21 +296,25 @@ end -- "${hello xmake}" -- "${hello xmake $beer}" -- -function colors.translate(str) +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 - str = "${reset}" .. str .. "${reset}" + 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 new file mode 100644 index 000000000..f7012a539 --- /dev/null +++ b/xmake/core/base/dump.lua @@ -0,0 +1,128 @@ +--!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 OpportunityLiu +-- @file dump.lua +-- + +-- define module +local dump = dump or {} + +-- load modules +local colors = require("base/colors") +local table = require("base/table") + + +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("${reset}${color.dump.string_quote}\"${reset}${color.dump.string}", { patch_reset = false })) + else + io.write(colors.translate("${reset}${color.dump.string}", { patch_reset = false })) + end + io.write(str) + if quote then + io.write(colors.translate("${reset}${color.dump.string_quote}\"${reset}", { patch_reset = false })) + else + io.write(colors.translate("${reset}", { patch_reset = false })) + end +end + +function dump._keyword(keyword) + io.write(colors.translate("${color.dump.keyword}" .. tostring(keyword))) +end + +function dump._number(num) + io.write(colors.translate("${color.dump.number}" .. tostring(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("${color.dump.function}function ${bright}" .. (funcinfo.name or "") .. "${reset}${dim}" .. srcinfo)) +end + +function dump._default(value) + io.write(colors.translate("${color.dump.default}", { patch_reset = false })) + local fmt = colors.theme():get("text.dump.default_format") or "%s" + io.write(string.format(fmt, value)) + io.write(colors.translate("${reset}", { patch_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 or type(k) ~= "number" 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._print(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._print
\ 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..e21680379 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 @@ -186,14 +186,6 @@ 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 -end - -- unwrap object if be only one function table.unwrap(object) if type(object) == "table" then diff --git a/xmake/core/base/utils.lua b/xmake/core/base/utils.lua index c19987813..caa633c7c 100644 --- a/xmake/core/base/utils.lua +++ b/xmake/core/base/utils.lua @@ -25,8 +25,16 @@ local utils = utils or {} local option = require("base/option") 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") + +-- dump value +function utils.dump(value, indent) + if not option.get("quiet") then + dump(value, indent or "") + io.write("\n") + end +end -- print string with newline function utils._print(...) @@ -39,8 +47,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..3f9ed4ccc 100644 --- a/xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua +++ b/xmake/core/sandbox/modules/import/core/sandbox/sandbox.lua @@ -28,26 +28,20 @@ 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(v0, ...) + local values = { v0, ... } + if #values <= 1 then + -- in case v0 is nil + dump(v0, "< ") + io.write("\n") else - utils._print(format, ...) + for i, v in ipairs(values) do + dump(v, "<" .. ((i < 10) and (i .. " ") or (i .. " "))) + io.write("\n") + end end end @@ -60,7 +54,8 @@ function sandbox_core_sandbox.interactive() raise("cannot get sandbox instance!") end - -- fork a new sandbox + -- fork a new sandbox + local errors instance, errors = instance:fork() if not instance then raise(errors) @@ -80,11 +75,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..7e3129fcb 100644 --- a/xmake/core/sandbox/modules/utils.lua +++ b/xmake/core/sandbox/modules/utils.lua @@ -175,7 +175,7 @@ function sandbox_utils.assert(value, format, ...) if format ~= nil then raise(format, ...) else - raise("assertion failed!") + raise("assertion failed!") end end @@ -188,6 +188,11 @@ function sandbox_utils.confirm(opt) return utils.confirm(opt) end +-- dump value +function sandbox_utils.dump(value, indent) + return utils.dump(value, indent) +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 diff --git a/xmake/themes/dark/xmake.lua b/xmake/themes/dark/xmake.lua index dcf8119d3..e31c32b0a 100644 --- a/xmake/themes/dark/xmake.lua +++ b/xmake/themes/dark/xmake.lua @@ -51,3 +51,12 @@ theme("dark") -- the building target file set_color("build.target", "magenta") + -- color dump + set_text("dump.default_format", "<%s>") + set_color("dump.default", "bright") + set_color("dump.string", "magenta bright") + set_color("dump.string_quote", "magenta") + set_color("dump.keyword", "blue") + set_color("dump.number", "green") + set_color("dump.function", "cyan") + diff --git a/xmake/themes/default/xmake.lua b/xmake/themes/default/xmake.lua index 86037da4b..69cef1fd5 100644 --- a/xmake/themes/default/xmake.lua +++ b/xmake/themes/default/xmake.lua @@ -51,3 +51,12 @@ theme("default") -- the building target file set_color("build.target", "magenta") + -- color dump + set_text("dump.default_format", "<%s>") + set_color("dump.default", "bright") + set_color("dump.string", "magenta bright") + set_color("dump.string_quote", "magenta") + set_color("dump.keyword", "blue") + set_color("dump.number", "green") + set_color("dump.function", "cyan") + diff --git a/xmake/themes/emoji/xmake.lua b/xmake/themes/emoji/xmake.lua index afb137928..0148891f7 100644 --- a/xmake/themes/emoji/xmake.lua +++ b/xmake/themes/emoji/xmake.lua @@ -51,3 +51,12 @@ theme("emoji") -- the building target file set_color("build.target", "magenta") + -- color dump + set_text("dump.default_format", "<%s>") + set_color("dump.default", "bright") + set_color("dump.string", "magenta bright") + set_color("dump.string_quote", "magenta") + set_color("dump.keyword", "blue") + set_color("dump.number", "green") + set_color("dump.function", "cyan") + diff --git a/xmake/themes/plain/xmake.lua b/xmake/themes/plain/xmake.lua index 3417eb317..0771a857d 100644 --- a/xmake/themes/plain/xmake.lua +++ b/xmake/themes/plain/xmake.lua @@ -51,3 +51,12 @@ theme("plain") -- the building target file set_color("build.target", "") + -- color dump + set_text("dump.default_format", "<%s>") + set_color("dump.default", "") + set_color("dump.string", "") + set_color("dump.string_quote", "") + set_color("dump.keyword", "") + set_color("dump.number", "") + set_color("dump.function", "") + |
