summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOpportunity <[email protected]>2020-01-16 17:25:48 +0800
committerOpportunity <[email protected]>2020-01-16 17:25:48 +0800
commit9cca6d065a6d8756876f6877725eeaa1304a6ef7 (patch)
tree888a589d7099b0d383b036c0fdba77189a50fdc6
parentd5f108398a0b638f60aaab8089831bb9e67023c4 (diff)
fix style
-rw-r--r--xmake/core/base/cli.lua22
-rw-r--r--xmake/core/base/option.lua4
-rw-r--r--xmake/core/base/text.lua53
-rw-r--r--xmake/core/base/utils.lua5
4 files changed, 49 insertions, 35 deletions
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