//! A key as vaxis parses it from a terminal's bytes, as the core's Event: //! the one map for every shell that reads a terminal (tty, gui's stdin, //! esp32p4). Three copies had drifted; the gui's had lost the F-keys, //! Insert and the keypad. And the other way, a cell's style as vaxis draws //! it. const std = @import("std"); const vaxis = @import("vaxis"); const pardes = @import("pardes.zig"); /// The effective codepoint the way vaxis's own `Key.matches` sees it: a /// single-character `text` wins, the terminal having resolved shift; /// otherwise the shifted codepoint. pub fn effCp(key: vaxis.Key) u21 { if (key.text) |t| { const view = std.unicode.Utf8View.init(t) catch return key.codepoint; var it = view.iterator(); if (it.nextCodepoint()) |c| { if (it.nextCodepoint() == null) return c; } } return key.shifted_codepoint orelse key.codepoint; } /// vaxis's functional-key codepoints as the core's; the ASCII ones /// (enter, tab, escape, backspace) already coincide. pub fn mapKey(cp: u21) u21 { return switch (cp) { vaxis.Key.kp_enter => pardes.Key.enter, vaxis.Key.up => pardes.Key.up, vaxis.Key.down => pardes.Key.down, vaxis.Key.left => pardes.Key.left, vaxis.Key.right => pardes.Key.right, vaxis.Key.home => pardes.Key.home, vaxis.Key.end => pardes.Key.end, vaxis.Key.page_up => pardes.Key.page_up, vaxis.Key.page_down => pardes.Key.page_down, vaxis.Key.delete => pardes.Key.delete, vaxis.Key.insert => pardes.Key.insert, vaxis.Key.print_screen => pardes.Key.print_screen, vaxis.Key.scroll_lock => pardes.Key.scroll_lock, vaxis.Key.pause => pardes.Key.pause, vaxis.Key.menu => pardes.Key.menu, vaxis.Key.f1...vaxis.Key.f25 => pardes.Key.f1 + (cp - vaxis.Key.f1), // The keypad's own keys, which the kitty keyboard protocol tells // apart, do what the keys they are named for do. vaxis.Key.kp_left => pardes.Key.left, vaxis.Key.kp_right => pardes.Key.right, vaxis.Key.kp_up => pardes.Key.up, vaxis.Key.kp_down => pardes.Key.down, vaxis.Key.kp_page_up => pardes.Key.page_up, vaxis.Key.kp_page_down => pardes.Key.page_down, vaxis.Key.kp_home => pardes.Key.home, vaxis.Key.kp_end => pardes.Key.end, vaxis.Key.kp_insert => pardes.Key.insert, vaxis.Key.kp_delete => pardes.Key.delete, else => cp, }; } /// The key as the core's event, its text the key's own. pub fn keyEvent(key: vaxis.Key) pardes.Event { return .{ .key = .{ .cp = mapKey(effCp(key)), .text = key.text orelse "", .ctrl = key.mods.ctrl, .alt = key.mods.alt, .shift = key.mods.shift, } }; } /// What a key typed inside a bracketed paste adds to it: its text, else a /// tab, or a newline for Enter (the keypad's too) and Ctrl-J. pub fn pasteBytes(key: vaxis.Key) []const u8 { const text = key.text orelse ""; if (text.len > 0) return text; const cp = mapKey(effCp(key)); if (cp == pardes.Key.tab) return "\t"; if (cp == pardes.Key.enter or (key.mods.ctrl and cp == 'j')) return "\n"; return ""; } /// A cell's style as vaxis draws it. pub fn style(s: pardes.CellStyle) vaxis.Style { return .{ .fg = color(s.fg), .bg = color(s.bg), .bold = s.bold, .dim = s.dim, .italic = s.italic, .blink = s.blink, .reverse = s.reverse, .invisible = s.invisible, .strikethrough = s.strikethrough, // The two underline enums name the same six styles. .ul_style = switch (s.ul) { inline else => |ul| @field(@FieldType(vaxis.Style, "ul_style"), @tagName(ul)), }, }; } pub fn color(c: pardes.Color) vaxis.Color { return switch (c) { .default => .default, .index => |i| .{ .index = i }, .rgb => |rgb| .{ .rgb = rgb }, }; } test "F-keys, Insert and the keypad map to the core's keys" { try std.testing.expectEqual(pardes.Key.f1 + 4, mapKey(vaxis.Key.f5)); try std.testing.expectEqual(pardes.Key.f1 + 24, mapKey(vaxis.Key.f25)); try std.testing.expectEqual(pardes.Key.insert, mapKey(vaxis.Key.insert)); try std.testing.expectEqual(pardes.Key.up, mapKey(vaxis.Key.kp_up)); try std.testing.expectEqual(pardes.Key.enter, keyEvent(.{ .codepoint = vaxis.Key.kp_enter }).key.cp); try std.testing.expectEqual(@as(u21, 'a'), mapKey('a')); } test "an event queue emptied in one batch lets every writer it held go on (build.zig's patch)" { const io = std.testing.io; const Q = vaxis.Queue(u8, 2); var q: Q = .init(io); try q.push(1); try q.push(2); const Writer = struct { fn run(queue: *Q, byte: u8) !void { try queue.push(byte); } }; // Two writers waiting on the full queue, as a pane's pty reader and // the frame ack's wake do under `yes`. var a = try io.concurrent(Writer.run, .{ &q, 3 }); defer a.cancel(io) catch {}; var b = try io.concurrent(Writer.run, .{ &q, 4 }); defer b.cancel(io) catch {}; try io.sleep(.fromMilliseconds(20), .awake); // The loop takes both at once: one pop from full, then one more. try q.lock(); _ = q.drain().?; _ = q.drain().?; q.unlock(); var got: usize = 0; var waited: usize = 0; while (got < 2 and waited < 200) : (waited += 1) { if (try q.tryPop() != null) got += 1 else try io.sleep(.fromMilliseconds(5), .awake); } try std.testing.expectEqual(@as(usize, 2), got); }