From 1ee0fae3d11f40c65c96339e9d077e7b00c8ea8a Mon Sep 17 00:00:00 2001 From: david Date: Thu, 30 Dec 2010 21:08:19 +0000 Subject: [PATCH] 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. --- nselib/tab.lua | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/nselib/tab.lua b/nselib/tab.lua index 9d10b2baa..6f831a8b7 100644 --- a/nselib/tab.lua +++ b/nselib/tab.lua @@ -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"