diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-04 23:35:20 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-05 01:21:29 -0300 |
| commit | dbdd21d8ebfaaedb5675a457aa385b97450237f5 (patch) | |
| tree | c501bfef83afb769f6156cb422ce0d56dd24c822 /src/vaxis_input.zig | |
| parent | e38820811c306e7575e87d3fe9a84c7a1503f7d9 (diff) | |
| download | pardes-dbdd21d8ebfaaedb5675a457aa385b97450237f5.tar.gz pardes-dbdd21d8ebfaaedb5675a457aa385b97450237f5.zip | |
vaxis_input: the core's cell styles as vaxis draws them (style, color, the underline mapped by its name) and a pasted key's text (pasteBytes) are vaxis_input's, the tui's copies gone; the gui's test-mode stdin takes a paste's keys through the same pasteBytes, which also knows the keypad's Enter (its own copy did not)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/vaxis_input.zig')
| -rw-r--r-- | src/vaxis_input.zig | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/src/vaxis_input.zig b/src/vaxis_input.zig index 59846d0f..5cb3c83e 100644 --- a/src/vaxis_input.zig +++ b/src/vaxis_input.zig @@ -1,7 +1,8 @@ //! 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. +//! 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"); @@ -67,6 +68,44 @@ pub fn keyEvent(key: vaxis.Key) pardes.Event { } }; } +/// 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)); |
