mod(read_input): read user input from tty
This commit is contained in:
47
src/app.zig
47
src/app.zig
@@ -1,6 +1,7 @@
|
||||
//! Application type for TUI-applications
|
||||
const std = @import("std");
|
||||
const terminal = @import("terminal.zig");
|
||||
const code_point = terminal.code_point;
|
||||
const mergeTaggedUnions = @import("event.zig").mergeTaggedUnions;
|
||||
const isTaggedUnion = @import("event.zig").isTaggedUnion;
|
||||
|
||||
@@ -70,20 +71,42 @@ pub fn App(comptime E: type) type {
|
||||
const size = terminal.getTerminalSize();
|
||||
this.postEvent(.{ .resize = size });
|
||||
// read input in loop
|
||||
const buf: [256]u8 = undefined;
|
||||
_ = buf;
|
||||
var buf: [256]u8 = undefined;
|
||||
while (!this.quit) {
|
||||
std.time.sleep(5 * std.time.ns_per_s);
|
||||
break;
|
||||
// try terminal.read(buf[0..]);
|
||||
// TODO: send corresponding events with key_presses
|
||||
// -> create corresponding event
|
||||
// -> handle key inputs (modifier, op codes, etc.)
|
||||
// -> I could take inspiration from `libvaxis` for this
|
||||
// FIXME: here is a race-condition -> i.e. there could be events
|
||||
// in the queue, but they will not be executed because the main
|
||||
// loop will close!
|
||||
const read_bytes = try terminal.read(buf[0..]);
|
||||
// escape key presses
|
||||
if (buf[0] == 0x1b and read_bytes > 1) {
|
||||
switch (buf[1]) {
|
||||
// TODO: parse corresponding codes
|
||||
else => {},
|
||||
}
|
||||
} else {
|
||||
const b = buf[0];
|
||||
const key: terminal.Key = switch (b) {
|
||||
0x00 => .{ .cp = '@', .mod = .{ .ctrl = true } },
|
||||
0x08 => .{ .cp = terminal.Key.backspace },
|
||||
0x09 => .{ .cp = terminal.Key.tab },
|
||||
0x0a, 0x0d => .{ .cp = terminal.Key.enter },
|
||||
0x01...0x07, 0x0b...0x0c, 0x0e...0x1a => .{ .cp = b + 0x60, .mod = .{ .ctrl = true } },
|
||||
0x1b => escape: {
|
||||
std.debug.assert(read_bytes == 1);
|
||||
break :escape .{ .cp = terminal.Key.escape };
|
||||
},
|
||||
0x7f => .{ .cp = terminal.Key.backspace },
|
||||
else => {
|
||||
var iter = code_point.Iterator{ .bytes = buf[0..read_bytes] };
|
||||
while (iter.next()) |cp| {
|
||||
this.postEvent(.{ .key = .{ .cp = cp.code } });
|
||||
}
|
||||
continue;
|
||||
},
|
||||
};
|
||||
this.postEvent(.{ .key = key });
|
||||
}
|
||||
}
|
||||
// FIXME: here is a race-condition -> i.e. there could be events in
|
||||
// the queue, but they will not be executed because the main loop
|
||||
// will close!
|
||||
this.postEvent(.quit);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user