From 6a650b7547db128679e51695148b7ddaf0ccf3c2 Mon Sep 17 00:00:00 2001 From: Isaiah Odhner Date: Tue, 9 May 2023 22:06:16 -0400 Subject: [PATCH] Fix missing last row when loading ANSI file not ending with a newline This fixes loading samples/4x4_font_template.ans, and I should be able to rebase my code for more efficient ANSI file saving, where I was also running into missing last rows. --- src/textual_paint/paint.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/textual_paint/paint.py b/src/textual_paint/paint.py index c3f6315..2918274 100755 --- a/src/textual_paint/paint.py +++ b/src/textual_paint/paint.py @@ -899,11 +899,8 @@ class AnsiArtDocument: elif char == '\n': x = 0 y += 1 - height = max(y, height) - if len(document.ch) <= y: - document.ch.append([]) - document.bg.append([]) - document.fg.append([]) + # Don't increase height until there's a character to put in the new row. + # This avoids an extra row if the file ends with a newline. elif char == '\t': x += 8 - (x % 8) elif char == '\b': @@ -916,6 +913,10 @@ class AnsiArtDocument: # ignore bell pass else: + while len(document.ch) <= y: + document.ch.append([]) + document.bg.append([]) + document.fg.append([]) while len(document.ch[y]) <= x: document.ch[y].append(' ') document.bg[y].append(default_bg) @@ -923,8 +924,9 @@ class AnsiArtDocument: document.ch[y][x] = char document.bg[y][x] = bg_color document.fg[y][x] = fg_color + width = max(x + 1, width) + height = max(y + 1, height) x += 1 - width = max(x, width) elif isinstance(instruction, stransi.SetColor) and instruction.color is not None: # Color (I'm not sure why instruction.color would be None, but it's typed as Optional[Color]) # (maybe just for initial state?) @@ -947,8 +949,8 @@ class AnsiArtDocument: y = instruction.move.x x = max(0, x) y = max(0, y) - width = max(x, width) - height = max(y, height) + width = max(x + 1, width) + height = max(y + 1, height) while len(document.ch) <= y: document.ch.append([]) document.bg.append([])