tryParse static method

Command? tryParse(
  1. Sequence sequence
)

Parses an escape sequence into a command.

Implementation

static Command? tryParse(Sequence sequence) {
  if (sequence is! EscapeSequence) {
    if (sequence is Literal) {
      return Print(sequence.value);
    }
    return null;
  }
  if (sequence.prefix == '?') {
    return switch (sequence.finalChars) {
      'h' => switch (sequence.parameters) {
          [25] => SetCursorVisibility.visible,
          [1049] => AlternateScreenBuffer.enter,
          [2026] => SynchronizedUpdate.start,
          _ => null,
        },
      'l' => switch (sequence.parameters) {
          [25] => SetCursorVisibility.hidden,
          [1049] => AlternateScreenBuffer.leave,
          [2026] => SynchronizedUpdate.end,
          _ => null,
        },
      _ => null,
    };
  }
  return switch (sequence.finalChars) {
    'H' => switch (sequence.parameters) {
        [final int row, final int column] => MoveCursorTo(row, column),
        [final int row] => MoveCursorTo(row),
        [] => MoveCursorTo(),
        _ => null,
      },
    'G' => switch (sequence.parameters) {
        [final int column] => MoveCursorToColumn(column),
        [] => MoveCursorToColumn(),
        _ => null,
      },
    'J' => switch (sequence.parameters) {
        [0] || [] => ClearScreen.all,
        [1] => ClearScreen.fromCursorDown,
        [2] => ClearScreen.fromCursorUp,
        [3] => ClearScreen.allAndScrollback,
        _ => null,
      },
    'K' => switch (sequence.parameters) {
        [0] || [] => ClearScreen.toEndOfLine,
        [2] => ClearScreen.currentLine,
        _ => null,
      },
    'm' => switch (sequence.parameters) {
        [38, 5, final int color] => SetForegroundColor256(color),
        [48, 5, final int color] => SetBackgroundColor256(color),
        [final int color] => SetColor16(color),
        _ => null,
      },
    _ => null,
  };
}