a modern tui library written in zig

actually make tests compile

authored by pluie.me and committed by rockorager.dev 50d7b413 70fe0a4a

Changed files
+26 -68
src
+1 -1
src/Loop.zig
··· 390 390 foo: u8, 391 391 }; 392 392 393 - var tty = try vaxis.Tty.init(); 393 + var tty = try vaxis.Tty.init(&.{}); 394 394 defer tty.deinit(); 395 395 396 396 var vx = try vaxis.init(std.testing.allocator, .{});
+1 -1
src/main.zig
··· 67 67 68 68 /// Resets the terminal state using the global tty instance. Use this only to recover during a panic 69 69 pub fn recover() void { 70 - if (tty.global_tty) |gty| { 70 + if (tty.global_tty) |*gty| { 71 71 const reset: []const u8 = ctlseqs.csi_u_pop ++ 72 72 ctlseqs.mouse_reset ++ 73 73 ctlseqs.bp_reset ++
+24 -66
src/tty.zig
··· 33 33 /// The file descriptor of the tty 34 34 fd: posix.fd_t, 35 35 36 + reader: std.fs.File.Reader, 37 + 36 38 /// File.Writer for efficient buffered writing 37 39 writer: std.fs.File.Writer, 38 40 ··· 74 76 const self: PosixTty = .{ 75 77 .fd = fd, 76 78 .termios = termios, 77 - .writer = std.fs.File.Writer.initStreaming(file, buffer), 79 + .writer = .initStreaming(file, buffer), 78 80 }; 79 81 80 82 global_tty = self; ··· 106 108 posix.sigaction(posix.SIG.WINCH, &act, null); 107 109 } 108 110 109 - /// Write bytes to the tty 110 - pub fn write(self: *PosixTty, bytes: []const u8) !usize { 111 - return self.writer.interface.write(bytes); 112 - } 113 - 114 - pub fn opaqueWrite(ptr: *const anyopaque, bytes: []const u8) !usize { 115 - const self: *PosixTty = @ptrCast(@alignCast(ptr)); 116 - return self.writer.interface.write(bytes); 117 - } 118 - 119 - pub fn anyWriter(self: *const PosixTty) *std.io.Writer { 120 - return @constCast(&self.writer.interface); 111 + pub fn anyWriter(self: *PosixTty) *std.Io.Writer { 112 + return &self.writer.interface; 121 113 } 122 114 123 115 pub fn read(self: *const PosixTty, buf: []u8) !usize { 124 116 return posix.read(self.fd, buf); 125 117 } 126 118 127 - pub fn opaqueRead(ptr: *const anyopaque, buf: []u8) !usize { 128 - const self: *const PosixTty = @ptrCast(@alignCast(ptr)); 129 - return posix.read(self.fd, buf); 130 - } 131 - 132 - pub fn anyReader(self: *const PosixTty) std.io.AnyReader { 133 - return .{ 134 - .context = self, 135 - .readFn = PosixTty.opaqueRead, 136 - }; 137 - } 138 - 139 119 /// Install a signal handler for winsize. A maximum of 8 handlers may be 140 120 /// installed 141 121 pub fn notifyWinsize(handler: SignalHandler) !void { ··· 205 185 .y_pixel = winsize.ypixel, 206 186 }; 207 187 return error.IoctlError; 208 - } 209 - 210 - pub fn bufferedWriter(self: *const PosixTty) *std.io.Writer { 211 - // The embedded writer is already buffered with a 4096-byte buffer 212 - return self.anyWriter(); 213 188 } 214 189 }; 215 190 ··· 225 200 buf: [4]u8 = undefined, 226 201 227 202 /// File.Writer for efficient buffered writing 203 + reader: std.fs.File.Writer, 228 204 writer: std.fs.File.Writer, 229 205 230 206 /// The last mouse button that was pressed. We store the previous state of button presses on each ··· 249 225 }; 250 226 251 227 pub fn init(buffer: []u8) !Tty { 252 - const stdin = std.io.getStdIn().handle; 253 - const stdout = std.io.getStdOut().handle; 228 + const stdin: std.fs.File = .stdout(); 229 + const stdout: std.fs.File = .stdout(); 254 230 255 231 // get initial modes 256 232 const initial_output_codepage = windows.kernel32.GetConsoleOutputCP(); 257 - const initial_input_mode = try getConsoleMode(CONSOLE_MODE_INPUT, stdin); 258 - const initial_output_mode = try getConsoleMode(CONSOLE_MODE_OUTPUT, stdout); 233 + const initial_input_mode = try getConsoleMode(CONSOLE_MODE_INPUT, stdin.handle); 234 + const initial_output_mode = try getConsoleMode(CONSOLE_MODE_OUTPUT, stdout.handle); 259 235 260 236 // set new modes 261 - try setConsoleMode(stdin, input_raw_mode); 262 - try setConsoleMode(stdout, output_raw_mode); 237 + try setConsoleMode(stdin.handle, input_raw_mode); 238 + try setConsoleMode(stdout.handle, output_raw_mode); 263 239 if (windows.kernel32.SetConsoleOutputCP(utf8_codepage) == 0) 264 240 return windows.unexpectedError(windows.kernel32.GetLastError()); 265 241 266 - const file = std.fs.File{ .handle = stdout }; 267 - 268 242 const self: Tty = .{ 269 - .stdin = stdin, 270 - .stdout = stdout, 243 + .stdin = stdin.handle, 244 + .stdout = stdout.handle, 271 245 .initial_codepage = initial_output_codepage, 272 246 .initial_input_mode = initial_input_mode, 273 247 .initial_output_mode = initial_output_mode, 274 - .writer = std.fs.File.Writer.initStreaming(file, buffer), 248 + .writer = .initStreaming(stdout, buffer), 275 249 }; 276 250 277 251 // save a copy of this tty as the global_tty for panic handling ··· 326 300 }; 327 301 } 328 302 329 - /// Write bytes to the tty 330 - pub fn write(self: *Tty, bytes: []const u8) !usize { 331 - return self.writer.interface.write(bytes); 332 - } 333 - 334 - pub fn opaqueWrite(ptr: *const anyopaque, bytes: []const u8) !usize { 335 - const self: *Tty = @ptrCast(@alignCast(ptr)); 336 - return self.writer.interface.write(bytes); 303 + pub fn anyWriter(self: *Tty) *std.Io.Writer { 304 + return &self.writer.interface; 337 305 } 338 306 339 - pub fn anyWriter(self: *const Tty) *std.io.Writer { 340 - return @constCast(&self.writer.interface); 307 + pub fn read(self: *const Tty, buf: []u8) !usize { 308 + return posix.read(self.fd, buf); 341 309 } 342 310 343 311 pub fn nextEvent(self: *Tty, parser: *Parser, paste_allocator: ?std.mem.Allocator) !Event { ··· 730 698 fd: posix.fd_t, 731 699 pipe_read: posix.fd_t, 732 700 pipe_write: posix.fd_t, 733 - writer: *std.io.Writer.Allocating, 701 + writer: *std.Io.Writer.Allocating, 734 702 735 703 /// Initializes a TestTty. 736 704 pub fn init(buffer: []u8) !TestTty { 705 + _ = buffer; 706 + 737 707 if (builtin.os.tag == .windows) return error.SkipZigTest; 738 - const list = try std.testing.allocator.create(std.io.Writer.Allocating); 708 + const list = try std.testing.allocator.create(std.Io.Writer.Allocating); 739 709 list.* = .init(std.testing.allocator); 740 710 const r, const w = try posix.pipe(); 741 711 return .{ ··· 753 723 std.testing.allocator.destroy(self.writer); 754 724 } 755 725 756 - pub fn anyWriter(self: *const TestTty) *std.io.Writer { 726 + pub fn anyWriter(self: *TestTty) *std.Io.Writer { 757 727 return &self.writer.writer; 758 728 } 759 729 760 730 pub fn read(self: *const TestTty, buf: []u8) !usize { 761 731 return posix.read(self.fd, buf); 762 - } 763 - 764 - pub fn opaqueRead(ptr: *const anyopaque, buf: []u8) !usize { 765 - const self: *const TestTty = @ptrCast(@alignCast(ptr)); 766 - return posix.read(self.fd, buf); 767 - } 768 - 769 - pub fn anyReader(self: *const TestTty) std.io.AnyReader { 770 - return .{ 771 - .context = self, 772 - .readFn = TestTty.opaqueRead, 773 - }; 774 732 } 775 733 776 734 /// Get the window size from the kernel