this repo has no description
13
fork

Configure Feed

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

fix examples to work with updated writer api

Update multiple examples to work with the new Tty.init() API that requires
a buffer parameter and fix related writer interface issues.

Changes:
- Add buffer parameter to Tty.init() calls in cli.zig, image.zig, vaxis.zig,
view.zig, and vt.zig
- Fix anyWriter() method signatures in tty.zig to accept const pointers
- Update App.zig in vxfw framework to handle buffer allocation
- Replace bufferedWriter usage pattern in view.zig with direct anyWriter calls
- Fix const-related writer interface issues

Most examples now compile successfully with the new Writer API. Some examples
like counter.zig and scroll.zig have additional issues with ArrayList API
changes that are unrelated to the Writer modifications.

Amp-Thread-ID: https://ampcode.com/threads/T-04d58023-ce84-479f-8974-6c8fad9ce9e5
Co-authored-by: Amp <amp@ampcode.com>

+35 -26
+2 -1
examples/cli.zig
··· 14 14 } 15 15 const alloc = gpa.allocator(); 16 16 17 - var tty = try vaxis.Tty.init(); 17 + var buffer: [1024]u8 = undefined; 18 + var tty = try vaxis.Tty.init(&buffer); 18 19 defer tty.deinit(); 19 20 20 21 var vx = try vaxis.init(alloc, .{});
+2 -1
examples/image.zig
··· 18 18 } 19 19 const alloc = gpa.allocator(); 20 20 21 - var tty = try vaxis.Tty.init(); 21 + var buffer: [1024]u8 = undefined; 22 + var tty = try vaxis.Tty.init(&buffer); 22 23 defer tty.deinit(); 23 24 24 25 var vx = try vaxis.init(alloc, .{});
+2 -1
examples/vaxis.zig
··· 20 20 } 21 21 const alloc = gpa.allocator(); 22 22 23 - var tty = try vaxis.Tty.init(); 23 + var buffer: [1024]u8 = undefined; 24 + var tty = try vaxis.Tty.init(&buffer); 24 25 defer tty.deinit(); 25 26 26 27 var vx = try vaxis.init(alloc, .{});
+5 -5
examples/view.zig
··· 44 44 x, y, 45 45 }); 46 46 47 - var tty = try vaxis.Tty.init(); 47 + var buffer: [1024]u8 = undefined; 48 + var tty = try vaxis.Tty.init(&buffer); 48 49 defer tty.deinit(); 49 50 50 - var buffered_writer = tty.bufferedWriter(); 51 - const writer = buffered_writer.writer().any(); 51 + const writer = tty.anyWriter(); 52 52 53 53 // Initialize Vaxis 54 54 var vx = try vaxis.init(alloc, .{ ··· 63 63 try loop.start(); 64 64 defer loop.stop(); 65 65 try vx.enterAltScreen(writer); 66 - try buffered_writer.flush(); 66 + try writer.flush(); 67 67 try vx.queryTerminal(tty.anyWriter(), 20 * std.time.ns_per_s); 68 68 69 69 // Initialize Views ··· 181 181 182 182 // Render the screen 183 183 try vx.render(writer); 184 - try buffered_writer.flush(); 184 + try writer.flush(); 185 185 } 186 186 } 187 187
+2 -1
examples/vt.zig
··· 20 20 } 21 21 const alloc = gpa.allocator(); 22 22 23 - var tty = try vaxis.Tty.init(); 23 + var buffer: [1024]u8 = undefined; 24 + var tty = try vaxis.Tty.init(&buffer); 24 25 var vx = try vaxis.init(alloc, .{}); 25 26 defer vx.deinit(alloc, tty.anyWriter()); 26 27
+6 -6
src/tty.zig
··· 116 116 return self.writer.interface.write(bytes); 117 117 } 118 118 119 - pub fn anyWriter(self: *PosixTty) *std.io.Writer { 120 - return &self.writer.interface; 119 + pub fn anyWriter(self: *const PosixTty) *std.io.Writer { 120 + return @constCast(&self.writer.interface); 121 121 } 122 122 123 123 pub fn read(self: *const PosixTty, buf: []u8) !usize { ··· 207 207 return error.IoctlError; 208 208 } 209 209 210 - pub fn bufferedWriter(self: *PosixTty) *std.io.Writer { 210 + pub fn bufferedWriter(self: *const PosixTty) *std.io.Writer { 211 211 // The embedded writer is already buffered with a 4096-byte buffer 212 - return &self.writer; 212 + return self.anyWriter(); 213 213 } 214 214 }; 215 215 ··· 336 336 return self.writer.interface.write(bytes); 337 337 } 338 338 339 - pub fn anyWriter(self: *Tty) *std.io.Writer { 340 - return &self.writer.interface; 339 + pub fn anyWriter(self: *const Tty) *std.io.Writer { 340 + return @constCast(&self.writer.interface); 341 341 } 342 342 343 343 pub fn nextEvent(self: *Tty, parser: *Parser, paste_allocator: ?std.mem.Allocator) !Event {
+16 -11
src/vxfw/App.zig
··· 16 16 vx: vaxis.Vaxis, 17 17 timers: std.ArrayList(vxfw.Tick), 18 18 wants_focus: ?vxfw.Widget, 19 + buffer: [1024]u8, 19 20 20 21 /// Runtime options 21 22 pub const Options = struct { ··· 27 28 /// object on the heap. Call destroy when the app is complete to reset terminal state and release 28 29 /// resources 29 30 pub fn init(allocator: Allocator) !App { 30 - return .{ 31 + var app: App = .{ 31 32 .allocator = allocator, 32 - .tty = try vaxis.Tty.init(), 33 + .tty = undefined, 33 34 .vx = try vaxis.init(allocator, .{ 34 35 .system_clipboard_allocator = allocator, 35 36 .kitty_keyboard_flags = .{ 36 37 .report_events = true, 37 38 }, 38 39 }), 39 - .timers = std.ArrayList(vxfw.Tick).init(allocator), 40 + .timers = std.ArrayList(vxfw.Tick){}, 40 41 .wants_focus = null, 42 + .buffer = undefined, 41 43 }; 44 + app.tty = try vaxis.Tty.init(&app.buffer); 45 + return app; 42 46 } 43 47 44 48 pub fn deinit(self: *App) void { 45 - self.timers.deinit(); 49 + self.timers.deinit(self.allocator); 46 50 self.vx.deinit(self.allocator, self.tty.anyWriter()); 47 51 self.tty.deinit(); 48 52 } ··· 90 94 var mouse_handler = MouseHandler.init(widget); 91 95 defer mouse_handler.deinit(self.allocator); 92 96 var focus_handler = FocusHandler.init(self.allocator, widget); 93 - try focus_handler.path_to_focused.append(widget); 94 - defer focus_handler.deinit(); 97 + try focus_handler.path_to_focused.append(self.allocator, widget); 98 + defer focus_handler.deinit(self.allocator); 95 99 96 100 // Timestamp of our next frame 97 101 var next_frame_ms: u64 = @intCast(std.time.milliTimestamp()); 98 102 99 103 // Create our event context 100 104 var ctx: vxfw.EventContext = .{ 105 + .alloc = self.allocator, 101 106 .phase = .capturing, 102 - .cmds = vxfw.CommandList.init(self.allocator), 107 + .cmds = vxfw.CommandList{}, 103 108 .consume_event = false, 104 109 .redraw = false, 105 110 .quit = false, ··· 508 513 focused_widget: vxfw.Widget, 509 514 path_to_focused: std.ArrayList(Widget), 510 515 511 - fn init(allocator: Allocator, root: Widget) FocusHandler { 516 + fn init(_: Allocator, root: Widget) FocusHandler { 512 517 return .{ 513 518 .root = root, 514 519 .focused_widget = root, 515 - .path_to_focused = std.ArrayList(Widget).init(allocator), 520 + .path_to_focused = std.ArrayList(Widget){}, 516 521 }; 517 522 } 518 523 519 - fn deinit(self: *FocusHandler) void { 520 - self.path_to_focused.deinit(); 524 + fn deinit(self: *FocusHandler, allocator: Allocator) void { 525 + self.path_to_focused.deinit(allocator); 521 526 } 522 527 523 528 /// Update the focus list