writeAlert static method
CappConsole
writeAlert(
- dynamic message, [
- CappColors color = CappColors.info,
- bool space = false
writeAlert method is used to print a message as a boxed alert with a
colored background and a severity icon, similar to alert components in
UI frameworks (e.g. writeAlert('Oops!', CappColors.error) for a
"danger" style alert).
Implementation
static CappConsole writeAlert(
dynamic message, [
CappColors color = CappColors.info,
bool space = false,
]) {
if (color == CappColors.off) return CappConsole(message, color);
var bg = _alertBackground(color);
var fg = _alertForeground(color);
const bold = '\x1B[1m';
const reset = '\x1B[0m';
var style = bg.isEmpty ? '' : '$bg$fg$bold';
// Wrap long messages so the box never grows wider than the terminal.
var prefix = '${_alertIcon(color)} ';
var maxContentWidth = _terminalWidth() - prefix.length - 4;
var wrapped = _wrapText('$message', maxContentWidth < 20 ? 20 : maxContentWidth);
var indent = ' ' * prefix.length;
var lines = [
'$prefix${wrapped.first}',
for (var i = 1; i < wrapped.length; i++) '$indent${wrapped[i]}',
];
var width = lines.fold<int>(0, (w, l) => l.length > w ? l.length : w);
var top = '╭${'─' * (width + 2)}╮';
var bottom = '╰${'─' * (width + 2)}╯';
if (space) stdout.writeln();
stdout.writeln('$style$top$reset');
for (var line in lines) {
stdout.writeln('$style│ ${line.padRight(width)} │$reset');
}
stdout.writeln('$style$bottom$reset');
if (space) stdout.writeln();
return CappConsole(message, color);
}