From 6a94864603a6c26b99b8834f91fc50ab1f92d637 Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Mon, 16 Sep 2024 22:40:23 -0500 Subject: [PATCH] use new generic tagged union parser --- src/apprt/gtk/Surface.zig | 25 ++++++++++- src/config/Config.zig | 90 --------------------------------------- 2 files changed, 23 insertions(+), 92 deletions(-) diff --git a/src/apprt/gtk/Surface.zig b/src/apprt/gtk/Surface.zig index 5ec41afe4..63aba95da 100644 --- a/src/apprt/gtk/Surface.zig +++ b/src/apprt/gtk/Surface.zig @@ -1154,11 +1154,32 @@ fn showContextMenu(self: *Surface, x: f32, y: f32) void { } pub fn bell(self: *Surface) !void { - if (self.app.config.@"bell-features".audio) { + if (self.app.config.@"bell-features".audio) audio: { const stream = switch (self.app.config.@"bell-audio") { .bell => c.gtk_media_file_new_for_resource("/com/mitchellh/ghostty/media/bell.oga"), .message => c.gtk_media_file_new_for_resource("/com/mitchellh/ghostty/media/message.oga"), - .custom => |filename| c.gtk_media_file_new_for_filename(filename), + .custom => |filename| stream: { + var arena = std.heap.ArenaAllocator.init(self.app.core_app.alloc); + defer arena.deinit(); + const alloc = arena.allocator(); + const pathname = pathname: { + if (std.fs.path.isAbsolute(filename)) + break :pathname try alloc.dupeZ(u8, filename) + else + break :pathname try std.fs.path.joinZ(alloc, &.{ + try internal_os.xdg.config( + alloc, + .{ .subdir = "ghostty/media" }, + ), + filename, + }); + }; + std.fs.accessAbsoluteZ(pathname, .{ .mode = .read_only }) catch { + log.warn("unable to find sound file: {s}", .{filename}); + break :audio; + }; + break :stream c.gtk_media_file_new_for_filename(pathname); + }, }; _ = c.g_signal_connect_data( stream, diff --git a/src/config/Config.zig b/src/config/Config.zig index 35f4425b6..97d96226d 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -4999,37 +4999,6 @@ pub const BellAudio = union(enum) { message: void, custom: [:0]const u8, - pub fn parseCLI(self: *BellAudio, alloc: std.mem.Allocator, input: ?[]const u8) !void { - const value = input orelse return error.ValueRequired; - const key_str = value[0 .. std.mem.indexOfScalar(u8, value, ':') orelse value.len]; - if (std.meta.stringToEnum(std.meta.Tag(BellAudio), std.mem.trim(u8, key_str, &std.ascii.whitespace))) |key| switch (key) { - .bell => { - self.* = .{ .bell = {} }; - }, - .message => { - self.* = .{ .message = {} }; - }, - .custom => { - if (key_str.len == value.len) return error.ValueRequired; - const rest = std.mem.trim(u8, value[key_str.len + 1 ..], &std.ascii.whitespace); - if (rest.len == 0) return error.ValueRequired; - if (std.fs.path.isAbsolute(rest)) - self.* = .{ - .custom = try alloc.dupeZ(u8, rest), - } - else - self.* = .{ - .custom = try std.fs.path.joinZ(alloc, &.{ - try internal_os.xdg.config(alloc, .{ .subdir = "ghostty/media" }), - rest, - }), - }; - }, - } else { - return error.ValueRequired; - } - } - pub fn formatEntry(self: BellAudio, formatter: anytype) !void { switch (self) { .bell, .message => try formatter.formatEntry([]const u8, @tagName(self)), @@ -5047,65 +5016,6 @@ pub const BellAudio = union(enum) { } } - test "parseCLI" { - var arena = std.heap.ArenaAllocator.init(std.testing.allocator); - defer arena.deinit(); - const alloc = arena.allocator(); - - { - var b: BellAudio = undefined; - try b.parseCLI(alloc, "bell"); - try std.testing.expect(b == .bell); - } - { - var b: BellAudio = undefined; - try b.parseCLI(alloc, "message"); - try std.testing.expect(b == .message); - } - { - var b: BellAudio = undefined; - try b.parseCLI(alloc, "message:"); - try std.testing.expect(b == .message); - } - { - var b: BellAudio = undefined; - try b.parseCLI(alloc, " message : "); - try std.testing.expect(b == .message); - } - { - var b: BellAudio = undefined; - try b.parseCLI(alloc, "custom:/tmp/bell.oga"); - try std.testing.expect(b == .custom); - try std.testing.expectEqualStrings("/tmp/bell.oga", b.custom); - } - { - var b: BellAudio = undefined; - try b.parseCLI(alloc, " custom : /tmp/bell.oga "); - try std.testing.expect(b == .custom); - try std.testing.expectEqualStrings("/tmp/bell.oga", b.custom); - } - { - var b: BellAudio = undefined; - try std.testing.expectError(error.ValueRequired, b.parseCLI(alloc, " custom : ")); - } - { - var b: BellAudio = undefined; - try std.testing.expectError(error.ValueRequired, b.parseCLI(alloc, " custom ")); - } - { - var b: BellAudio = undefined; - try std.testing.expectError(error.ValueRequired, b.parseCLI(alloc, " ")); - } - { - var b: BellAudio = undefined; - try std.testing.expectError(error.ValueRequired, b.parseCLI(alloc, "")); - } - { - var b: BellAudio = undefined; - try std.testing.expectError(error.ValueRequired, b.parseCLI(alloc, null)); - } - } - test "test formatEntry 1" { var buf = std.ArrayList(u8).init(std.testing.allocator); defer buf.deinit();