padVisibleRight function

String padVisibleRight(
  1. String text,
  2. int width
)

Pads styled text to width based on visible character length.

Unlike padRight, this accounts for ANSI escape codes when calculating how much padding is needed.

Example:

padVisibleRight('\x1B[32mHi\x1B[0m', 5); // '\x1B[32mHi\x1B[0m   '

Implementation

String padVisibleRight(String text, int width) {
  final visible = visibleLength(text);
  if (visible >= width) return text;
  return text + ' ' * (width - visible);
}