From 689199251acc66096d868951c43995e2a0a0332f Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Wed, 15 Nov 2023 11:09:27 -0600 Subject: [PATCH] core: use arrays instead of WriteReq for desktop notifications --- src/Surface.zig | 14 ++------------ src/apprt/surface.zig | 4 ++-- src/termio/Exec.zig | 28 ++++++++-------------------- 3 files changed, 12 insertions(+), 34 deletions(-) diff --git a/src/Surface.zig b/src/Surface.zig index ce3fd8394..4336ac00a 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -722,18 +722,8 @@ pub fn handleMessage(self: *Surface, msg: Message) !void { return; } - const title: [:0]const u8 = switch (notification.title) { - .small => |v| v.data[0..v.len :0], - // Stream handler only sends small messages - else => unreachable, - }; - - const body: [:0]const u8 = switch (notification.body) { - .small => |v| v.data[0..v.len :0], - // Stream handler only sends small messages - else => unreachable, - }; - + const title = std.mem.sliceTo(¬ification.title, 0); + const body = std.mem.sliceTo(¬ification.body, 0); try self.showDesktopNotification(title, body); }, } diff --git a/src/apprt/surface.zig b/src/apprt/surface.zig index cf54a7bb3..8e8fe01e0 100644 --- a/src/apprt/surface.zig +++ b/src/apprt/surface.zig @@ -49,10 +49,10 @@ pub const Message = union(enum) { /// Show a desktop notification. desktop_notification: struct { /// Desktop notification title. - title: WriteReq, + title: [63:0]u8, /// Desktop notification body. - body: WriteReq, + body: [255:0]u8, }, }; diff --git a/src/termio/Exec.zig b/src/termio/Exec.zig index 85f2ce8ce..7bbc95226 100644 --- a/src/termio/Exec.zig +++ b/src/termio/Exec.zig @@ -2394,28 +2394,16 @@ const StreamHandler = struct { title: []const u8, body: []const u8, ) !void { - // Subtract one to leave room for the sentinel - const max_length = apprt.surface.Message.WriteReq.Small.Max - 1; - if (title.len >= max_length or body.len >= max_length) { - log.warn("requested notification is too long", .{}); - return; - } + var message = apprt.surface.Message{ .desktop_notification = undefined }; - var req_title: apprt.surface.Message.WriteReq.Small = .{}; - @memcpy(req_title.data[0..title.len], title); - req_title.data[title.len] = 0; - req_title.len = @intCast(title.len); + const title_len = @min(title.len, message.desktop_notification.title.len); + @memcpy(message.desktop_notification.title[0..title_len], title[0..title_len]); + message.desktop_notification.title[title_len] = 0; - var req_body: apprt.surface.Message.WriteReq.Small = .{}; - @memcpy(req_body.data[0..body.len], body); - req_body.data[body.len] = 0; - req_body.len = @intCast(body.len); + const body_len = @min(body.len, message.desktop_notification.body.len); + @memcpy(message.desktop_notification.body[0..body_len], body[0..body_len]); + message.desktop_notification.body[body_len] = 0; - _ = self.ev.surface_mailbox.push(.{ - .desktop_notification = .{ - .title = .{ .small = req_title }, - .body = .{ .small = req_body }, - }, - }, .{ .forever = {} }); + _ = self.ev.surface_mailbox.push(message, .{ .forever = {} }); } };