diff --git a/README.md b/README.md index 675bc55..b7beb22 100644 --- a/README.md +++ b/README.md @@ -297,7 +297,7 @@ pub fn main() !void { var vx = try vaxis.init(alloc, .{}); // deinit takes an optional allocator. If your program is exiting, you can // choose to pass a null allocator to save some exit time. - defer vx.deinit(alloc, tty.anyWriter()); + defer vx.deinit(alloc, tty.writer()); // The event loop requires an intrusive init. We create an instance with @@ -317,7 +317,7 @@ pub fn main() !void { defer loop.stop(); // Optionally enter the alternate screen - try vx.enterAltScreen(tty.anyWriter()); + try vx.enterAltScreen(tty.writer()); // We'll adjust the color index every keypress for the border var color_idx: u8 = 0; @@ -329,7 +329,7 @@ pub fn main() !void { // Sends queries to terminal to detect certain features. This should always // be called after entering the alt screen, if you are using the alt screen - try vx.queryTerminal(tty.anyWriter(), 1 * std.time.ns_per_s); + try vx.queryTerminal(tty.writer(), 1 * std.time.ns_per_s); while (true) { // nextEvent blocks until an event is in the queue @@ -365,7 +365,7 @@ pub fn main() !void { // more than one byte will incur an allocation on the first render // after it is drawn. Thereafter, it will not allocate unless the // screen is resized - .winsize => |ws| try vx.resize(alloc, tty.anyWriter(), ws), + .winsize => |ws| try vx.resize(alloc, tty.writer(), ws), else => {}, } @@ -401,7 +401,7 @@ pub fn main() !void { // Render the screen. Using a buffered writer will offer much better // performance, but is not required - try vx.render(tty.anyWriter()); + try vx.render(tty.writer()); } } ``` diff --git a/USAGE.md b/USAGE.md index 997384f..221bd20 100644 --- a/USAGE.md +++ b/USAGE.md @@ -247,7 +247,7 @@ pub fn TtyWatcher(comptime Userdata: type) type { self.vx.caps.color_scheme_updates = true; }, .cap_da1 => { - self.vx.enableDetectedFeatures(self.tty.anyWriter()) catch |err| { + self.vx.enableDetectedFeatures(self.tty.writer()) catch |err| { log.err("couldn't enable features: {}", .{err}); }; }, @@ -328,7 +328,7 @@ pub fn LoopWithModules(T: type, aio: type, coro: type) type { } pub fn deinit(self: *@This(), vx: *vaxis.Vaxis, tty: *vaxis.Tty) void { - vx.deviceStatusReport(tty.anyWriter()) catch {}; + vx.deviceStatusReport(tty.writer()) catch {}; if (self.winsize_task) |task| task.cancel(); if (self.reader_task) |task| task.cancel(); self.source.deinit(); diff --git a/examples/cli.zig b/examples/cli.zig index 2334211..7ddb53a 100644 --- a/examples/cli.zig +++ b/examples/cli.zig @@ -19,7 +19,7 @@ pub fn main() !void { defer tty.deinit(); var vx = try vaxis.init(alloc, .{}); - defer vx.deinit(alloc, tty.anyWriter()); + defer vx.deinit(alloc, tty.writer()); var loop: vaxis.Loop(Event) = .{ .tty = &tty, .vaxis = &vx }; try loop.init(); @@ -27,7 +27,7 @@ pub fn main() !void { try loop.start(); defer loop.stop(); - try vx.queryTerminal(tty.anyWriter(), 1 * std.time.ns_per_s); + try vx.queryTerminal(tty.writer(), 1 * std.time.ns_per_s); var text_input = TextInput.init(alloc, &vx.unicode); defer text_input.deinit(); @@ -75,7 +75,7 @@ pub fn main() !void { } }, .winsize => |ws| { - try vx.resize(alloc, tty.anyWriter(), ws); + try vx.resize(alloc, tty.writer(), ws); }, else => {}, } @@ -96,7 +96,7 @@ pub fn main() !void { _ = win.print(&seg, .{ .row_offset = @intCast(j + 1) }); } } - try vx.render(tty.anyWriter()); + try vx.render(tty.writer()); } } diff --git a/examples/image.zig b/examples/image.zig index f39fcc1..dc878c7 100644 --- a/examples/image.zig +++ b/examples/image.zig @@ -23,7 +23,7 @@ pub fn main() !void { defer tty.deinit(); var vx = try vaxis.init(alloc, .{}); - defer vx.deinit(alloc, tty.anyWriter()); + defer vx.deinit(alloc, tty.writer()); var loop: vaxis.Loop(Event) = .{ .tty = &tty, .vaxis = &vx }; try loop.init(); @@ -31,21 +31,21 @@ pub fn main() !void { try loop.start(); defer loop.stop(); - try vx.enterAltScreen(tty.anyWriter()); - try vx.queryTerminal(tty.anyWriter(), 1 * std.time.ns_per_s); + try vx.enterAltScreen(tty.writer()); + try vx.queryTerminal(tty.writer(), 1 * std.time.ns_per_s); var read_buffer: [1024 * 1024]u8 = undefined; // 1MB buffer var img1 = try vaxis.zigimg.Image.fromFilePath(alloc, "examples/zig.png", &read_buffer); defer img1.deinit(); const imgs = [_]vaxis.Image{ - try vx.transmitImage(alloc, tty.anyWriter(), &img1, .rgba), + try vx.transmitImage(alloc, tty.writer(), &img1, .rgba), // var img1 = try vaxis.zigimg.Image.fromFilePath(alloc, "examples/zig.png"); - // try vx.loadImage(alloc, tty.anyWriter(), .{ .path = "examples/zig.png" }), - try vx.loadImage(alloc, tty.anyWriter(), .{ .path = "examples/vaxis.png" }), + // try vx.loadImage(alloc, tty.writer(), .{ .path = "examples/zig.png" }), + try vx.loadImage(alloc, tty.writer(), .{ .path = "examples/vaxis.png" }), }; - defer vx.freeImage(tty.anyWriter(), imgs[0].id); - defer vx.freeImage(tty.anyWriter(), imgs[1].id); + defer vx.freeImage(tty.writer(), imgs[0].id); + defer vx.freeImage(tty.writer(), imgs[1].id); var n: usize = 0; @@ -64,7 +64,7 @@ pub fn main() !void { else if (key.matches('k', .{})) clip_y -|= 1; }, - .winsize => |ws| try vx.resize(alloc, tty.anyWriter(), ws), + .winsize => |ws| try vx.resize(alloc, tty.writer(), ws), } n = (n + 1) % imgs.len; @@ -78,6 +78,6 @@ pub fn main() !void { .y = clip_y, } }); - try vx.render(tty.anyWriter()); + try vx.render(tty.writer()); } } diff --git a/examples/main.zig b/examples/main.zig index 672dfce..2a4b96b 100644 --- a/examples/main.zig +++ b/examples/main.zig @@ -19,7 +19,7 @@ pub fn main() !void { defer tty.deinit(); var vx = try vaxis.init(alloc, .{}); - defer vx.deinit(alloc, tty.anyWriter()); + defer vx.deinit(alloc, tty.writer()); var loop: vaxis.Loop(Event) = .{ .tty = &tty, .vaxis = &vx }; try loop.init(); @@ -28,8 +28,8 @@ pub fn main() !void { defer loop.stop(); // Optionally enter the alternate screen - try vx.enterAltScreen(tty.anyWriter()); - try vx.queryTerminal(tty.anyWriter(), 1 * std.time.ns_per_s); + try vx.enterAltScreen(tty.writer()); + try vx.queryTerminal(tty.writer(), 1 * std.time.ns_per_s); // We'll adjust the color index every keypress var color_idx: u8 = 0; @@ -66,7 +66,7 @@ pub fn main() !void { } }, .winsize => |ws| { - try vx.resize(alloc, tty.anyWriter(), ws); + try vx.resize(alloc, tty.writer(), ws); }, else => {}, } @@ -114,7 +114,7 @@ pub fn main() !void { child.writeCell(@intCast(i), scale, second_cell); } // Render the screen - try vx.render(tty.anyWriter()); + try vx.render(tty.writer()); } } diff --git a/examples/table.zig b/examples/table.zig index ac1fa59..00f1fd7 100644 --- a/examples/table.zig +++ b/examples/table.zig @@ -30,11 +30,11 @@ pub fn main() !void { var buffer: [1024]u8 = undefined; var tty = try vaxis.Tty.init(&buffer); defer tty.deinit(); - const tty_writer = tty.anyWriter(); + const tty_writer = tty.writer(); var vx = try vaxis.init(alloc, .{ .kitty_keyboard_flags = .{ .report_events = true }, }); - defer vx.deinit(alloc, tty.anyWriter()); + defer vx.deinit(alloc, tty.writer()); var loop: vaxis.Loop(union(enum) { key_press: vaxis.Key, @@ -44,8 +44,8 @@ pub fn main() !void { try loop.init(); try loop.start(); defer loop.stop(); - try vx.enterAltScreen(tty.anyWriter()); - try vx.queryTerminal(tty.anyWriter(), 250 * std.time.ns_per_ms); + try vx.enterAltScreen(tty.writer()); + try vx.queryTerminal(tty.writer(), 250 * std.time.ns_per_ms); const logo = \\░█░█░█▀█░█░█░▀█▀░█▀▀░░░▀█▀░█▀█░█▀▄░█░░░█▀▀░ @@ -191,7 +191,7 @@ pub fn main() !void { } moving = false; }, - .winsize => |ws| try vx.resize(alloc, tty.anyWriter(), ws), + .winsize => |ws| try vx.resize(alloc, tty.writer(), ws), else => {}, } diff --git a/examples/text_input.zig b/examples/text_input.zig index b1fcbde..7a75d7d 100644 --- a/examples/text_input.zig +++ b/examples/text_input.zig @@ -36,13 +36,13 @@ pub fn main() !void { // Use a buffered writer for better performance. There are a lot of writes // in the render loop and this can have a significant savings - const writer = tty.anyWriter(); + const writer = tty.writer(); // Initialize Vaxis var vx = try vaxis.init(alloc, .{ .kitty_keyboard_flags = .{ .report_events = true }, }); - defer vx.deinit(alloc, tty.anyWriter()); + defer vx.deinit(alloc, tty.writer()); var loop: vaxis.Loop(Event) = .{ .vaxis = &vx, @@ -71,7 +71,7 @@ pub fn main() !void { try writer.flush(); // Sends queries to terminal to detect certain features. This should // _always_ be called, but is left to the application to decide when - try vx.queryTerminal(tty.anyWriter(), 1 * std.time.ns_per_s); + try vx.queryTerminal(tty.writer(), 1 * std.time.ns_per_s); // The main event loop. Vaxis provides a thread safe, blocking, buffered // queue which can serve as the primary event queue for an application @@ -92,12 +92,12 @@ pub fn main() !void { } else if (key.matches('l', .{ .ctrl = true })) { vx.queueRefresh(); } else if (key.matches('n', .{ .ctrl = true })) { - try vx.notify(tty.anyWriter(), "vaxis", "hello from vaxis"); + try vx.notify(tty.writer(), "vaxis", "hello from vaxis"); loop.stop(); var child = std.process.Child.init(&.{"nvim"}, alloc); _ = try child.spawnAndWait(); try loop.start(); - try vx.enterAltScreen(tty.anyWriter()); + try vx.enterAltScreen(tty.writer()); vx.queueRefresh(); } else if (key.matches(vaxis.Key.enter, .{}) or key.matches('j', .{ .ctrl = true })) { text_input.clearAndFree(); @@ -121,7 +121,7 @@ pub fn main() !void { // more than one byte will incur an allocation on the first render // after it is drawn. Thereafter, it will not allocate unless the // screen is resized - .winsize => |ws| try vx.resize(alloc, tty.anyWriter(), ws), + .winsize => |ws| try vx.resize(alloc, tty.writer(), ws), else => {}, } diff --git a/examples/vaxis.zig b/examples/vaxis.zig index 60c7843..5ca566c 100644 --- a/examples/vaxis.zig +++ b/examples/vaxis.zig @@ -25,7 +25,7 @@ pub fn main() !void { defer tty.deinit(); var vx = try vaxis.init(alloc, .{}); - defer vx.deinit(alloc, tty.anyWriter()); + defer vx.deinit(alloc, tty.writer()); var loop: vaxis.Loop(Event) = .{ .tty = &tty, .vaxis = &vx }; try loop.init(); @@ -33,11 +33,11 @@ pub fn main() !void { try loop.start(); defer loop.stop(); - try vx.enterAltScreen(tty.anyWriter()); - try vx.queryTerminal(tty.anyWriter(), 1 * std.time.ns_per_s); + try vx.enterAltScreen(tty.writer()); + try vx.queryTerminal(tty.writer(), 1 * std.time.ns_per_s); - try vx.queryColor(tty.anyWriter(), .fg); - try vx.queryColor(tty.anyWriter(), .bg); + try vx.queryColor(tty.writer(), .fg); + try vx.queryColor(tty.writer(), .bg); var pct: u8 = 0; var dir: enum { up, @@ -53,7 +53,7 @@ pub fn main() !void { switch (event) { .key_press => |key| if (key.matches('c', .{ .ctrl = true })) return, .winsize => |ws| { - try vx.resize(alloc, tty.anyWriter(), ws); + try vx.resize(alloc, tty.writer(), ws); break; }, } @@ -63,7 +63,7 @@ pub fn main() !void { while (loop.tryEvent()) |event| { switch (event) { .key_press => |key| if (key.matches('c', .{ .ctrl = true })) return, - .winsize => |ws| try vx.resize(alloc, tty.anyWriter(), ws), + .winsize => |ws| try vx.resize(alloc, tty.writer(), ws), } } @@ -83,7 +83,7 @@ pub fn main() !void { // var bw = tty.bufferedWriter(); // try vx.render(bw.writer().any()); // try bw.flush(); - try vx.render(tty.anyWriter()); + try vx.render(tty.writer()); std.Thread.sleep(16 * std.time.ns_per_ms); switch (dir) { .up => { diff --git a/examples/view.zig b/examples/view.zig index 88d4bf5..f9f1500 100644 --- a/examples/view.zig +++ b/examples/view.zig @@ -48,13 +48,13 @@ pub fn main() !void { var tty = try vaxis.Tty.init(&buffer); defer tty.deinit(); - const writer = tty.anyWriter(); + const writer = tty.writer(); // Initialize Vaxis var vx = try vaxis.init(alloc, .{ .kitty_keyboard_flags = .{ .report_events = true }, }); - defer vx.deinit(alloc, tty.anyWriter()); + defer vx.deinit(alloc, tty.writer()); var loop: vaxis.Loop(Event) = .{ .vaxis = &vx, .tty = &tty, @@ -64,7 +64,7 @@ pub fn main() !void { defer loop.stop(); try vx.enterAltScreen(writer); try writer.flush(); - try vx.queryTerminal(tty.anyWriter(), 20 * std.time.ns_per_s); + try vx.queryTerminal(tty.writer(), 20 * std.time.ns_per_s); // Initialize Views // - Large Map @@ -128,7 +128,7 @@ pub fn main() !void { // Mini View (Forced Width & Height Limits) if (key.matches('m', .{})) use_mini_view = !use_mini_view; }, - .winsize => |ws| try vx.resize(alloc, tty.anyWriter(), ws), + .winsize => |ws| try vx.resize(alloc, tty.writer(), ws), } const win = vx.window(); diff --git a/examples/vt.zig b/examples/vt.zig index 934324a..bc95242 100644 --- a/examples/vt.zig +++ b/examples/vt.zig @@ -23,7 +23,7 @@ pub fn main() !void { var buffer: [1024]u8 = undefined; var tty = try vaxis.Tty.init(&buffer); var vx = try vaxis.init(alloc, .{}); - defer vx.deinit(alloc, tty.anyWriter()); + defer vx.deinit(alloc, tty.writer()); var loop: vaxis.Loop(Event) = .{ .tty = &tty, .vaxis = &vx }; try loop.init(); @@ -33,8 +33,8 @@ pub fn main() !void { var buffered = tty.bufferedWriter(); - try vx.enterAltScreen(tty.anyWriter()); - try vx.queryTerminal(tty.anyWriter(), 1 * std.time.ns_per_s); + try vx.enterAltScreen(tty.writer()); + try vx.queryTerminal(tty.writer(), 1 * std.time.ns_per_s); var env = try std.process.getEnvMap(alloc); defer env.deinit(); @@ -82,7 +82,7 @@ pub fn main() !void { try vt.update(.{ .key_press = key }); }, .winsize => |ws| { - try vx.resize(alloc, tty.anyWriter(), ws); + try vx.resize(alloc, tty.writer(), ws); }, } } diff --git a/src/Loop.zig b/src/Loop.zig index 0892a15..8209a1e 100644 --- a/src/Loop.zig +++ b/src/Loop.zig @@ -58,7 +58,7 @@ pub fn Loop(comptime T: type) type { if (self.thread == null) return; self.should_quit = true; // trigger a read - self.vaxis.deviceStatusReport(self.tty.anyWriter()) catch {}; + self.vaxis.deviceStatusReport(self.tty.writer()) catch {}; if (self.thread) |thread| { thread.join(); @@ -398,7 +398,7 @@ test Loop { defer tty.deinit(); var vx = try vaxis.init(std.testing.allocator, .{}); - defer vx.deinit(std.testing.allocator, tty.anyWriter()); + defer vx.deinit(std.testing.allocator, tty.writer()); var loop: vaxis.Loop(Event) = .{ .tty = &tty, .vaxis = &vx }; try loop.init(); @@ -407,6 +407,6 @@ test Loop { defer loop.stop(); // Optionally enter the alternate screen - try vx.enterAltScreen(tty.anyWriter()); - try vx.queryTerminal(tty.anyWriter(), 1 * std.time.ns_per_ms); + try vx.enterAltScreen(tty.writer()); + try vx.queryTerminal(tty.writer(), 1 * std.time.ns_per_ms); } diff --git a/src/main.zig b/src/main.zig index 65a4c02..443da59 100644 --- a/src/main.zig +++ b/src/main.zig @@ -73,7 +73,7 @@ pub fn recover() void { ctlseqs.bp_reset ++ ctlseqs.rmcup; - gty.anyWriter().writeAll(reset) catch {}; + gty.writer().writeAll(reset) catch {}; gty.deinit(); } diff --git a/src/tty.zig b/src/tty.zig index 7784cc1..5b87cff 100644 --- a/src/tty.zig +++ b/src/tty.zig @@ -36,7 +36,7 @@ pub const PosixTty = struct { reader: std.fs.File.Reader, /// File.Writer for efficient buffered writing - writer: std.fs.File.Writer, + tty_writer: std.fs.File.Writer, pub const SignalHandler = struct { context: *anyopaque, @@ -77,7 +77,7 @@ pub const PosixTty = struct { .fd = fd, .termios = termios, .reader = file.reader(buffer), - .writer = .initStreaming(file, buffer), + .tty_writer = .initStreaming(file, buffer), }; global_tty = self; @@ -109,8 +109,8 @@ pub const PosixTty = struct { posix.sigaction(posix.SIG.WINCH, &act, null); } - pub fn anyWriter(self: *PosixTty) *std.Io.Writer { - return &self.writer.interface; + pub fn writer(self: *PosixTty) *std.Io.Writer { + return &self.tty_writer.interface; } pub fn read(self: *const PosixTty, buf: []u8) !usize { @@ -202,7 +202,7 @@ pub const WindowsTty = struct { /// File.Writer for efficient buffered writing reader: std.fs.File.Writer, - writer: std.fs.File.Writer, + tty_writer: std.fs.File.Writer, /// The last mouse button that was pressed. We store the previous state of button presses on each /// mouse event so we can detect which button was released @@ -301,8 +301,8 @@ pub const WindowsTty = struct { }; } - pub fn anyWriter(self: *Tty) *std.Io.Writer { - return &self.writer.interface; + pub fn writer(self: *Tty) *std.Io.Writer { + return &self.tty_writer.interface; } pub fn read(self: *const Tty, buf: []u8) !usize { @@ -699,7 +699,7 @@ pub const TestTty = struct { fd: posix.fd_t, pipe_read: posix.fd_t, pipe_write: posix.fd_t, - writer: *std.Io.Writer.Allocating, + tty_writer: *std.Io.Writer.Allocating, /// Initializes a TestTty. pub fn init(buffer: []u8) !TestTty { @@ -713,19 +713,19 @@ pub const TestTty = struct { .fd = r, .pipe_read = r, .pipe_write = w, - .writer = list, + .tty_writer = list, }; } pub fn deinit(self: TestTty) void { std.posix.close(self.pipe_read); std.posix.close(self.pipe_write); - self.writer.deinit(); - std.testing.allocator.destroy(self.writer); + self.tty_writer.deinit(); + std.testing.allocator.destroy(self.tty_writer); } - pub fn anyWriter(self: *TestTty) *std.Io.Writer { - return &self.writer.writer; + pub fn writer(self: *TestTty) *std.Io.Writer { + return &self.tty_writer.writer; } pub fn read(self: *const TestTty, buf: []u8) !usize { diff --git a/src/vxfw/App.zig b/src/vxfw/App.zig index 166d60a..9210dbb 100644 --- a/src/vxfw/App.zig +++ b/src/vxfw/App.zig @@ -47,7 +47,7 @@ pub fn init(allocator: Allocator) !App { pub fn deinit(self: *App) void { self.timers.deinit(self.allocator); - self.vx.deinit(self.allocator, self.tty.anyWriter()); + self.vx.deinit(self.allocator, self.tty.writer()); self.tty.deinit(); } @@ -64,10 +64,10 @@ pub fn run(self: *App, widget: vxfw.Widget, opts: Options) anyerror!void { // Also always initialize the app with a focus event loop.postEvent(.focus_in); - try vx.enterAltScreen(tty.anyWriter()); - try vx.queryTerminal(tty.anyWriter(), 1 * std.time.ns_per_s); - try vx.setBracketedPaste(tty.anyWriter(), true); - try vx.subscribeToColorSchemeUpdates(tty.anyWriter()); + try vx.enterAltScreen(tty.writer()); + try vx.queryTerminal(tty.writer(), 1 * std.time.ns_per_s); + try vx.setBracketedPaste(tty.writer(), true); + try vx.subscribeToColorSchemeUpdates(tty.writer()); { // This part deserves a comment. loop.init installs a signal handler for the tty. We wait to @@ -78,7 +78,7 @@ pub fn run(self: *App, widget: vxfw.Widget, opts: Options) anyerror!void { // NOTE: We don't use pixel mouse anywhere vx.caps.sgr_pixels = false; - try vx.setMouseMode(tty.anyWriter(), true); + try vx.setMouseMode(tty.writer(), true); // Give DrawContext the unicode data vxfw.DrawContext.init(&vx.unicode, vx.screen.width_method); @@ -149,7 +149,7 @@ pub fn run(self: *App, widget: vxfw.Widget, opts: Options) anyerror!void { }, .mouse => |mouse| try mouse_handler.handleMouse(self, &ctx, mouse), .winsize => |ws| { - try vx.resize(self.allocator, tty.anyWriter(), ws); + try vx.resize(self.allocator, tty.writer(), ws); ctx.redraw = true; }, else => { @@ -246,7 +246,7 @@ fn render( }); surface.render(root_win, focused_widget); - try vx.render(tty.anyWriter()); + try vx.render(tty.writer()); } fn addTick(self: *App, tick: vxfw.Tick) Allocator.Error!void { @@ -262,7 +262,7 @@ fn handleCommand(self: *App, cmds: *vxfw.CommandList) Allocator.Error!void { .set_mouse_shape => |shape| self.vx.setMouseShape(shape), .request_focus => |widget| self.wants_focus = widget, .copy_to_clipboard => |content| { - self.vx.copyToSystemClipboard(self.tty.anyWriter(), content, self.allocator) catch |err| { + self.vx.copyToSystemClipboard(self.tty.writer(), content, self.allocator) catch |err| { switch (err) { error.OutOfMemory => return Allocator.Error.OutOfMemory, else => std.log.err("copy error: {}", .{err}), @@ -270,13 +270,13 @@ fn handleCommand(self: *App, cmds: *vxfw.CommandList) Allocator.Error!void { }; }, .set_title => |title| { - self.vx.setTitle(self.tty.anyWriter(), title) catch |err| { + self.vx.setTitle(self.tty.writer(), title) catch |err| { std.log.err("set_title error: {}", .{err}); }; }, .queue_refresh => self.vx.queueRefresh(), .notify => |notification| { - self.vx.notify(self.tty.anyWriter(), notification.title, notification.body) catch |err| { + self.vx.notify(self.tty.writer(), notification.title, notification.body) catch |err| { std.log.err("notify error: {}", .{err}); }; const alloc = self.allocator; @@ -286,7 +286,7 @@ fn handleCommand(self: *App, cmds: *vxfw.CommandList) Allocator.Error!void { alloc.free(notification.body); }, .query_color => |kind| { - self.vx.queryColor(self.tty.anyWriter(), kind) catch |err| { + self.vx.queryColor(self.tty.writer(), kind) catch |err| { std.log.err("queryColor error: {}", .{err}); }; }, diff --git a/src/widgets/terminal/Terminal.zig b/src/widgets/terminal/Terminal.zig index a236b60..1efd328 100644 --- a/src/widgets/terminal/Terminal.zig +++ b/src/widgets/terminal/Terminal.zig @@ -241,7 +241,7 @@ pub fn tryEvent(self: *Terminal) ?Event { pub fn update(self: *Terminal, event: InputEvent) !void { switch (event) { - .key_press => |k| try key.encode(self.anyWriter(), k, true, self.back_screen.csi_u_flags), + .key_press => |k| try key.encode(self.writer(), k, true, self.back_screen.csi_u_flags), } } @@ -250,12 +250,14 @@ fn opaqueWrite(ptr: *const anyopaque, buf: []const u8) !usize { return posix.write(self.pty.pty, buf); } -pub fn anyWriter(self: *const Terminal) *std.io.Writer { - const writer: std.io.Writer = .{ - .context = self, - .writeFn = Terminal.opaqueWrite, +pub fn writer(self: *const Terminal) *std.io.Writer { + const local = struct { + var writer: std.io.Writer = .{ + .context = self, + .writeFn = Terminal.opaqueWrite, + }; }; - return @constCast(&writer); + return &local.writer; } fn opaqueRead(ptr: *const anyopaque, buf: []u8) !usize { @@ -521,13 +523,13 @@ fn run(self: *Terminal) !void { if (seq.private_marker) |pm| { switch (pm) { // Secondary - '>' => try self.anyWriter().writeAll("\x1B[>1;69;0c"), - '=' => try self.anyWriter().writeAll("\x1B[=0000c"), + '>' => try self.writer().writeAll("\x1B[>1;69;0c"), + '=' => try self.writer().writeAll("\x1B[=0000c"), else => log.info("unhandled CSI: {}", .{seq}), } } else { // Primary - try self.anyWriter().writeAll("\x1B[?62;22c"); + try self.writer().writeAll("\x1B[?62;22c"); } }, // Cursor Vertical Position Absolute @@ -592,8 +594,8 @@ fn run(self: *Terminal) !void { const ps = iter.next() orelse 0; if (seq.intermediate == null and seq.private_marker == null) { switch (ps) { - 5 => try self.anyWriter().writeAll("\x1b[0n"), - 6 => try self.anyWriter().print("\x1b[{d};{d}R", .{ + 5 => try self.writer().writeAll("\x1b[0n"), + 6 => try self.writer().print("\x1b[{d};{d}R", .{ self.back_screen.cursor.row + 1, self.back_screen.cursor.col + 1, }), @@ -609,10 +611,10 @@ fn run(self: *Terminal) !void { // report mode '$' => { switch (ps) { - 2026 => try self.anyWriter().writeAll("\x1b[?2026;2$p"), + 2026 => try self.writer().writeAll("\x1b[?2026;2$p"), else => { std.log.warn("unhandled mode: {}", .{ps}); - try self.anyWriter().print("\x1b[?{d};0$p", .{ps}); + try self.writer().print("\x1b[?{d};0$p", .{ps}); }, } }, @@ -634,7 +636,7 @@ fn run(self: *Terminal) !void { if (seq.private_marker) |pm| { switch (pm) { // XTVERSION - '>' => try self.anyWriter().print( + '>' => try self.writer().print( "\x1bP>|libvaxis {s}\x1B\\", .{"dev"}, ),