Tabby: A Keyboard Event Library

Tabby

Tabby is a Keyboard Event handling library, primarily for use in terminal user interfaces (TUI’s). Currently only Linux is supported. Tabby supports the Kitty Keybaord Protocol.

Example

const std = @import("std");
const ztui = @import("ztui");
const keycodes = ztui.event_reader.keycodes;


pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const allocator = gpa.allocator();
    defer {
        const status = gpa.deinit();
        if (status == .leak) @panic("Memory Leak Occured");
    }

    try ztui.terminal.enableRawMode();
    defer ztui.terminal.disableRawMode() catch {}; // UH-OH

    while (true) {
        const res = try ztui.event_reader.read(allocator);
        defer res.deinit();
        for (res.items) |event| {
            std.debug.print("{any}", .{event});
            switch (event.code) {
                .Char => |char| {
                    // Quit on `q`
                    if (char == 113) {
                        return;
                    }
                },
                else => {},
            }
        }
    }
}
6 Likes

I think that would be clearer like this:

if (char == 'q') {
    return; // quit
}
4 Likes

I think an enum would be even better this way it’s easier to adapt to another platform ?
btw @Calder-Ty great stuff :slight_smile:

2 Likes

Nice project!

In what sense is only Linux supported? Do you mean that only Kitty is supported? It’s available for other Unix platforms, at least macOS and some of the BSDs.

Does it use non-POSIX syscalls or something like that?

1 Like

I would like us to use “EVIOCGKEY” with ioctl in Zig.

Thanks for the feedback all!

@gonzo

Yes that would be clearer. I forgot to update it. I Think i had run into some strange typing/programming error that caused me to go to use 113 directly, but i should change it back.

@mnemnion

In what sense is only Linux supported? Do you mean that only Kitty is supported? > It’s available for other Unix platforms, at least macOS and some of the BSDs.

Does it use non-POSIX syscalls or something like that?

That is a good point. I’ve only tested it on LInux, but I imagine it would work on most unix flavors. The big problem right now is that I have reading /dev/tty hardcoded in a few spots so Windows is right out. As for Sys-calls, it does make use of poll, but using the std posix layer.

1 Like

Also note that you can’t poll /dev/tty on macos. You have to use a regular blocking read. I found this out the hard way the other day trying to get libvaxis working on macos in a non-blocking application (flow).

3 Likes