From ad36c481af2407a516280ac54e61f3fe03281e1d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 31 Jul 2024 09:06:17 +0530 Subject: [PATCH] Allow closing notifications with the kitten --- kittens/notify/main.go | 29 ++++++++++++++++++++++++++--- kittens/notify/main.py | 8 ++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/kittens/notify/main.go b/kittens/notify/main.go index a4fca0e34..c3af15d33 100644 --- a/kittens/notify/main.go +++ b/kittens/notify/main.go @@ -39,6 +39,7 @@ type parsed_data struct { expire_time time.Duration title, body, identifier string image_data []byte + initial_msg string } func (p *parsed_data) create_metadata() string { @@ -119,6 +120,9 @@ func (p *parsed_data) run_loop() (err error) { }) } lp.OnInitialize = func() (string, error) { + if p.initial_msg != "" { + return p.initial_msg, nil + } p.generate_chunks(func(x string) { lp.QueueWriteString(x) }) return "", nil } @@ -255,9 +259,6 @@ func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) { var p parsed_data p.opts = opts p.title = args[0] - if len(p.title) == 0 { - return 1, fmt.Errorf("Must specify a non-empty TITLE for the notification") - } if len(args) > 1 { p.body = strings.Join(args[1:], " ") } @@ -277,6 +278,28 @@ func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) { if !check_id_valid(opts.IconCacheId) { return 1, bad_ident(opts.IconCacheId) } + if len(p.title) == 0 { + if ident == "" { + return 1, fmt.Errorf("Must specify a non-empty TITLE for the notification or specify an identifier to close a notification.") + } + msg := ESC_CODE_PREFIX + "i=" + ident + ":p=close;" + ESC_CODE_SUFFIX + if opts.OnlyPrintEscapeCode { + _, err = os.Stdout.WriteString(msg) + } else if p.wait_till_closed { + p.initial_msg = msg + err = p.run_loop() + } else { + var term *tty.Term + if term, err = tty.OpenControllingTerm(); err != nil { + return 1, fmt.Errorf("Failed to open controlling terminal with error: %w", err) + } + if _, err = term.WriteString(msg); err != nil { + term.RestoreAndClose() + return 1, err + } + term.RestoreAndClose() + } + } if p.expire_time, err = parse_duration(opts.ExpireTime); err != nil { return 1, fmt.Errorf("Invalid expire time: %s with error: %w", opts.ExpireTime, err) } diff --git a/kittens/notify/main.py b/kittens/notify/main.py index 465e39599..5fcdb5cbe 100644 --- a/kittens/notify/main.py +++ b/kittens/notify/main.py @@ -83,6 +83,14 @@ if this kitten is being used inside a larger application, with --only-print-esca help_text = '''\ Send notifications to the user that are displayed to them via the desktop environment's notifications service. Works over SSH as well. + +To update an existing notification, specify the identifier of the notification +with the --identifier option. The value should be the same as the identifier specified for +the notification you wish to update. + +If no title is specified and an identifier is specified using the --identifier +option, then instead of creating a new notification, an existing notification +with the specified identifier is closed. ''' usage = 'TITLE [BODY ...]'