docs: def write_terminal(matrix, version, out, border=None):
      """\
      Function to write to a terminal which supports ANSI escape codes.
  
      :param matrix: The matrix to serialize.
      :param int version: The (Micro) QR code version.
      :param out: Filename or a file-like object supporting to write text.
      :param int border: Integer indicating the size of the quiet zone.
              If set to ``None`` (default), the recommended border size
              will be used (``4`` for QR Codes, ``2`` for a Micro QR Codes).
      """
      with writable(out, 'wt') as f:
          write = f.write
          colours = ['\033[{0}m'.format(i) for i in (7, 49)]
          for row in matrix_iter(matrix, version, scale=1, border=border):
              prev_bit = -1
              cnt = 0
              for bit in row:
                  if bit == prev_bit:
                      cnt += 1
                  else:
                      if cnt:
                          write(colours[prev_bit])
                          write('  ' * cnt)
                          write('\033[0m')  # reset color
                      prev_bit = bit
                      cnt = 1
              if cnt:
                  write(colours[prev_bit])
                  write('  ' * cnt)
                  write('\033[0m')  # reset color
              write('\n')