notifySequence function

String notifySequence(
  1. String title,
  2. String body, {
  3. required NotificationProtocol protocol,
  4. Multiplexer multiplexer = Multiplexer.none,
  5. String id = '1',
})

Returns a desktop notification of title and body in protocol, wrapped for multiplexer.

Empty for NotificationProtocol.none or with nothing to show. Control characters and the blanks around each text are removed; a line break in the body is kept by OSC 99 and is a space elsewhere, as a tab is. The title is cut to 256 bytes of UTF-8 and the body to 1024, between graphemes, ending with …. OSC 9 shows title: body; OSC 777 shows a body without a title as the title, and , for ;, which would end its field. OSC 99 sends the title and the body as two sequences under id (letters, digits and _-+., the rest dropped): a notification with the same id that is still shown is replaced. NotificationProtocol.bell is BEL alone, which a multiplexer rings on the terminal outside as its own bell, unwrapped.

Implementation

String notifySequence(
  String title,
  String body, {
  required NotificationProtocol protocol,
  Multiplexer multiplexer = Multiplexer.none,
  String id = '1',
}) {
  var t = _cut(_plain(title, lines: false), 256);
  var b = _cut(
    _plain(body, lines: protocol == NotificationProtocol.osc99),
    1024,
  );
  if (t.isEmpty && b.isEmpty) {
    return '';
  }
  final String s;
  switch (protocol) {
    case NotificationProtocol.none:
      return '';
    case NotificationProtocol.bell:
      return '\x07';
    case NotificationProtocol.osc9:
      s = '\x1b]9;${[t, b].where((p) => p.isNotEmpty).join(': ')}\x1b\\';
    case NotificationProtocol.osc777:
      if (t.isEmpty) {
        (t, b) = (b, '');
      }
      s =
          '\x1b]777;notify;${t.replaceAll(';', ',')};'
          '${b.replaceAll(';', ',')}\x1b\\';
    case NotificationProtocol.osc99:
      final i = 'i=${id.replaceAll(RegExp(r'[^\w+.-]'), '')}';
      final sb = StringBuffer();
      if (t.isNotEmpty) {
        sb.write('\x1b]99;$i${b.isEmpty ? '' : ':d=0'};$t\x1b\\');
      }
      if (b.isNotEmpty) {
        sb.write(
          b.contains('\n')
              ? '\x1b]99;$i:p=body:e=1;${base64.encode(utf8.encode(b))}\x1b\\'
              : '\x1b]99;$i:p=body;$b\x1b\\',
        );
      }
      s = sb.toString();
  }
  return passthroughSequence(s, multiplexer);
}