styleToSgr function

String styleToSgr(
  1. UvStyle style
)

Returns the SGR sequence for style.

Implementation

String styleToSgr(UvStyle style) {
  if (style.isZero) return UvAnsi.resetStyle;
  final simple = _simpleRgbStyleToSgr(style);
  if (simple != null) return simple;

  final codes = <String>[];

  final attrs = style.attrs;
  if ((attrs & Attr.bold) != 0) codes.add('1');
  if ((attrs & Attr.faint) != 0) codes.add('2');
  if ((attrs & Attr.italic) != 0) codes.add('3');
  if ((attrs & Attr.blink) != 0) codes.add('5');
  if ((attrs & Attr.rapidBlink) != 0) codes.add('6');
  if ((attrs & Attr.reverse) != 0) codes.add('7');
  if ((attrs & Attr.conceal) != 0) codes.add('8');
  if ((attrs & Attr.strikethrough) != 0) codes.add('9');

  switch (style.underline) {
    case UnderlineStyle.none:
      break;
    case UnderlineStyle.single:
      codes.add('4');
    case UnderlineStyle.double:
      codes.add('4:2');
    case UnderlineStyle.curly:
      codes.add('4:3');
    case UnderlineStyle.dotted:
      codes.add('4:4');
    case UnderlineStyle.dashed:
      codes.add('4:5');
  }

  final fg = _colorCode(style.fg, _ColorTarget.fg);
  if (fg != null) codes.add(fg);
  final bg = _colorCode(style.bg, _ColorTarget.bg);
  if (bg != null) codes.add(bg);
  final ul = _colorCode(style.underlineColor, _ColorTarget.underline);
  if (ul != null) codes.add(ul);

  if (codes.isEmpty) return UvAnsi.resetStyle;
  return '\x1b[${codes.join(';')}m';
}