vis-tables - Vis plugin for writing tables
ssh://anon@thyssentishman.com/vis-tables
Log | Files | Refs | Feed | Contribute | README | LICENSE

Commit: bda4155187fe508b0f00fa57b19a90d38754486f
Parent: 87bd4918f3af9027fec24a26d59d01ff5f690124
Author: Johannes Thyssen Tishman
Date:   Sun, 27 Aug 2023 18:13:47 +0000

Fix creation of first column when empty

Diffstat:
M init.lua | 28 ++++++++++++++++++----------

1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/init.lua b/init.lua
@@ -91,17 +91,20 @@ local function gettable(ln, lc, lines)
 			end
 			::continue::
 		end
-		tbl.colw[c] = max
+		tbl.colw[c] = (max == 0 and 1 or max)
 	end
+	if tbl.ncols == 0 then tbl.colw[1] = 1 end
 
 	return tbl
 end
 
 local function newrow(colw)
 	local row = M.csep
-	for i = 1, #colw do
+	local i = 1
+	repeat
 		row = row .. (' '):rep(colw[i] + (M.npad * 2)) .. M.csep
-	end
+		i = i + 1
+	until i > #colw
 	return row .. '\n'
 end
 
@@ -109,13 +112,18 @@ local function printtable(tbl, lines)
 	local rowfmt = M.csep
 	local divfmt = M.csep
 
-	for i = 1, tbl.ncols do
-		rowfmt = string.format('%s%s%%-%ss%s%s', 
-			rowfmt, (' '):rep(M.npad), tbl.colw[i], 
-			(' '):rep(M.npad), M.csep)
-		divfmt = string.format('%s%s%s', 
-			divfmt, M.rsep:rep(M.npad * 2 + tbl.colw[i]),
-			i < #tbl.colw and M.xint or M.csep)
+	if tbl.ncols == 0 then
+		rowfmt = string.format('%s%s %s%s', 
+			rowfmt, (' '):rep(M.npad), (' '):rep(M.npad), M.csep)
+	else
+		for i = 1, tbl.ncols do
+			rowfmt = string.format('%s%s%%-%ss%s%s', 
+				rowfmt, (' '):rep(M.npad), tbl.colw[i], 
+				(' '):rep(M.npad), M.csep)
+			divfmt = string.format('%s%s%s', 
+				divfmt, M.rsep:rep(M.npad * 2 + tbl.colw[i]),
+				i < #tbl.colw and M.xint or M.csep)
+		end
 	end
 
 	for i = tbl.start, tbl.finish do