write method

void write(
  1. String text
)

Implementation

void write(String text) {
  final linesUp = totalRows - rowIndex;
  // Split text into lines to handle multi-line rendering properly
  final lines = text.split('\n');
  final height = lines.length;
  final buffer = StringBuffer();
  // 1. Move up to this animation's top target row
  buffer.write('\x1B[${linesUp}A');
  // 2. Clear and print each line, applying color explicitly to each line
  for (var i = 0; i < height; i++) {
    buffer.write('\x1B[2K\r'); // Clear current line & carriage return
    // Explicitly wrap each line with the animation's designated color
    buffer.write('$color${lines[i]}${AnsiColor.reset}');
    if (i < height - 1) {
      buffer.write('\n'); // Move down to next line for multi-line animations
    }
  }
  // 3. Move back down to the anchor position at the very bottom
  final moveDown = linesUp - (height - 1);
  if (moveDown > 0) {
    buffer.write('\x1B[${moveDown}B\r');
  } else {
    buffer.write('\r');
  }

  stderr.write(buffer.toString());
}