From dfab5367665d27c0618cefdf312d07093a2cc320 Mon Sep 17 00:00:00 2001
From: ankraft <an.kraft@gmail.com>
Date: Mon, 17 Feb 2025 16:58:37 +0100
Subject: [PATCH] Corrected width of multiline cells

---
 .gitignore   | 3 +++
 gridTable.py | 7 +++++--
 2 files changed, 8 insertions(+), 2 deletions(-)
 create mode 100644 .gitignore

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..8f2857a
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+out
+.vscode
+__pycache__
diff --git a/gridTable.py b/gridTable.py
index 91ab0c7..c859bef 100644
--- a/gridTable.py
+++ b/gridTable.py
@@ -94,7 +94,7 @@ def formatGridTable(lines: list[str]) -> list[str]:
 		return lines
 
 	# Get column widths from first separator line
-	colWidths = [len(col.strip()) for col in lines[0].split('+')[1:-1]]
+	colWidths = [0] * (lines[0].count('+') - 1)
 	result = []
 
 	# Adjust column widths if any cell is longer
@@ -105,7 +105,10 @@ def formatGridTable(lines: list[str]) -> list[str]:
 			for i, cell in enumerate(rowCells):
 				if i >= len(colWidths):
 					continue
-				cellWidth = len(cell.strip())
+				# Calculate maximum width of each line in the cell. Lines could be multilines, so we need to split them.
+				cellLines = cell.strip().split('\\\n')
+				cellWidth = max(len(line.strip()) if line != colspanMarker else 0
+								for line in cellLines)
 				if cellWidth > colWidths[i]:
 					colWidths[i] = cellWidth
 
-- 
GitLab