diff options
| author | Opportunity <[email protected]> | 2020-01-17 11:55:43 +0800 |
|---|---|---|
| committer | Opportunity <[email protected]> | 2020-01-17 11:55:43 +0800 |
| commit | 1e36d6b2290a8ad67c34b5a549dcbf87f99d31f1 (patch) | |
| tree | df37a6526c271e2ec1189f2ddef6c3757f2feef2 | |
| parent | 19fef8183e250a49628a4281264753a7da1338dc (diff) | |
improve dump
| -rw-r--r-- | xmake/core/base/bytes.lua | 15 | ||||
| -rw-r--r-- | xmake/core/base/dump.lua | 58 | ||||
| -rw-r--r-- | xmake/core/base/hashset.lua | 17 | ||||
| -rw-r--r-- | xmake/core/base/todisplay.lua | 114 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/todisplay.lua | 23 |
5 files changed, 169 insertions, 58 deletions
diff --git a/xmake/core/base/bytes.lua b/xmake/core/base/bytes.lua index 894f701d4..bdf9b80d6 100644 --- a/xmake/core/base/bytes.lua +++ b/xmake/core/base/bytes.lua @@ -23,10 +23,11 @@ local bytes = bytes or {} local _instance = _instance or {} -- load modules -local bit = require('bit') -local ffi = require('ffi') -local os = require("base/os") -local utils = require("base/utils") +local bit = require('bit') +local ffi = require('ffi') +local os = require("base/os") +local utils = require("base/utils") +local todisplay = require("base/todisplay") -- define ffi interfaces ffi.cdef[[ @@ -409,8 +410,8 @@ function _instance:__concat(other) return new end --- tostring(bytes) -function _instance:__tostring() +-- todisplay(bytes) +function _instance:__todisplay() local parts = {} local size = self:size() if size > 8 then @@ -419,7 +420,7 @@ function _instance:__tostring() for i = 1, size do parts[i] = "0x" .. bit.tohex(self[i], 2) end - return "<bytes(" .. self:size() .. "): " .. table.concat(parts, " ") .. (self:size() > 8 and "..>" or ">") + return "bytes${reset}(" .. todisplay(self:size()) .. ") <${color.dump.number}" .. table.concat(parts, " ") .. (self:size() > 8 and "${reset} ..>" or "${reset}>") end -- new an bytes instance diff --git a/xmake/core/base/dump.lua b/xmake/core/base/dump.lua index 5e7ed9b8f..dbfdc2e3f 100644 --- a/xmake/core/base/dump.lua +++ b/xmake/core/base/dump.lua @@ -22,7 +22,8 @@ local dump = dump or {} -- load modules -local colors = require("base/colors") +local colors = require("base/colors") +local todisplay = require("base/todisplay") -- format string with theme colors function dump._format(fmtkey, fmtdefault, ...) @@ -48,15 +49,9 @@ end 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}")) + 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}")) - end - io.write(str) - if quote then - io.write(dump._translate("${reset}${color.dump.string_quote}\"${reset}")) - else - io.write(dump._translate("${reset}")) + io.write(dump._translate("${reset}${color.dump.string}"), str, dump._translate("${reset}")) end end @@ -67,49 +62,21 @@ end -- print number function dump._print_number(num) - io.write(dump._translate("${reset}${color.dump.number}"), tostring(num), dump._translate("${reset}")) + io.write(dump._translate(todisplay(num))) end -- print function function dump._print_function(func, as_key) - io.write(dump._translate("${reset}${color.dump.function}")) if as_key then - io.write(dump._format("text.dump.default_format", "%s", func)) + io.write(dump._translate("${reset}${color.dump.function}"), dump._format("text.dump.default_format", "%s", func), dump._translate("${reset}")) else - local funcinfo = debug.getinfo(func) - local srcinfo = funcinfo.short_src - if funcinfo.linedefined >= 0 then - srcinfo = srcinfo .. ":" .. funcinfo.linedefined - end - local funcname = funcinfo.name and (funcinfo.name .. " ") or "" - io.write(dump._translate("function ${bright}"), funcname, dump._translate("${reset}${dim}"), srcinfo) + io.write(dump._translate(todisplay(func))) end - io.write(dump._translate("${reset}")) end -- print value with default format function dump._print_default_scalar(value) - io.write(dump._translate("${reset}${color.dump.default}"), dump._format("text.dump.default_format", "%s", value), dump._translate("${reset}")) -end - --- print udata value with scalar format -function dump._print_udata_scalar(value) - local metatable = debug.getmetatable(value) - local tostringmethod = metatable and (rawget(metatable, "__todisplay") or rawget(metatable, "__tostring")) - if tostringmethod then - value = tostringmethod(value) - end - io.write(dump._translate("${reset}${color.dump.udata}"), dump._format("text.dump.udata_format", "%s", value), dump._translate("${reset}")) -end - --- print table value with scalar format -function dump._print_table_scalar(value) - local metatable = debug.getmetatable(value) - local tostringmethod = metatable and (rawget(metatable, "__todisplay") or rawget(metatable, "__tostring")) - if tostringmethod then - value = tostringmethod(value) - end - io.write(dump._translate("${reset}${color.dump.table}"), dump._format("text.dump.table_format", "%s", value), dump._translate("${reset}")) + io.write(dump._translate(todisplay(value))) end -- print scalar value @@ -122,10 +89,6 @@ function dump._print_scalar(value, as_key) dump._print_string(value, as_key) elseif type(value) == "function" then dump._print_function(value, as_key) - elseif type(value) == "userdata" then - dump._print_udata_scalar(value) - elseif type(value) == "table" then - dump._print_table_scalar(value) else dump._print_default_scalar(value) end @@ -290,10 +253,7 @@ 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 - local ok, strrep = pcall(tostringmethod, value, value) - if ok then - return dump._print_table_scalar(strrep) - end + return dump._print_default_scalar(value) end local inner_indent = remain_indent .. " " diff --git a/xmake/core/base/hashset.lua b/xmake/core/base/hashset.lua index 93acbee89..49e4f094e 100644 --- a/xmake/core/base/hashset.lua +++ b/xmake/core/base/hashset.lua @@ -23,10 +23,23 @@ local hashset = hashset or {} local hashset_impl = hashset.__index or {} -- load modules -local table = require("base/table") +local table = require("base/table") +local todisplay = require("base/todisplay") -- representaion for nil key -hashset._NIL = setmetatable({}, {__tostring = function() return "nil" end }) +hashset._NIL = setmetatable({}, { __todisplay = function() return "${color.dump.keyword}nil${reset}" end, __tostring = function() return "${color.dump.keyword}nil${reset}" end }) + +function hashset:__todisplay() + return string.format("hashset${reset}(%s) {%s}", todisplay(self._SIZE), table.concat(table.imap(table.keys(self._DATA), function (i, k) + if i > 10 then + return nil + elseif i == 10 and self._SIZE ~= 10 then + return "..." + else + return todisplay(k) + end + end), ", ")) +end function hashset._to_key(key) if key == nil then diff --git a/xmake/core/base/todisplay.lua b/xmake/core/base/todisplay.lua new file mode 100644 index 000000000..1a2d59d43 --- /dev/null +++ b/xmake/core/base/todisplay.lua @@ -0,0 +1,114 @@ +--!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-2020, TBOOX Open Source Group. +-- +-- @author OpportunityLiu +-- @file todisplay.lua +-- + +-- define module +local todisplay = todisplay or {} + +-- load modules +local colors = require("base/colors") + +-- format string with theme colors +function todisplay._format(fmtkey, fmtdefault, ...) + local theme = colors.theme() + return string.format((theme and theme:get(fmtkey)) or fmtdefault, ...) +end + +-- print keyword +function todisplay._print_keyword(keyword) + return string.format("${reset}${color.dump.keyword}%s${reset}", keyword) +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) +end + +-- print number +function todisplay._print_number(num) + return string.format("${reset}${color.dump.number}%g${reset}", num) +end + +-- print function +function todisplay._print_function(func) + local funcinfo = debug.getinfo(func) + local srcinfo = funcinfo.short_src + if funcinfo.linedefined >= 0 then + 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) +end + +-- print value with default format +function todisplay._print_default_scalar(value, style, formatkey) + 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(__todisplay, value) + if ok then + value = str + end + end + end + if formatkey then + value = todisplay._format(formatkey, "%s", value) + end + return string.format("${reset}%s%s${reset}", style, value) +end + +-- print udata value with scalar format +function todisplay._print_udata_scalar(value) + return todisplay._print_default_scalar(value, "${color.dump.udata}", "text.dump.udata_format") +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") +end + +-- print scalar value +function todisplay._print_scalar(value) + if type(value) == "nil" or type(value) == "boolean" then + return todisplay._print_keyword(value) + elseif type(value) == "number" then + return todisplay._print_number(value) + elseif type(value) == "string" then + return todisplay._print_string(value) + elseif type(value) == "function" then + return todisplay._print_function(value) + elseif type(value) == "userdata" then + return todisplay._print_udata_scalar(value) + elseif type(value) == "table" then + return todisplay._print_table_scalar(value) + else + return todisplay._print_default_scalar(value, "${color.dump.default}", "text.dump.default_format") + end +end + +return todisplay._print_scalar diff --git a/xmake/core/sandbox/modules/todisplay.lua b/xmake/core/sandbox/modules/todisplay.lua new file mode 100644 index 000000000..97824c9bc --- /dev/null +++ b/xmake/core/sandbox/modules/todisplay.lua @@ -0,0 +1,23 @@ +--!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-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file todisplay.lua +-- + +-- load module +return require("base/todisplay") + |
