mirror of
https://github.com/kovidgoyal/kitty
synced 2026-06-06 09:15:57 +02:00
Add a new escape code to allow terminal programs to trigger desktop notifications
Fixes #1474
This commit is contained in:
@@ -3,10 +3,15 @@
|
||||
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
|
||||
|
||||
import time
|
||||
from base64 import standard_b64encode
|
||||
from binascii import hexlify
|
||||
from functools import partial
|
||||
|
||||
from kitty.fast_data_types import CURSOR_BLOCK, parse_bytes, parse_bytes_dump
|
||||
from kitty.notify import (
|
||||
NotificationCommand, handle_notification_cmd, notification_activated,
|
||||
reset_registry
|
||||
)
|
||||
|
||||
from . import BaseTest
|
||||
|
||||
@@ -195,6 +200,7 @@ class TestParser(BaseTest):
|
||||
pb('\t\033[b', ('screen_tab',), ('screen_repeat_character', 1))
|
||||
self.ae(str(s.line(0)), '\t')
|
||||
s.reset()
|
||||
b']]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
|
||||
|
||||
def test_osc_codes(self):
|
||||
s = self.create_screen()
|
||||
@@ -216,6 +222,74 @@ class TestParser(BaseTest):
|
||||
self.ae(c.titlebuf, '')
|
||||
pb('\033]110\x07', ('set_dynamic_color', 110, ''))
|
||||
self.ae(c.colorbuf, '')
|
||||
c.clear()
|
||||
pb('\033]9;\x07', ('desktop_notify', 9, ''))
|
||||
pb('\033]9;test it\x07', ('desktop_notify', 9, 'test it'))
|
||||
pb('\033]99;moo=foo;test it\x07', ('desktop_notify', 99, 'moo=foo;test it'))
|
||||
self.ae(c.notifications, [(9, ''), (9, 'test it'), (99, 'moo=foo;test it')])
|
||||
|
||||
def test_desktop_notify(self):
|
||||
reset_registry()
|
||||
notifications = []
|
||||
activations = []
|
||||
prev_cmd = NotificationCommand()
|
||||
|
||||
def reset():
|
||||
nonlocal prev_cmd
|
||||
reset_registry()
|
||||
del notifications[:]
|
||||
del activations[:]
|
||||
prev_cmd = NotificationCommand()
|
||||
|
||||
def notify(title, body, identifier):
|
||||
notifications.append((title, body, identifier))
|
||||
|
||||
def h(raw_data, osc_code=99, window_id=1):
|
||||
nonlocal prev_cmd
|
||||
x = handle_notification_cmd(osc_code, raw_data, window_id, prev_cmd, notify)
|
||||
if x is not None and osc_code == 99:
|
||||
prev_cmd = x
|
||||
|
||||
def activated(identifier, window_id, focus, report):
|
||||
activations.append((identifier, window_id, focus, report))
|
||||
|
||||
h('test it', osc_code=9)
|
||||
self.ae(notifications, [('test it', '', 'i0')])
|
||||
notification_activated(notifications[-1][-1], activated)
|
||||
self.ae(activations, [('0', 1, True, False)])
|
||||
reset()
|
||||
|
||||
h('d=0:i=x;title')
|
||||
h('d=1:i=x:p=body;body')
|
||||
self.ae(notifications, [('title', 'body', 'i0')])
|
||||
notification_activated(notifications[-1][-1], activated)
|
||||
self.ae(activations, [('x', 1, True, False)])
|
||||
reset()
|
||||
|
||||
h('i=x:p=body:a=-focus;body')
|
||||
self.ae(notifications, [('body', '', 'i0')])
|
||||
notification_activated(notifications[-1][-1], activated)
|
||||
self.ae(activations, [])
|
||||
reset()
|
||||
|
||||
h('i=x:e=1;' + standard_b64encode(b'title').decode('ascii'))
|
||||
self.ae(notifications, [('title', '', 'i0')])
|
||||
notification_activated(notifications[-1][-1], activated)
|
||||
self.ae(activations, [('x', 1, True, False)])
|
||||
reset()
|
||||
|
||||
h('d=0:i=x:a=-report;title')
|
||||
h('d=1:i=x:a=report;body')
|
||||
self.ae(notifications, [('titlebody', '', 'i0')])
|
||||
notification_activated(notifications[-1][-1], activated)
|
||||
self.ae(activations, [('x', 1, True, True)])
|
||||
reset()
|
||||
|
||||
h(';title')
|
||||
self.ae(notifications, [('title', '', 'i0')])
|
||||
notification_activated(notifications[-1][-1], activated)
|
||||
self.ae(activations, [('0', 1, True, False)])
|
||||
reset()
|
||||
|
||||
def test_dcs_codes(self):
|
||||
s = self.create_screen()
|
||||
|
||||
Reference in New Issue
Block a user