New Terminal Output:

root@f3f8d6319494:/app# cat > /app/termzip_ansi.py << 'EOF'
> import struct
> import sys
>
> def find_runs(changes):
>     """Find contiguous runs of changed columns."""
>     runs = []
>     if not changes:
>         return runs
>     run_start = changes[0]
>     run_end = changes[0]
>     for c in changes[1:]:
>         if c == run_end + 1:
>             run_end = c
>         else:
>             runs.append((run_start, run_end))
>             run_start = run_end = c
>     runs.append((run_start, run_end))
>     return runs
>
> def main():
>     if len(sys.argv) != 2:
>         sys.exit(1)
>
>     with open(sys.argv[1], 'rb') as f:
>         magic = f.read(4)
>         if magic != b'TZA1':
>             sys.exit(1)
>         w, h = struct.unpack('<HH', f.read(4))
>         n_frames, max_bytes = struct.unpack('<II', f.read(8))
>
>         # Read all frames
>         frames = [f.read(w * h) for _ in range(n_frames)]
>
>     out = []
>
>     # Frame 0: write non-space characters using absolute positioning
>     for r in range(h):
>         row = frames[0][r*w:(r+1)*w]
>         non_space = [c for c in range(w) if row[c] != 32]
>         runs = find_runs(non_space)
>         for start, end in runs:
>             out.append(f'\x1b[{r+1};{start+1}H')
>             out.append(row[start:end+1].decode('latin1'))
>     out.append('\x1e')
>
>     # Subsequent frames
>     for i in range(1, n_frames):
>         # Find changed rows and their changes
>         row_changes = {}
>         for r in range(h):
>             prev_row = frames[i-1][r*w:(r+1)*w]
>             curr_row = frames[i][r*w:(r+1)*w]
>             if prev_row != curr_row:
>                 changes = [c for c in range(w) if prev_row[c] != curr_row[c]]
>                 row_changes[r] = (changes, curr_row)
>
>         if not row_changes:
>             out.append('\x1e')
>             continue
>
>         for r, (changes, curr_row) in row_changes.items():
>             # Decide: EL+rewrite or partial updates?
>             # EL+rewrite: ESC[r;1H + ESC[2K + row
>             cost_erase = 3 + len(str(r+1)) + 1 + 1 + 4 + w
>
>             # Partial: runs of absolute positioning
>             runs = find_runs(changes)
>             cost_partial = sum(3 + len(str(r+1)) + 1 + len(str(start+1)) + 1 + (end-start+1) for start, end in runs)
>
>             if len(changes) > 40:  # Threshold for EL+rewrite
>                 out.append(f'\x1b[{r+1};1H')
>                 out.append('\x1b[2K')
>                 out.append(curr_row.decode('latin1'))
>             else:
>                 for start, end in runs:
>                     out.append(f'\x1b[{r+1};{start+1}H')
>                     out.append(curr_row[start:end+1].decode('latin1'))
>
>         out.append('\x1e')
>
>     sys.stdout.write(''.join(out))
>
> if __name__ == '__main__':
>     main()
> EOF
root@f3f8d6319494:/app#
