GhosttyTerminalResolvedStyle.fromFormattedStyle constructor

GhosttyTerminalResolvedStyle.fromFormattedStyle({
  1. required GhosttyTerminalStyle style,
  2. required List<Color> palette,
  3. required Color defaultForeground,
  4. required Color defaultBackground,
})

Resolves a formatter-driven style using a Flutter palette and defaults.

Implementation

factory GhosttyTerminalResolvedStyle.fromFormattedStyle({
  required GhosttyTerminalStyle style,
  required List<Color> palette,
  required Color defaultForeground,
  required Color defaultBackground,
}) {
  Color resolveStyleColor({
    required GhosttyTerminalColor? color,
    required Color fallback,
    required List<Color> palette,
  }) {
    if (color == null) {
      return fallback;
    }
    final rgb = color.rgb;
    if (rgb != null) {
      return Color.fromARGB(
        0xFF,
        (rgb >> 16) & 0xFF,
        (rgb >> 8) & 0xFF,
        rgb & 0xFF,
      );
    }
    final index = color.paletteIndex;
    if (index == null) {
      return fallback;
    }
    if (index >= 0 && index < palette.length) {
      return palette[index];
    }
    return GhosttyTerminalPalette.xterm.resolve(
      GhosttyTerminalColor.palette(index),
      fallback: fallback,
    );
  }

  final hasExplicitForeground = style.foreground != null;
  final hasExplicitBackground = style.background != null;
  final hasExplicitUnderlineColor = style.underlineColor != null;
  const transparent = Color(0x00000000);

  var foreground = hasExplicitForeground
      ? resolveStyleColor(
          color: style.foreground,
          fallback: defaultForeground,
          palette: palette,
        )
      : transparent;
  var background = hasExplicitBackground
      ? resolveStyleColor(
          color: style.background,
          fallback: defaultBackground,
          palette: palette,
        )
      : transparent;
  if (style.inverse) {
    final swappedForeground = background;
    background = hasExplicitForeground
        ? foreground
        : (foreground == transparent ? defaultForeground : foreground);
    foreground = hasExplicitBackground
        ? swappedForeground
        : (swappedForeground == transparent
              ? defaultBackground
              : swappedForeground);
  }
  if (style.invisible) {
    foreground = background == transparent ? defaultBackground : background;
  }
  if (style.faint) {
    foreground = foreground.withValues(alpha: 0.72);
  }
  if (foreground == transparent) {
    foreground = defaultForeground;
  }

  return GhosttyTerminalResolvedStyle(
    foreground: foreground,
    background: background,
    underlineColor: resolveStyleColor(
      color: style.underlineColor,
      fallback: hasExplicitUnderlineColor ? defaultForeground : transparent,
      palette: palette,
    ),
    foregroundToken: style.foreground,
    backgroundToken: style.background,
    underlineColorToken: style.underlineColor,
    bold: style.bold,
    italic: style.italic,
    blink: style.blink,
    overline: style.overline,
    strikethrough: style.strikethrough,
    underline:
        style.underline ?? GhosttySgrUnderline.GHOSTTY_SGR_UNDERLINE_NONE,
    inverse: style.inverse,
    invisible: style.invisible,
    faint: style.faint,
    hasExplicitUnderlineColor: hasExplicitUnderlineColor,
    hasExplicitForeground: hasExplicitForeground,
    hasExplicitBackground: hasExplicitBackground,
  );
}