diff options
| author | ruki <[email protected]> | 2017-05-18 16:28:43 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-05-18 16:28:43 +0800 |
| commit | 75cfb11546cd271ebb8b251d291ad9c7b6277b66 (patch) | |
| tree | a54d87f951beef41ae0be1be17cd47d849b2e416 | |
| parent | 102c4ce16295fe322704b6e5186f325effa989a8 (diff) | |
improve print and table.dump
| -rw-r--r-- | CHANGELOG.md | 2 | ||||
| -rw-r--r-- | core/src/xmake/sandbox/interactive.c | 2 | ||||
| -rw-r--r-- | xmake/core/base/table.lua | 31 | ||||
| -rw-r--r-- | xmake/core/base/utils.lua | 18 | ||||
| -rw-r--r-- | xmake/plugins/lua/xmake.lua | 7 |
5 files changed, 27 insertions, 33 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index aa0b1bb77..2bc4fc5a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ * Improve `import` to load user extension and global modules * [#93](https://github.com/tboox/xmake/pull/93): Improve `xmake lua` to run a single line command * Improve to print gcc error and warning info +* Improve `print` interface to dump table ### Bugs fixed @@ -303,6 +304,7 @@ * 改进`import`接口,去加载用户扩展模块 * [#93](https://github.com/tboox/xmake/pull/93): 改进 `xmake lua`,支持运行单行命令和模块 * 改进编译错误提示信息输出 +* 改进`print`接口去更好些显示table数据 ### Bugs修复 diff --git a/core/src/xmake/sandbox/interactive.c b/core/src/xmake/sandbox/interactive.c index 258256d0e..c4fcaef9b 100644 --- a/core/src/xmake/sandbox/interactive.c +++ b/core/src/xmake/sandbox/interactive.c @@ -338,7 +338,7 @@ tb_int_t xm_sandbox_interactive(lua_State* lua) * stack: arg1(sandbox_scope) [results] -> ... * after: arg1(sandbox_scope) print [results] -> ... */ - lua_getglobal(lua, "print"); + lua_getfield(lua, 1, "print"); // load print() 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))); diff --git a/xmake/core/base/table.lua b/xmake/core/base/table.lua index 3767fc1c4..b7f5719a9 100644 --- a/xmake/core/base/table.lua +++ b/xmake/core/base/table.lua @@ -208,18 +208,9 @@ end -- dump it with the level function table._dump(self, exclude, level) - -- dump string - if type(self) == "string" then - io.write(string.format("%q", self)) - -- dump boolean - elseif type(self) == "boolean" then + -- dump basic type + if type(self) == "string" or type(self) == "boolean" or type(self) == "number" then io.write(tostring(self)) - -- dump number - elseif type(self) == "number" then - io.write(self) - -- dump function - elseif type(self) == "function" then - io.write("<function>") -- dump table elseif type(self) == "table" then @@ -254,9 +245,7 @@ function table._dump(self, exclude, level) end -- dump value - if not table._dump(v, exclude, level + 1) then - return false - end + table._dump(v, exclude, level + 1) -- dump newline io.write("\n") @@ -269,17 +258,9 @@ function table._dump(self, exclude, level) io.write(" ") end io.write("}\n") - -- dump userdata - elseif type(self) == "userdata" then - io.write("<userdata>") - else - -- error - print(string.format("error: invalid object type: %s", type(self))) - return false - end - - -- ok - return true + else + io.write("<" .. tostring(v) .. ">") + end end -- dump it diff --git a/xmake/core/base/utils.lua b/xmake/core/base/utils.lua index 030c10d5c..3bda8060c 100644 --- a/xmake/core/base/utils.lua +++ b/xmake/core/base/utils.lua @@ -29,13 +29,29 @@ local utils = utils or {} local option = require("base/option") local colors = require("base/colors") local string = require("base/string") +local table = require("base/table") -- print string with newline function utils._print(...) -- print it if not quiet if not option.get("quiet") then - print(...) + local values = {...} + for i, v in ipairs(values) do + -- dump basic type + 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) + else + io.write("<" .. tostring(v) .. ">") + end + if i ~= #values then + io.write(" ") + end + end + io.write('\n') end end diff --git a/xmake/plugins/lua/xmake.lua b/xmake/plugins/lua/xmake.lua index 77833e9a3..2789bf26a 100644 --- a/xmake/plugins/lua/xmake.lua +++ b/xmake/plugins/lua/xmake.lua @@ -69,12 +69,7 @@ task("lua") else -- run modules (xmake lua core.xxx.xxx) - local result = import(script)(unpack(option.get("arguments") or {})) - if type(result) == "table" then - table.dump(table) - elseif result ~= nil then - print(result) - end + print(import(script)(unpack(option.get("arguments") or {}))) end else -- enter interactive mode |
