Previous response had warnings:
WARNINGS: - Extra text detected before JSON object
- Extra text detected after JSON object

New Terminal Output:

root@d7299c7f5982:/app# cat > /app/termzip_ansi.py << 'ENDOFFILE'
 if prev_frame[row_start + c] != frame[row_start + c]:
                    move_to(r, c)
                    while c < width and prev_frame[row_start + c] != frame[row_start + c]:
                        output.append(frame[row_start + c])
                        c += 1
                        cur_col += 1
                        if cur_col >= width:
                            cur_col = 0
                            cur_row += 1
                else:
                    c += 1
            # After processing row, check if rest should be cleared
            if cur_col < width and cur_row == r:
                all_spaces = True
                for cc in range(cur_col, width):
                    if frame[row_start + cc] != ord(' '):
                        all_spaces = False
                        break
                if all_spaces:
                    output.extend(b'\x1b[0K')
                    cur_col = width

        output.append(0x1e)
        prev_frame = frame

    sys.stdout.buffer.write(output)

if __name__ == '__main__':
    main()
ENDOFFILE
> #!/usr/bin/env python3
> import struct
> import sys
>
> def read_tza(filename):
>     with open(filename, 'rb') as f:
>         magic = f.read(4)
>         if magic != b'TZA1':
>             raise ValueError(f'Invalid magic: {magic}')
>         width, height = struct.unpack('<HH', f.read(4))
>         n_frames, max_bytes = struct.unpack('<II', f.read(8))
>         frames = []
>         for _ in range(n_frames):
>             frame_data = f.read(width * height)
>             frames.append(frame_data)
>         return width, height, n_frames, max_bytes, frames
>
> def main():
>     if len(sys.argv) != 2:
>         sys.stderr.write(f'Usage: {sys.argv[0]} <frames.tza>\n')
>         sys.exit(1)
>
>     filename = sys.argv[1]
>     width, height, n_frames, max_bytes, frames = read_tza(filename)
>
>     prev_frame = b' ' * (width * height)
>     output = bytearray()
>
>     cur_row, cur_col = 0, 0
>
>     def move_to(row, col):
>         nonlocal cur_row, cur_col, output
>         if row == cur_row and col == cur_col:
>             return
>
>         if row > cur_row:
>             output.extend(b'\n' * (row - cur_row))
>             cur_row = row
>             cur_col = 0
>         elif row < cur_row:
>             output.extend(f'\x1b[{cur_row - row}A'.encode())
>             cur_row = row
>
>         if col != cur_col:
>             if col == 0:
>                 output.extend(b'\r')
>             elif col < cur_col:
>                 output.extend(b'\b' * (cur_col - col))
>             else:
>                 dist = col - cur_col
>                 cost_cuf = len(f'\x1b[{dist}C')
>                 cost_cr_cuf = 1 + len(f'\x1b[{col}C')
>                 if cost_cr_cuf < cost_cuf:
>                     output.extend(b'\r')
>                     output.extend(f'\x1b[{col}C'.encode())
>                 else:
>                     output.extend(f'\x1b[{dist}C'.encode())
>             cur_col = col
>
>     for frame in frames:
>         for r in range(height):
>             row_start = r * width
>             c = 0
>             while c < width:
>                 if prev_frame[row_start + c] != frame[row_start + c]:
>                     move_to(r, c)
>                     while c < width and prev_frame[row_start + c] != frame[row_start + c]:
>                         output.append(frame[row_start + c])
>                         c += 1
>                         cur_col += 1
>                         if cur_col >= width:
>                             cur_col = 0
>                             cur_row += 1
>                 else:
>                     c += 1
>             # After processing row, check if rest should be cleared
>             if cur_col < width and cur_row == r:
>                 all_spaces = True
>                 for cc in range(cur_col, width):
>                     if frame[row_start + cc] != ord(' '):
>                         all_spaces = False
>                         break
>                 if all_spaces:
>                     output.extend(b'\x1b[0K')
>                     cur_col = width
>
>         output.append(0x1e)
>         prev_frame = frame
>
>     sys.stdout.buffer.write(output)
>
> if __name__ == '__main__':
>     main()
> ENDOFFILE
root@d7299c7f5982:/app# python3 /app/termzip_ansi.py /app/frames.tza | wc -c
359180
root@d7299c7f5982:/app#
