mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-07-26 10:41:58 +00:00
input: add Binding.Action.format to convert action to string
This commit is contained in:
@@ -303,6 +303,41 @@ pub const Action = union(enum) {
|
||||
return Error.InvalidAction;
|
||||
}
|
||||
|
||||
/// Implements the formatter for the fmt package. This encodes the
|
||||
/// action back into the format used by parse.
|
||||
pub fn format(
|
||||
self: Action,
|
||||
comptime layout: []const u8,
|
||||
opts: std.fmt.FormatOptions,
|
||||
writer: anytype,
|
||||
) !void {
|
||||
_ = layout;
|
||||
_ = opts;
|
||||
|
||||
switch (self) {
|
||||
inline else => |value| {
|
||||
const Value = @TypeOf(value);
|
||||
const value_info = @typeInfo(Value);
|
||||
|
||||
// All actions start with the tag.
|
||||
try writer.print("{s}", .{@tagName(self)});
|
||||
|
||||
// Write the value depending on the type
|
||||
switch (Value) {
|
||||
void => {},
|
||||
[]const u8 => try writer.print(":{s}", .{value}),
|
||||
else => switch (value_info) {
|
||||
.Enum => try writer.print(":{s}", .{@tagName(value)}),
|
||||
.Float => try writer.print(":{d}", .{value}),
|
||||
.Int => try writer.print(":{d}", .{value}),
|
||||
.Struct => try writer.print("{} (not configurable)", .{value}),
|
||||
else => @compileError("unhandled type: " ++ @typeName(Value)),
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a hash code that can be used to uniquely identify this
|
||||
/// action.
|
||||
pub fn hash(self: Action) u64 {
|
||||
|
||||
Reference in New Issue
Block a user