Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,20 @@ prof = Profile(

### What's new

#### Desktop notifications for `OSC 9`, Kitty `OSC 99`, and `OSC 777` (#31)

{class}`~libvt.terminal.Terminal` parses iTerm2 `OSC 9` (filtering ConEmu
`9;1`–`9;12` subcommands), chunked Kitty `OSC 99` (`d=0`/`d=1`, `e=0`/`e=1`
Base64, `p=title`/`p=body`/`p=?`, `u=0..2`), and rxvt `OSC 777 ; notify`
sequences into sanitized {class}`~libvt.events.Notification` events
({class}`~libvt.events.NotificationSource`,
{class}`~libvt.events.NotificationUrgency`, included in
{type}`~libvt.events.KnownEvent`), gated on
{attr}`~libvt.config.Policy.allow_notifications`,
{attr}`~libvt.config.Policy.allow_notification_queries`,
{attr}`~libvt.config.Limits.max_pending_notifications`, and
{attr}`~libvt.config.Limits.max_notification_bytes`.

#### Stateful `XTSMGRAPHICS` color and geometry registers (#28)

{class}`~libvt.terminal.Terminal` implements stateful `XTSMGRAPHICS`
Expand Down
2 changes: 1 addition & 1 deletion docs/api/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

.. py:type:: KnownEvent
:module: libvt.events
:canonical: Bell | TitleChanged | WorkingDirectoryChanged | ClipboardWrite | ClipboardReadRequested | PromptMark | ShellCommandReported | ShellPropertyChanged | ProgramStatusChanged | RenderHoldChanged | ColorSchemeQueried | InBandResize | SixelGraphic | KittyGraphic | ScreenErased | Passthrough | UnhandledSequence
:canonical: Bell | TitleChanged | WorkingDirectoryChanged | ClipboardWrite | ClipboardReadRequested | PromptMark | ShellCommandReported | ShellPropertyChanged | ProgramStatusChanged | RenderHoldChanged | ColorSchemeQueried | InBandResize | SixelGraphic | KittyGraphic | ScreenErased | Passthrough | Notification | UnhandledSequence

Closed union of built-in core :class:`~libvt.events.Event` subclasses for
callers opting into static ``typing.assert_never`` exhaustiveness checks.
Expand Down
11 changes: 6 additions & 5 deletions docs/topics/protocols-and-input.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ to {meth}`~libvt.terminal.Terminal.drain_replies` according to active
{class}`~libvt.config.Profile` and {class}`~libvt.config.Policy` settings (or
stay silent when {attr}`~libvt.config.Policy.emit_replies` is `False` on passive
observer streams). Notifications (`OSC 0`/`1`/`2` and `ESC k` title,
`OSC 7` / `9;9` / `633;P` working directory, `OSC 52`
`OSC 7` / `9;9` / `633;P` working directory, `OSC 9` / Kitty `OSC 99` /
`OSC 777;notify` desktop {class}`~libvt.events.Notification` events, `OSC 52`
{class}`~libvt.events.ClipboardWrite` events and
{class}`~libvt.events.ClipboardReadRequested` queries under
{attr}`~libvt.config.ClipboardReadPolicy.EMIT_EVENT` answered via
Expand All @@ -83,10 +84,10 @@ observer streams). Notifications (`OSC 0`/`1`/`2` and `ESC k` title,
to {attr}`Cell.hyperlink <libvt.screen.Cell.hyperlink>`:

```python
>>> term = libvt.Terminal(rows=4, cols=20)
>>> term.feed(b"\x1b[?1;1;0S")
>>> term.drain_replies()
b'\x1b[?1;0;256S'
>>> term = libvt.Terminal(rows=4, cols=20, record_events=True)
>>> term.feed(b"\x1b[?1;1;0S\x1b]99;i=build:u=2:p=title;CI\x1b\\")
>>> term.drain_replies(), term.drain_events()[0].title
(b'\x1b[?1;0;256S', 'CI')
```

## Keyboard, mouse, and focus encoding
Expand Down
17 changes: 17 additions & 0 deletions src/libvt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,13 @@ class Policy:
track_graphics : bool
Track ``SIXEL`` and Kitty ``APC G`` image metadata and grid
placements (default ``True``).
allow_notifications : bool
Emit :class:`~libvt.events.Notification` events on ``OSC 9``, Kitty
``OSC 99``, and ``OSC 777;notify`` sequences (default ``True``).
allow_notification_queries : bool
Answer Kitty ``OSC 99`` ``p=?`` capability queries when
``emit_replies`` and ``allow_notifications`` are ``True`` (default
``False``).
"""

emit_replies: bool = True
Expand All @@ -405,6 +412,8 @@ class Policy:
osc7_hosts: frozenset[str] | None = None
strict: bool = False
track_graphics: bool = True
allow_notifications: bool = True
allow_notification_queries: bool = False


@dataclass(frozen=True, slots=True, kw_only=True)
Expand Down Expand Up @@ -453,6 +462,12 @@ class Limits:
max_image_dimension : int
Maximum pixel width or height accepted for ``SIXEL`` or Kitty images
(default ``10_000``).
max_pending_notifications : int
Maximum concurrent incomplete Kitty ``OSC 99`` (``d=0``) chunked
notifications retained (default ``16``).
max_notification_bytes : int
Maximum cumulative UTF-8 text bytes retained per notification payload
(default ``16_384``).
"""

max_grapheme_codepoints: int = 32
Expand All @@ -471,6 +486,8 @@ class Limits:
max_images: int = 256
max_placements: int = 1_024
max_image_dimension: int = 10_000
max_pending_notifications: int = 16
max_notification_bytes: int = 16_384


#: Default modern VT500 / ``Ghostty``-aligned terminal profile.
Expand Down
87 changes: 87 additions & 0 deletions src/libvt/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,89 @@ class Passthrough(Event):
data: bytes


class NotificationSource(enum.StrEnum):
"""Protocol origin of a :class:`Notification` event.

References
----------
- `Kitty Desktop Notification Protocol (OSC 99)
<https://sw.kovidgoyal.net/kitty/desktop-notifications/>`__

Examples
--------
>>> from libvt.events import NotificationSource
>>> NotificationSource.OSC_99.value
'osc_99'
"""

OSC_9 = "osc_9"
OSC_99 = "osc_99"
OSC_777 = "osc_777"


class NotificationUrgency(enum.StrEnum):
"""Desktop notification urgency level (Kitty ``OSC 99`` ``u=0/1/2``).

References
----------
- `Kitty Desktop Notification Protocol (OSC 99) — Urgency
<https://sw.kovidgoyal.net/kitty/desktop-notifications/#urgency>`__

Examples
--------
>>> from libvt.events import NotificationUrgency
>>> NotificationUrgency.HIGH.value
'high'
"""

LOW = "low"
NORMAL = "normal"
HIGH = "high"


@dataclass(frozen=True, slots=True, kw_only=True, match_args=False)
class Notification(Event):
r"""Emitted when ``OSC 9``, Kitty ``OSC 99``, or ``OSC 777`` notifies.

Attributes
----------
source : NotificationSource
Originating escape protocol (``OSC_9``, ``OSC_99``, or ``OSC_777``).
title : str
Sanitized notification title (``""`` for ``OSC 9`` body-only messages).
body : str
Sanitized notification body text.
notification_id : str
Optional notification identifier from Kitty ``OSC 99`` ``i=`` metadata.
urgency : NotificationUrgency
Urgency level (``LOW``, ``NORMAL``, or ``HIGH``; defaults to ``NORMAL``).

References
----------
- `Kitty Desktop Notification Protocol (OSC 99)
<https://sw.kovidgoyal.net/kitty/desktop-notifications/>`__
- `ghostty-org/ghostty kitty_desktop_notification.zig (v1.4.0)
<https://github.com/ghostty-org/ghostty/blob/v1.4.0/src/terminal/osc/parsers/kitty_desktop_notification.zig#L101-L218>`__
- `ghostty-org/ghostty rxvt_extension.zig (v1.4.0)
<https://github.com/ghostty-org/ghostty/blob/v1.4.0/src/terminal/osc/parsers/rxvt_extension.zig#L8-L45>`__

Examples
--------
>>> from libvt.terminal import Terminal
>>> term = Terminal(record_events=True)
>>> term.feed(b"\x1b]777;notify;Build;All green\x07")
>>> ev = term.drain_events()[0]
>>> (ev.source.value, ev.title, ev.body)
('osc_777', 'Build', 'All green')
"""

source: NotificationSource
title: str = ""
body: str = ""
notification_id: str = ""
urgency: NotificationUrgency = NotificationUrgency.NORMAL


@dataclass(frozen=True, slots=True, kw_only=True, match_args=False)
class UnhandledSequence(Event):
"""Recorded when an unrecognized control sequence arrives and ``strict=False``.
Expand Down Expand Up @@ -498,6 +581,7 @@ class UnhandledSequence(Event):
| KittyGraphic
| ScreenErased
| Passthrough
| Notification
| UnhandledSequence
)

Expand All @@ -513,6 +597,9 @@ class UnhandledSequence(Event):
"KittyCommand",
"KittyGraphic",
"KnownEvent",
"Notification",
"NotificationSource",
"NotificationUrgency",
"Passthrough",
"ProgramStatusChanged",
"PromptKind",
Expand Down
Loading
Loading