refactor: move formatters for input types to input module
This commit is contained in:
parent
572f74d8ba
commit
5d381dcc7b
2 changed files with 44 additions and 16 deletions
|
@ -1,8 +1,8 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const input = @import("input");
|
const input = @import("input");
|
||||||
|
|
||||||
key: input.Key = 0,
|
|
||||||
event: input.Event = input.event.press,
|
event: input.Event = input.event.press,
|
||||||
|
key: input.Key = 0,
|
||||||
modifiers: input.Mods = 0,
|
modifiers: input.Mods = 0,
|
||||||
|
|
||||||
pub fn eql(self: @This(), other: @This()) bool {
|
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 {
|
pub fn format(self: @This(), comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
|
||||||
if (self.event == input.event.press) try writer.writeAll("press ");
|
try writer.print("{}:{}{}", .{ input.event_fmt(self.event), input.mod_fmt(self.modifiers), input.key_fmt(self.key) });
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
const vaxis = @import("vaxis");
|
const vaxis = @import("vaxis");
|
||||||
|
|
||||||
const utf8Encode = @import("std").unicode.utf8Encode;
|
const utf8Encode = @import("std").unicode.utf8Encode;
|
||||||
|
const FormatOptions = @import("std").fmt.FormatOptions;
|
||||||
|
|
||||||
pub const key = vaxis.Key;
|
pub const key = vaxis.Key;
|
||||||
pub const Key = u21;
|
pub const Key = u21;
|
||||||
|
@ -166,3 +167,44 @@ pub const utils = struct {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub fn event_fmt(evt: Event) struct {
|
||||||
|
event: Event,
|
||||||
|
pub fn format(self: @This(), comptime _: []const u8, _: FormatOptions, writer: anytype) !void {
|
||||||
|
return switch (self.event) {
|
||||||
|
event.press => writer.writeAll("press"),
|
||||||
|
event.repeat => writer.writeAll("repeat"),
|
||||||
|
event.release => writer.writeAll("release"),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} {
|
||||||
|
return .{ .event = evt };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn key_fmt(key_: Key) struct {
|
||||||
|
key: Key,
|
||||||
|
pub fn format(self: @This(), comptime _: []const u8, _: FormatOptions, writer: anytype) !void {
|
||||||
|
var key_string = utils.key_id_string(self.key);
|
||||||
|
var buf: [6]u8 = undefined;
|
||||||
|
if (key_string.len == 0) {
|
||||||
|
const bytes = try ucs32_to_utf8(&[_]u32{self.key}, &buf);
|
||||||
|
key_string = buf[0..bytes];
|
||||||
|
}
|
||||||
|
try writer.writeAll(key_string);
|
||||||
|
}
|
||||||
|
} {
|
||||||
|
return .{ .key = key_ };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn mod_fmt(mods: Mods) struct {
|
||||||
|
modifiers: Mods,
|
||||||
|
pub fn format(self: @This(), comptime _: []const u8, _: FormatOptions, writer: anytype) !void {
|
||||||
|
const modset: ModSet = @bitCast(self.modifiers);
|
||||||
|
if (modset.super) try writer.writeAll("super+");
|
||||||
|
if (modset.ctrl) try writer.writeAll("ctrl+");
|
||||||
|
if (modset.alt) try writer.writeAll("alt+");
|
||||||
|
if (modset.shift) try writer.writeAll("shift+");
|
||||||
|
}
|
||||||
|
} {
|
||||||
|
return .{ .modifiers = mods };
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue