From f1a9e21a98e299788f77856cb2ea2a3bc9ce4caa Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Mon, 22 Jan 2024 10:40:30 -0600 Subject: [PATCH] tty: use grapheme cache in the tty run method We only need the grapheme cache when we are parsing input Signed-off-by: Tim Culverhouse --- src/Tty.zig | 13 +++++++++++-- src/parser.zig | 1 - src/vaxis.zig | 4 ---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Tty.zig b/src/Tty.zig index 6244b33..2e84f02 100644 --- a/src/Tty.zig +++ b/src/Tty.zig @@ -6,6 +6,7 @@ const Vaxis = vaxis.Vaxis; const Event = @import("event.zig").Event; const parser = @import("parser.zig"); const Key = vaxis.Key; +const GraphemeCache = @import("GraphemeCache.zig"); const log = std.log.scoped(.tty); @@ -112,6 +113,9 @@ pub fn run( }; try WinchHandler.init(vx, self.fd); + // initialize a grapheme cache + var cache: GraphemeCache = .{}; + // Set up fds for polling var pollfds: [2]std.os.pollfd = .{ .{ .fd = self.fd, .events = std.os.POLL.IN, .revents = undefined }, @@ -132,7 +136,7 @@ pub fn run( var start: usize = 0; while (start < n) { const result = try parser.parse(buf[start..n]); - start = result.n; + start += result.n; // TODO: if we get 0 byte read, copy the remaining bytes to the // beginning of the buffer and read mmore? this should only happen // if we are in the middle of a grapheme at and filled our @@ -143,7 +147,12 @@ pub fn run( switch (event) { .key_press => |key| { if (@hasField(EventType, "key_press")) { - vx.postEvent(.{ .key_press = key }); + // HACK: yuck. there has to be a better way + var mut_key = key; + if (key.text) |text| { + mut_key.text = cache.put(text); + } + vx.postEvent(.{ .key_press = mut_key }); } }, .focus_in => { diff --git a/src/parser.zig b/src/parser.zig index 06f4ed3..6016a5b 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -79,7 +79,6 @@ pub fn parse(input: []const u8) !Result { state = .escape; continue; }, - // 0x20...0x7E => .{ .codepoint = b }, 0x7F => .{ .codepoint = Key.backspace }, else => blk: { var iter: CodePointIterator = .{ .bytes = input[i..] }; diff --git a/src/vaxis.zig b/src/vaxis.zig index 68bc82b..253f173 100644 --- a/src/vaxis.zig +++ b/src/vaxis.zig @@ -9,7 +9,6 @@ const Screen = @import("Screen.zig"); const Window = @import("Window.zig"); const Options = @import("Options.zig"); const Style = @import("cell.zig").Style; -const GraphemeCache = @import("GraphemeCache.zig"); /// Vaxis is the entrypoint for a Vaxis application. The provided type T should /// be a tagged union which contains all of the events the application will @@ -47,9 +46,6 @@ pub fn Vaxis(comptime T: type) type { renders: usize = 0, render_dur: i128 = 0, - // grapheme cache - g_cache: GraphemeCache = .{}, - /// Initialize Vaxis with runtime options pub fn init(_: Options) !Self { return Self{