setAttributes method

void setAttributes(
  1. int x,
  2. int y, {
  3. String? char,
  4. int? fg,
  5. int? bg,
  6. int? modifiers,
})

Sets the cell attributes at (x, y). Does nothing if coordinates are out of bounds.

Implementation

void setAttributes(
  int x,
  int y, {
  String? char,
  int? fg,
  int? bg,
  int? modifiers,
}) {
  if (!isCellValid(x, y)) return;
  final charIdx = y * width + x;
  final idx = charIdx * 3;
  final bool isWide = char != null && char != '' && isWideGrapheme(char);
  if (char != null) {
    if (char != '') {
      if (characters[charIdx] == '') {
        if (x - 1 >= 0) {
          final prevIdx = charIdx - 1;
          if (isWideGrapheme(characters[prevIdx])) {
            characters[prevIdx] = ' ';
            attributes[prevIdx * 3 + 0] = 0;
            attributes[prevIdx * 3 + 1] = 0;
            attributes[prevIdx * 3 + 2] = Modifier.transparent;
          }
        }
      } else if (isWideGrapheme(characters[charIdx])) {
        if (x + 1 < width) {
          final nextIdx = charIdx + 1;
          if (characters[nextIdx] == '') {
            characters[nextIdx] = ' ';
            attributes[nextIdx * 3 + 0] = 0;
            attributes[nextIdx * 3 + 1] = 0;
            attributes[nextIdx * 3 + 2] = Modifier.transparent;
          }
        }
      }
    }
    characters[charIdx] = char;
    if (isWide && x + 1 < width) {
      final nextIdx = charIdx + 1;
      if (isWideGrapheme(characters[nextIdx]) && x + 2 < width) {
        final nextNextIdx = charIdx + 2;
        if (characters[nextNextIdx] == '') {
          characters[nextNextIdx] = ' ';
          attributes[nextNextIdx * 3 + 0] = 0;
          attributes[nextNextIdx * 3 + 1] = 0;
          attributes[nextNextIdx * 3 + 2] = Modifier.transparent;
        }
      }
      characters[nextIdx] = '';
    }
  }
  if (fg != null) attributes[idx + 0] = blendColor(fg, attributes[idx + 0]);
  if (bg != null) attributes[idx + 1] = blendColor(bg, attributes[idx + 1]);
  if (modifiers != null) attributes[idx + 2] = modifiers;
  if (isWide && x + 1 < width) {
    final nextAttrIdx = (charIdx + 1) * 3;
    if (fg != null) {
      attributes[nextAttrIdx + 0] = blendColor(
        fg,
        attributes[nextAttrIdx + 0],
      );
    }
    if (bg != null) {
      attributes[nextAttrIdx + 1] = blendColor(
        bg,
        attributes[nextAttrIdx + 1],
      );
    }
    if (modifiers != null) attributes[nextAttrIdx + 2] = modifiers;
  }
}