printTextBox static method

Future<bool> printTextBox(
  1. String text, {
  2. int lineWidth = 48,
  3. List<String> boxChars = const ['-', '|', '+'],
})

Print a text box with border.

Creates a box around text for emphasis.

  • text: The text to put in the box
  • lineWidth: Width of the box in characters
  • boxChars: Characters to use for the box (horizontal, vertical, corners)

Returns true if printing was successful.

Implementation

static Future<bool> printTextBox(
  String text, {
  int lineWidth = 48,
  List<String> boxChars = const ['-', '|', '+'],
}) async {
  final horizontalChar = boxChars[0];
  final verticalChar = boxChars[1];
  final cornerChar = boxChars.length > 2 ? boxChars[2] : '+';

  // Inner width of the box (accounting for vertical borders)
  final innerWidth = lineWidth - 4;

  // Wrap the text to fit inside the box
  final lines = TextFormatter.wrapText(text, innerWidth);

  // Create the box
  final List<String> boxLines = [];

  // Top border
  boxLines.add('$cornerChar${horizontalChar * (lineWidth - 2)}$cornerChar');

  // Content with side borders
  for (String line in lines) {
    final centeredText = TextFormatter.centerText(line, innerWidth);
    boxLines.add('$verticalChar  $centeredText  $verticalChar');
  }

  // Bottom border
  boxLines.add('$cornerChar${horizontalChar * (lineWidth - 2)}$cornerChar');

  // Print the box
  return await printLines(boxLines);
}