From 9862d1b95fa5978eaf24d65f1ca5f151d410a14b Mon Sep 17 00:00:00 2001 From: Opportunity Date: Wed, 15 Jan 2020 11:24:36 +0800 Subject: Improve options parsing --- xmake/core/base/utils.lua | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'xmake/core/base/utils.lua') diff --git a/xmake/core/base/utils.lua b/xmake/core/base/utils.lua index d85d1ecd9..ca13ab097 100644 --- a/xmake/core/base/utils.lua +++ b/xmake/core/base/utils.lua @@ -42,7 +42,7 @@ function utils.dump(...) local info = debug.getinfo(2) local line = info.currentline if not line or line < 0 then line = info.linedefined end - io.write(string.format("dump form %s %s:%s\n", info.name or "", info.source, line)) + io.write(string.format("dump from %s %s:%s\n", info.name or "", info.source, line)) end local values = table.pack(...) @@ -327,5 +327,13 @@ function utils.confirm(opt) return confirm end +function utils.table(data, opt) + utils.printf(colors.table(data, opt)) +end + +function utils.vtable(data, opt) + utils.vprintf(colors.table(data, opt)) +end + -- return module return utils -- cgit v1.3.1 From f4e30b5e7879d4dbfbe4424f5f9156cb6cfdaf50 Mon Sep 17 00:00:00 2001 From: Opportunity Date: Wed, 15 Jan 2020 12:06:01 +0800 Subject: Improve dump --- xmake/core/base/dump.lua | 14 ++++++++++++-- xmake/core/base/utils.lua | 21 ++++++--------------- xmake/plugins/lua/xmake.lua | 10 ++++++---- 3 files changed, 24 insertions(+), 21 deletions(-) (limited to 'xmake/core/base/utils.lua') diff --git a/xmake/core/base/dump.lua b/xmake/core/base/dump.lua index 3adb54133..5e7ed9b8f 100644 --- a/xmake/core/base/dump.lua +++ b/xmake/core/base/dump.lua @@ -88,17 +88,27 @@ function dump._print_function(func, as_key) end -- print value with default format -function dump._print_default(value) +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}")) end @@ -117,7 +127,7 @@ function dump._print_scalar(value, as_key) elseif type(value) == "table" then dump._print_table_scalar(value) else - dump._print_default(value) + dump._print_default_scalar(value) end end diff --git a/xmake/core/base/utils.lua b/xmake/core/base/utils.lua index ca13ab097..fa42d7e25 100644 --- a/xmake/core/base/utils.lua +++ b/xmake/core/base/utils.lua @@ -29,7 +29,7 @@ local log = require("base/log") local io = require("base/io") local dump = require("base/dump") --- dump value +-- dump values function utils.dump(...) if option.get("quiet") then return ... @@ -49,25 +49,16 @@ function utils.dump(...) if values.n == 0 then return end - local indent = nil - local values_count = values.n - values.n = nil - -- use last input as indent if it is a string - if values_count > 1 and type(values[values_count]) == "string" then - indent = values[values_count] - values[values_count] = nil - values_count = values_count - 1 - end - if values_count == 1 then - dump(values[1], indent or "", diagnosis) + if values.n == 1 then + dump(values[1], "", diagnosis) else - for i = 1, values_count do - dump(values[i], indent or string.format("%2d: ", i), diagnosis) + for i = 1, values.n do + dump(values[i], string.format("%2d: ", i), diagnosis) end end - return table.unpack(values, 1, values_count) + return table.unpack(values, 1, values.n) end -- print string with newline diff --git a/xmake/plugins/lua/xmake.lua b/xmake/plugins/lua/xmake.lua index 57b2bc5f4..e264ba14d 100644 --- a/xmake/plugins/lua/xmake.lua +++ b/xmake/plugins/lua/xmake.lua @@ -54,10 +54,12 @@ task("lua") if script then local args = option.get("arguments") or {} + args.n = #args for i, value in ipairs(args) do if value:startswith('@') then local v, err = string.deserialize(value:sub(2)) if err then + -- for strings that failed to deserialize, regaed it as a normal string, just show a warning message utils.warning(err) else args[i] = v @@ -70,13 +72,13 @@ task("lua") -- run the given lua script file (xmake lua /tmp/script.lua) vprint("running given lua script file: %s", path.relative(script)) - import(path.basename(script), {rootdir = path.directory(script), anonymous = true})(unpack(args)) + import(path.basename(script), {rootdir = path.directory(script), anonymous = true})(table.unpack(args, 1, args.n)) elseif os.isfile(path.join(os.scriptdir(), "scripts", script .. ".lua")) then -- run builtin lua script (xmake lua echo "hello xmake") vprint("running builtin lua script: %s", script) - import("scripts." .. script, {anonymous = true})(unpack(args)) + import("scripts." .. script, {anonymous = true})(table.unpack(args, 1, args.n)) else -- attempt to find the builtin module @@ -91,11 +93,11 @@ task("lua") if object then -- run builtin modules (xmake lua core.xxx.xxx) vprint("running builtin module: %s", script) - result = table.pack(object(unpack(args))) + result = table.pack(object(table.unpack(args, 1, args.n))) else -- run imported modules (xmake lua core.xxx.xxx) vprint("running imported module: %s", script) - result = table.pack(import(script, {anonymous = true})(unpack(args))) + result = table.pack(import(script, {anonymous = true})(table.unpack(args, 1, args.n))) end if result and result.n ~= 0 then utils.dump(unpack(result, 1, result.n)) end end -- cgit v1.3.1 From 9cca6d065a6d8756876f6877725eeaa1304a6ef7 Mon Sep 17 00:00:00 2001 From: Opportunity Date: Thu, 16 Jan 2020 17:25:48 +0800 Subject: fix style --- xmake/core/base/cli.lua | 22 +++++++++---------- xmake/core/base/option.lua | 4 ++-- xmake/core/base/text.lua | 53 +++++++++++++++++++++++++++++----------------- xmake/core/base/utils.lua | 5 +++-- 4 files changed, 49 insertions(+), 35 deletions(-) (limited to 'xmake/core/base/utils.lua') diff --git a/xmake/core/base/cli.lua b/xmake/core/base/cli.lua index 310335fd4..ea328c22e 100644 --- a/xmake/core/base/cli.lua +++ b/xmake/core/base/cli.lua @@ -49,15 +49,15 @@ function cli._make_segment(type, string, argv, argi, obj) end function cli._make_arg(value, argv, argi) - return cli._make_segment('arg', value, argv, argi, { value = value }) + return cli._make_segment("arg", value, argv, argi, { value = value }) end function cli._make_flag(key, short, argv, argi) - return cli._make_segment('flag', short and ('-' .. key) or ('--' .. key), argv, argi, { key = key, value = true, short = short or false }) + return cli._make_segment("flag", short and ("-" .. key) or ("--" .. key), argv, argi, { key = key, value = true, short = short or false }) end function cli._make_option(key, value, short, argv, argi) - return cli._make_segment('option', short and ('-' .. key .. ' ' .. value) or ('--' .. key .. '=' .. value), argv, argi, { key = key, value = value, short = short or false }) + return cli._make_segment("option", short and ("-" .. key .. " " .. value) or ("--" .. key .. "=" .. value), argv, argi, { key = key, value = value, short = short or false }) end function cli.parse(args, ...) @@ -74,17 +74,17 @@ function cli.parsev(argv, flags) while index <= #argv do value = argv[index] - if raw or not value:startswith('-') or #value < 2 then - -- all args after '--' or first arg, args don't start with '-', and short args (include a single char '-') + if raw or not value:startswith("-") or #value < 2 then + -- all args after "--" or first arg, args don"t start with "-", and short args (include a single char "-") raw = true table.insert(parsed, cli._make_arg(value, argv, index)) - elseif value == '--' then - -- stop parsing after '--' + elseif value == "--" then + -- stop parsing after "--" raw = true - table.insert(parsed, cli._make_segment('sep', '--', argv, index, {})) - elseif value:startswith('--') then - -- '--key:value', '--key=value', '--long-flag' - local sep = value:find('[=:]', 3, false) + table.insert(parsed, cli._make_segment("sep", "--", argv, index, {})) + elseif value:startswith("--") then + -- "--key:value", "--key=value", "--long-flag" + local sep = value:find("[=:]", 3, false) if sep then table.insert(parsed, cli._make_option(value:sub(3, sep - 1), value:sub(sep + 1), false, argv, index)) else diff --git a/xmake/core/base/option.lua b/xmake/core/base/option.lua index b9721edec..dc472bccc 100644 --- a/xmake/core/base/option.lua +++ b/xmake/core/base/option.lua @@ -195,7 +195,7 @@ function option.parse(argv, options, opt) assert(o and ((mode ~= "v" and mode ~= "vs") or name)) -- fill short flags - if o[3] == 'k' and o[1] then + if o[3] == "k" and o[1] then table.insert(flags, o[1]) end end @@ -651,7 +651,7 @@ function option.show_main() -- sort categories categories = table.values(categories) table.sort(categories, function (a, b) - if a.name == 'action' then + if a.name == "action" then return true end return a.name < b.name diff --git a/xmake/core/base/text.lua b/xmake/core/base/text.lua index 462c757a1..965c1daa9 100644 --- a/xmake/core/base/text.lua +++ b/xmake/core/base/text.lua @@ -66,10 +66,10 @@ function text.wordwrap(str, width, opt) opt = opt or {} -- split to lines - if type(str) == 'table' then - str = table.concat(str, '\n') + if type(str) == "table" then + str = table.concat(str, "\n") end - local lines = tostring(str):split('\n', {plain = true, strict = true}) + local lines = tostring(str):split("\n", {plain = true, strict = true}) local result = {} local actual_width = 0 @@ -77,7 +77,7 @@ function text.wordwrap(str, width, opt) -- handle lines for _, v in ipairs(lines) do - -- remove tailing spaces, include '\r', which will be produced by `('l1\r\nl2'):split(...)` + -- remove tailing spaces, include "\r", which will be produced by `("l1\r\nl2"):split(...)` v = v:rtrim() while #v > width do @@ -140,17 +140,17 @@ end -- @param data table data, array of array of cells with optional styles -- eg: { -- {"1", nil, "3"}, -- use nil to make previous cell to span next column --- {"4", "5", {"line1", "line2", style="${yellow}", align = 'r'}}, -- multi-line content & set style or align for cell --- {"7", "8", {"9", style="${reset}${red}"}, style="${bright}", align = 'c'}, -- set style or align for row +-- {"4", "5", {"line1", "line2", style="${yellow}", align = "r"}}, -- multi-line content & set style or align for cell +-- {"7", "8", {"9", style="${reset}${red}"}, style="${bright}", align = "c"}, -- set style or align for row -- style = {"${underline}"}, -- set style for columns -- -- or use "${underline}" for all columns -- width = { 20, {10, 50}, "auto"}, -- -- 2 numbers - min and max width (nil for not set, eg: {nil, 50}); -- -- a number - width, num is equivalent to {num, num}; -- -- nil - no limit, equivalent to {nil, nil} --- -- "auto" - use remain space of console, only one 'auto' colunm is allowed --- align = {'l', 'r', 'c'} -- align mode for each column, 'left', 'center' or 'right' --- sep = "${dim} | ", -- table colunm sepertor, default is ' | ', use '' to hide +-- -- "auto" - use remain space of console, only one "auto" colunm is allowed +-- align = {"l", "r", "c"} -- align mode for each column, "left", "center" or "right" +-- sep = "${dim} | ", -- table colunm sepertor, default is " | ", use "" to hide -- } -- priority of style and align: cell > row > col -- @param opt options for color rendering and word warpping @@ -160,9 +160,8 @@ function text.table(data, opt) -- init options opt = opt or { patch_reset = false, ignore_unknown = true } + data.sep = data.sep or " | " opt.patch_reset = false - local sep = colors.translate(data.sep or ' | ', opt) - local sep_len = #colors.ignore(data.sep or ' | ', opt) -- col ordered cells local cols = {} @@ -214,8 +213,12 @@ function text.table(data, opt) if type(data.style) == "string" then style = data.style data.style = {} + data.sep = style .. data.sep .. "${reset}" end + local sep = colors.translate(data.sep, opt) + local sep_len = #colors.ignore(data.sep, opt) + -- index of auto col local auto_col = nil for i = 1, n_col do @@ -226,7 +229,7 @@ function text.table(data, opt) local wl, wu if w == nil then wl, wu = 0, math.huge - elseif type(w) == 'number' then + elseif type(w) == "number" then if math.isnan(w) or math.isinf(w) then wl, wu = 0, math.huge else @@ -239,12 +242,12 @@ function text.table(data, opt) wu = wu or math.huge data.width[i] = {wl, wu} else - assert(not auto_col, 'Only one "auto" colunm is allowed.') + assert(not auto_col, "Only one 'auto' colunm is allowed.") auto_col = i end -- load align - cols[i].align = (data.align[i] or 'l'):sub(1, 1):lower() + cols[i].align = (data.align[i] or "l"):sub(1, 1):lower() -- load style cols[i].style = data.style[i] or style end @@ -299,6 +302,7 @@ function text.table(data, opt) -- reorder for i = 1, n_row do + local d_row = data[i] or {} local row = {} local line = 1 for j = 1, n_col do @@ -310,6 +314,15 @@ function text.table(data, opt) row[j] = cell end row.line = line + + -- load align + if d_row.align then + row.align = d_row.align:sub(1, 1):lower() + end + + -- load style + row.style = d_row.style or "" + rows[i] = row end @@ -341,18 +354,18 @@ function text.table(data, opt) end local padded - if cell.align == 'r' then + if cell.align == "r" then -- right align - padded = string.rep(' ', width - #str) .. str - elseif cell.align == 'c' then + padded = string.rep(" ", width - #str) .. str + elseif cell.align == "c" then -- centered local padding = width - #str local lp = math.floor(padding / 2) local rp = math.ceil(padding / 2) - padded = string.rep(' ', lp) .. str .. string.rep(' ', rp) + padded = string.rep(" ", lp) .. str .. string.rep(" ", rp) else --left align, emit tailing spaces for last colunm - padded = str .. ((j + span == n_col + 1) and "" or string.rep(' ', width - #str)) + padded = str .. ((j + span == n_col + 1) and "" or string.rep(" ", width - #str)) end table.insert(cells, cell.style .. padded .. reset) j = j + span @@ -363,7 +376,7 @@ function text.table(data, opt) -- concat rendered rows results[#results + 1] = "" - return table.concat(results, '\n') + return table.concat(results, "\n") end -- return module diff --git a/xmake/core/base/utils.lua b/xmake/core/base/utils.lua index fa42d7e25..dda5077d3 100644 --- a/xmake/core/base/utils.lua +++ b/xmake/core/base/utils.lua @@ -28,6 +28,7 @@ local string = require("base/string") local log = require("base/log") local io = require("base/io") local dump = require("base/dump") +local text = require("base/text") -- dump values function utils.dump(...) @@ -319,11 +320,11 @@ function utils.confirm(opt) end function utils.table(data, opt) - utils.printf(colors.table(data, opt)) + utils.printf(text.table(data, opt)) end function utils.vtable(data, opt) - utils.vprintf(colors.table(data, opt)) + utils.vprintf(text.table(data, opt)) end -- return module -- cgit v1.3.1