From 7023e1506b65eb6f36e83bb7ace288169cb232b6 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 29 Jul 2024 15:10:11 +0530 Subject: [PATCH] get icon by path loading working in the notify kitten --- kittens/notify/main.go | 27 +++++++++++++++++++++++++++ kittens/notify/main.py | 5 ++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/kittens/notify/main.go b/kittens/notify/main.go index aec64604f..920e8e091 100644 --- a/kittens/notify/main.go +++ b/kittens/notify/main.go @@ -4,6 +4,8 @@ import ( "bytes" "encoding/base64" "fmt" + "image" + "io" "os" "slices" "strconv" @@ -36,6 +38,7 @@ type parsed_data struct { wait_till_closed bool expire_time time.Duration title, body, identifier string + image_data []byte } func (p *parsed_data) create_metadata() string { @@ -94,6 +97,9 @@ func (p *parsed_data) generate_chunks(callback func(string)) { if p.body != "" { add_payload("body", p.body) } + if len(p.image_data) > 0 { + add_payload("icon", utils.UnsafeBytesToString(p.image_data)) + } write_chunk(";") } @@ -221,6 +227,24 @@ func parse_duration(x string) (ans time.Duration, err error) { return } +func (p *parsed_data) load_image_data() (err error) { + if p.opts.IconPath == "" { + return nil + } + f, err := os.Open(p.opts.IconPath) + if err != nil { + return err + } + defer f.Close() + _, imgfmt, err := image.DecodeConfig(f) + f.Seek(0, io.SeekStart) + if err == nil && imgfmt != "" && strings.Contains("jpeg jpg gif png", strings.ToLower(imgfmt)) { + p.image_data, err = io.ReadAll(f) + return + } + return fmt.Errorf("The icon must be in PNG, JPEG or GIF formats") +} + func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) { if len(args) == 0 { return 1, fmt.Errorf("Must specify a TITLE for the notification") @@ -254,6 +278,9 @@ func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) { return 1, fmt.Errorf("Invalid expire time: %s with error: %w", opts.ExpireTime, err) } p.wait_till_closed = opts.WaitTillClosed + if err = p.load_image_data(); err != nil { + return 1, fmt.Errorf("Failed to load image data from %s with error %w", opts.IconPath, err) + } if opts.OnlyPrintEscapeCode { p.generate_chunks(func(x string) { if err == nil { diff --git a/kittens/notify/main.py b/kittens/notify/main.py index b0e4c1497..465e39599 100644 --- a/kittens/notify/main.py +++ b/kittens/notify/main.py @@ -16,10 +16,9 @@ Standard names: {', '.join(sorted(standard_icon_names))} --icon-path -I -Path to an image file in PNG/JPEG/WEBP/GIF formats to use as the icon. If both +Path to an image file in PNG/JPEG/GIF formats to use as the icon. If both name and path are specified then first the name will be looked for and if not found -then the path will be used. Other image formats are supported if ImageMagick is -installed on the system. +then the path will be used. --app-name -a