mirror of
https://github.com/nmap/nmap.git
synced 2025-12-25 00:49:01 +00:00
Insert an empty row table when tab.nextrow is called and there is
nothing in the current row yet. This allows using #t or ipairs to get the number of rows that have been filled by the user. t.rows is the index number of the next row that will be filled in, or the one that is currently being filled in if something has already been entered. t.rows == #t + 1 means that we've finished with the previous row, but we don't want to count a new (blank) row until we've started filling something in.
This commit is contained in:
@@ -40,8 +40,6 @@ end
|
||||
-- @param c The column position at which to add the item.
|
||||
function add(t, c, v)
|
||||
assert(t)
|
||||
assert(v)
|
||||
assert(t['rows'])
|
||||
assert(type(v) == "string")
|
||||
|
||||
-- add a new row if one doesn't exist
|
||||
@@ -73,6 +71,7 @@ end
|
||||
function nextrow(t)
|
||||
assert(t)
|
||||
assert(t['rows'])
|
||||
t[t['rows']] = t[t['rows']] or {}
|
||||
t['rows'] = t['rows'] + 1
|
||||
end
|
||||
|
||||
@@ -83,16 +82,13 @@ end
|
||||
-- @param t The table.
|
||||
function dump(t)
|
||||
assert(t)
|
||||
assert(t['rows'])
|
||||
|
||||
local column_width = {}
|
||||
local num_columns = 0
|
||||
local buf = strbuf.new()
|
||||
local len
|
||||
|
||||
-- find widest element in each column
|
||||
for i = 1, t['rows'] do
|
||||
local row = t[i]
|
||||
for i, row in ipairs(t) do
|
||||
for x, elem in pairs(row) do
|
||||
local elem_width = string.len(elem)
|
||||
if not column_width[x] or elem_width > column_width[x] then
|
||||
@@ -105,10 +101,10 @@ function dump(t)
|
||||
end
|
||||
|
||||
-- build buf with padding so all column elements line up
|
||||
for i = 1, t['rows'] do
|
||||
for i, row in ipairs(t) do
|
||||
local text_row = {}
|
||||
for x = 1, num_columns do
|
||||
local elem = t[i][x] or ""
|
||||
local elem = row[x] or ""
|
||||
text_row[#text_row + 1] = elem .. string.rep(" ", column_width[x] - #elem)
|
||||
end
|
||||
buf = buf .. table.concat(text_row, " ") .. "\n"
|
||||
|
||||
Reference in New Issue
Block a user