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 <tim@timculverhouse.com>
This commit is contained in:
Tim Culverhouse 2024-01-22 10:40:30 -06:00
parent 64557bf9ab
commit f1a9e21a98
3 changed files with 11 additions and 7 deletions

View file

@ -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 => {

View file

@ -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..] };

View file

@ -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{