style method
Applies an Ansi compliant modifier to the string and returns it.
Note: Returns the string unmodified if Ansi.status is set to AnsiOutput.disabled.
Usage:
var message = 'The ' + 'grass'.style(Ansi.green + Ansi.italic);
// message: 'The \u001B[3;32mgrass\u001B[0m';
message = message.style(Ansi.yellow, method: Replace.first);
// message: 'The \u001B[33mgrass\u001B[0m;
message = message.style(Ansi.bold);
// message = '\u001B[1mThe \u001B[33mgrass\u001B[0m;
Implementation
String style(
Ansi modifier, {
Replace method = Replace.starting,
}) =>
isEmpty
? this
: switch ((Ansi.status, method)) {
(AnsiOutput.disabled, _) => this,
(AnsiOutput.enabled, Replace.first) =>
replaceFirst(matchAnsi, modifier.code),
(AnsiOutput.enabled, Replace.starting) => startsWith(escLeft)
? replaceFirst(
matchAnsi,
modifier.code,
)
: (modifier.code + this)._appendReset,
(AnsiOutput.enabled, Replace.none) =>
(modifier.code + this)._appendReset,
(AnsiOutput.enabled, Replace.clearPrevious) =>
modifier == Ansi.reset
? clearStyle()
: modifier.code + clearStyle() + resetSeq,
};