alignText method

String alignText({
  1. required int width,
  2. TextAlignment alignment = TextAlignment.left,
})

Implementation

String alignText(
    {required int width, TextAlignment alignment = TextAlignment.left}) {
  // We can't use the padLeft() and padRight() methods here, since they
  // don't account for ANSI escape sequences.
  switch (alignment) {
    case TextAlignment.center:
      // By using ceil _and_ floor, we ensure that the target width is reached
      // even if the padding is uneven (e.g. a single character wrapped in a 4
      // character width should be wrapped as '··c·' rather than '··c··').
      final leftPadding = ' ' * ((width - displayWidth) / 2).ceil();
      final rightPadding = ' ' * ((width - displayWidth) / 2).floor();
      return leftPadding + this + rightPadding;
    case TextAlignment.right:
      final padding = ' ' * (width - displayWidth);
      return padding + this;
    case TextAlignment.left:
    default:
      final padding = ' ' * (width - displayWidth);
      return this + padding;
  }
}