mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-08-02 14:57:31 +03:00
use new generic tagged union parser
This commit is contained in:
@ -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,
|
||||
|
@ -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();
|
||||
|
Reference in New Issue
Block a user