refactor: move formatters for input types to input module

This commit is contained in:
CJ van den Berg 2024-11-18 21:23:07 +01:00
parent 572f74d8ba
commit 5d381dcc7b
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 44 additions and 16 deletions

View file

@ -1,8 +1,8 @@
const std = @import("std");
const input = @import("input");
key: input.Key = 0,
event: input.Event = input.event.press,
key: input.Key = 0,
modifiers: input.Mods = 0,
pub fn eql(self: @This(), other: @This()) bool {
@ -10,19 +10,5 @@ pub fn eql(self: @This(), other: @This()) bool {
}
pub fn format(self: @This(), comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
if (self.event == input.event.press) try writer.writeAll("press ");
if (self.event == input.event.repeat) try writer.writeAll("repeat ");
if (self.event == input.event.release) try writer.writeAll("release ");
const mods: input.ModSet = @bitCast(self.modifiers);
if (mods.super) try writer.writeAll("super+");
if (mods.ctrl) try writer.writeAll("ctrl+");
if (mods.alt) try writer.writeAll("alt+");
if (mods.shift) try writer.writeAll("shift+");
var key_string = input.utils.key_id_string(self.key);
var buf: [6]u8 = undefined;
if (key_string.len == 0) {
const bytes = try input.ucs32_to_utf8(&[_]u32{self.key}, &buf);
key_string = buf[0..bytes];
}
try writer.writeAll(key_string);
try writer.print("{}:{}{}", .{ input.event_fmt(self.event), input.mod_fmt(self.modifiers), input.key_fmt(self.key) });
}