use new generic tagged union parser

This commit is contained in:
Jeffrey C. Ollie
2024-09-16 22:40:23 -05:00
parent 510b96d872
commit 6a94864603
2 changed files with 23 additions and 92 deletions

View File

@ -1154,11 +1154,32 @@ fn showContextMenu(self: *Surface, x: f32, y: f32) void {
} }
pub fn bell(self: *Surface) !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") { const stream = switch (self.app.config.@"bell-audio") {
.bell => c.gtk_media_file_new_for_resource("/com/mitchellh/ghostty/media/bell.oga"), .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"), .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( _ = c.g_signal_connect_data(
stream, stream,

View File

@ -4999,37 +4999,6 @@ pub const BellAudio = union(enum) {
message: void, message: void,
custom: [:0]const u8, 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 { pub fn formatEntry(self: BellAudio, formatter: anytype) !void {
switch (self) { switch (self) {
.bell, .message => try formatter.formatEntry([]const u8, @tagName(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" { test "test formatEntry 1" {
var buf = std.ArrayList(u8).init(std.testing.allocator); var buf = std.ArrayList(u8).init(std.testing.allocator);
defer buf.deinit(); defer buf.deinit();