this repo has no description
13
fork

Configure Feed

Select the types of activity you want to include in your feed.

tty: add test tty

This TestTTY is only valid on posix, for now. To get windows support we
will need to do a pipe or something on windows

+126 -6
+33 -5
src/Loop.zig
··· 31 31 switch (builtin.os.tag) { 32 32 .windows => {}, 33 33 else => { 34 - const handler: Tty.SignalHandler = .{ 35 - .context = self, 36 - .callback = Self.winsizeCallback, 37 - }; 38 - try Tty.notifyWinsize(handler); 34 + if (!builtin.is_test) { 35 + const handler: Tty.SignalHandler = .{ 36 + .context = self, 37 + .callback = Self.winsizeCallback, 38 + }; 39 + try Tty.notifyWinsize(handler); 40 + } 39 41 }, 40 42 } 41 43 } ··· 312 314 }, 313 315 } 314 316 } 317 + 318 + test Loop { 319 + if (builtin.os.tag == .windows) return; 320 + const Event = union(enum) { 321 + key_press: vaxis.Key, 322 + winsize: vaxis.Winsize, 323 + focus_in, 324 + foo: u8, 325 + }; 326 + 327 + var tty = try vaxis.Tty.init(); 328 + defer tty.deinit(); 329 + 330 + var vx = try vaxis.init(std.testing.allocator, .{}); 331 + defer vx.deinit(std.testing.allocator, tty.anyWriter()); 332 + 333 + var loop: vaxis.Loop(Event) = .{ .tty = &tty, .vaxis = &vx }; 334 + try loop.init(); 335 + 336 + try loop.start(); 337 + defer loop.stop(); 338 + 339 + // Optionally enter the alternate screen 340 + try vx.enterAltScreen(tty.anyWriter()); 341 + try vx.queryTerminal(tty.anyWriter(), 1 * std.time.ns_per_ms); 342 + }
+6 -1
src/main.zig
··· 34 34 /// The target TTY implementation 35 35 pub const Tty = switch (builtin.os.tag) { 36 36 .windows => @import("windows/Tty.zig"), 37 - else => @import("posix/Tty.zig"), 37 + else => if (builtin.is_test) 38 + @import("test/Tty.zig") 39 + else 40 + @import("posix/Tty.zig"), 38 41 }; 39 42 40 43 /// The size of the terminal screen ··· 88 91 _ = @import("gwidth.zig"); 89 92 _ = @import("queue.zig"); 90 93 _ = @import("widgets/TextInput.zig"); 94 + 95 + _ = @import("Loop.zig"); 91 96 }
+87
src/test/Tty.zig
··· 1 + //! Testable TTY 2 + const TestTTY = @This(); 3 + 4 + const std = @import("std"); 5 + const builtin = @import("builtin"); 6 + 7 + const posix = std.posix; 8 + 9 + const Winsize = @import("../main.zig").Winsize; 10 + 11 + const ctlseqs = @import("../ctlseqs.zig"); 12 + 13 + /// Used for API compat 14 + fd: posix.fd_t, 15 + pipe_read: posix.fd_t, 16 + pipe_write: posix.fd_t, 17 + writer: *std.ArrayList(u8), 18 + 19 + /// Initializes a TestTTY. 20 + pub fn init() !TestTTY { 21 + const list = try std.testing.allocator.create(std.ArrayList(u8)); 22 + list.* = std.ArrayList(u8).init(std.testing.allocator); 23 + const r, const w = try posix.pipe(); 24 + return .{ 25 + .fd = r, 26 + .pipe_read = r, 27 + .pipe_write = w, 28 + .writer = list, 29 + }; 30 + } 31 + 32 + pub fn deinit(self: TestTTY) void { 33 + std.posix.close(self.pipe_read); 34 + std.posix.close(self.pipe_write); 35 + self.writer.deinit(); 36 + std.testing.allocator.destroy(self.writer); 37 + } 38 + 39 + /// Write bytes to the tty 40 + pub fn write(self: *const TestTTY, bytes: []const u8) !usize { 41 + if (std.mem.eql(u8, bytes, ctlseqs.device_status_report)) { 42 + _ = posix.write(self.pipe_write, "\x1b") catch {}; 43 + } 44 + return self.writer.writer().write(bytes); 45 + } 46 + 47 + pub fn opaqueWrite(ptr: *const anyopaque, bytes: []const u8) !usize { 48 + const self: *const TestTTY = @ptrCast(@alignCast(ptr)); 49 + return self.write(bytes); 50 + } 51 + 52 + pub fn anyWriter(self: *const TestTTY) std.io.AnyWriter { 53 + return .{ 54 + .context = self, 55 + .writeFn = TestTTY.opaqueWrite, 56 + }; 57 + } 58 + 59 + pub fn read(self: *const TestTTY, buf: []u8) !usize { 60 + return posix.read(self.fd, buf); 61 + } 62 + 63 + pub fn opaqueRead(ptr: *const anyopaque, buf: []u8) !usize { 64 + const self: *const TestTTY = @ptrCast(@alignCast(ptr)); 65 + return posix.read(self.fd, buf); 66 + } 67 + 68 + pub fn anyReader(self: *const TestTTY) std.io.AnyReader { 69 + return .{ 70 + .context = self, 71 + .readFn = TestTTY.opaqueRead, 72 + }; 73 + } 74 + 75 + /// Get the window size from the kernel 76 + pub fn getWinsize(_: posix.fd_t) !Winsize { 77 + return .{ 78 + .rows = 40, 79 + .cols = 80, 80 + .x_pixel = 40 * 8, 81 + .y_pixel = 40 * 8 * 2, 82 + }; 83 + } 84 + 85 + pub fn bufferedWriter(self: *const TestTTY) std.io.BufferedWriter(4096, std.io.AnyWriter) { 86 + return std.io.bufferedWriter(self.anyWriter()); 87 + }