diff --git a/README.md b/README.md --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Libvaxis _does not use terminfo_. Support for vt features is detected through terminal queries. -Vaxis uses zig `0.15.1`. +Vaxis uses zig `0.16.0`. ## Features @@ -277,24 +277,17 @@ foo: u8, }; -pub fn main() !void { - var gpa = std.heap.GeneralPurposeAllocator(.{}){}; - defer { - const deinit_status = gpa.deinit(); - //fail test; can't try in defer as defer is executed after we return - if (deinit_status == .leak) { - std.log.err("memory leak", .{}); - } - } - const alloc = gpa.allocator(); +pub fn main(init: std.process.Init) !void { + const io = init.io; + const alloc = init.gpa; // Initialize a tty var buffer: [1024]u8 = undefined; - var tty = try vaxis.Tty.init(&buffer); + var tty = try vaxis.Tty.init(io, &buffer); defer tty.deinit(); // Initialize Vaxis - var vx = try vaxis.init(alloc, .{}); + var vx = try vaxis.init(io, alloc, init.environ_map, .{}); // 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.writer()); @@ -305,11 +298,7 @@ // installs a signal handler for SIGWINCH on posix TTYs // // This event loop is thread safe. It reads the tty in a separate thread - var loop: vaxis.Loop(Event) = .{ - .tty = &tty, - .vaxis = &vx, - }; - try loop.init(); + var loop: vaxis.Loop(Event) = .init(io, &tty, &vx); // Start the read loop. This puts the terminal in raw mode and begins // reading user input diff --git a/build.zig b/build.zig --- a/build.zig +++ b/build.zig @@ -110,6 +110,17 @@ }), }); + // Let's make sure that all of the examples compile and can run any tests + // that they may have defined. + var it = examples.iterator(); + while (it.next()) |v| { + const e = b.addTest(.{ + .root_module = v.value.*, + }); + const r = b.addRunArtifact(e); + tests_step.dependOn(&r.step); + } + const tests_run = b.addRunArtifact(tests); b.installArtifact(tests); tests_step.dependOn(&tests_run.step); diff --git a/flake.lock b/flake.lock --- a/flake.lock +++ b/flake.lock @@ -2,11 +2,11 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1775710090, - "narHash": "sha256-WGjBfvXv/mcg5yBg+AtK1Q3FHyXfjAAeJROmg7DLYfM=", - "rev": "4c1018dae018162ec878d42fec712642d214fdfa", + "lastModified": 1776169885, + "narHash": "sha256-Gk2T0tDDDAs319hp/ak+bAIUG5bPMvnNEjPV8CS86Fg=", + "rev": "4bd9165a9165d7b5e33ae57f3eecbcb28fb231c9", "type": "tarball", - "url": "https://releases.nixos.org/nixos/unstable/nixos-26.05pre977467.4c1018dae018/nixexprs.tar.xz" + "url": "https://releases.nixos.org/nixos/unstable/nixos-26.05pre980183.4bd9165a9165/nixexprs.tar.xz" }, "original": { "type": "tarball", diff --git a/typos.toml b/typos.toml new file mode 100644 --- /dev/null +++ b/typos.toml @@ -0,0 +1,5 @@ +[default] + +[default.extend-words] +varius = "varius" +Nam = "Nam" diff --git a/examples/cli.zig b/examples/cli.zig --- a/examples/cli.zig +++ b/examples/cli.zig @@ -102,3 +102,7 @@ focus_in, foo: u8, }; + +test { + std.testing.refAllDecls(@This()); +} diff --git a/examples/counter.zig b/examples/counter.zig --- a/examples/counter.zig +++ b/examples/counter.zig @@ -136,3 +136,7 @@ try app.run(model.widget(), .{}); } + +test { + std.testing.refAllDecls(@This()); +} diff --git a/examples/fuzzy.zig b/examples/fuzzy.zig --- a/examples/fuzzy.zig +++ b/examples/fuzzy.zig @@ -240,3 +240,7 @@ return 130; } } + +test { + std.testing.refAllDecls(@This()); +} diff --git a/examples/image.zig b/examples/image.zig --- a/examples/image.zig +++ b/examples/image.zig @@ -73,3 +73,7 @@ try vx.render(tty.writer()); } } + +// test { +// std.testing.refAllDecls(@This()); +// } diff --git a/examples/list_view.zig b/examples/list_view.zig --- a/examples/list_view.zig +++ b/examples/list_view.zig @@ -95,3 +95,7 @@ try app.run(model.widget(), .{}); } + +test { + std.testing.refAllDecls(@This()); +} diff --git a/examples/main.zig b/examples/main.zig --- a/examples/main.zig +++ b/examples/main.zig @@ -119,3 +119,7 @@ focus_in, foo: u8, }; + +test { + std.testing.refAllDecls(@This()); +} diff --git a/examples/scroll.zig b/examples/scroll.zig --- a/examples/scroll.zig +++ b/examples/scroll.zig @@ -210,3 +210,7 @@ try app.run(model.widget(), .{}); } + +test { + std.testing.refAllDecls(@This()); +} diff --git a/examples/split_view.zig b/examples/split_view.zig --- a/examples/split_view.zig +++ b/examples/split_view.zig @@ -70,3 +70,7 @@ try app.run(model.widget(), .{}); } + +test { + std.testing.refAllDecls(@This()); +} diff --git a/examples/table.zig b/examples/table.zig --- a/examples/table.zig +++ b/examples/table.zig @@ -347,3 +347,7 @@ .{ .first = "Tyler", .last = "Sanders", .user = "bennettjessica", .email = null, .phone = "1966269423" }, .{ .first = "Pamela", .last = "Carter", .user = "zsnyder", .email = null, .phone = "125-062-9130x58413" }, }; + +test { + std.testing.refAllDecls(@This()); +} diff --git a/examples/text_input.zig b/examples/text_input.zig --- a/examples/text_input.zig +++ b/examples/text_input.zig @@ -149,3 +149,7 @@ try writer.flush(); } } + +test { + std.testing.refAllDecls(@This()); +} diff --git a/examples/text_view.zig b/examples/text_view.zig --- a/examples/text_view.zig +++ b/examples/text_view.zig @@ -57,3 +57,7 @@ try tty.writer().flush(); } } + +test { + std.testing.refAllDecls(@This()); +} diff --git a/examples/vaxis.zig b/examples/vaxis.zig --- a/examples/vaxis.zig +++ b/examples/vaxis.zig @@ -110,3 +110,7 @@ @min(b_a + b_b, 255), } }; } + +test { + std.testing.refAllDecls(@This()); +} diff --git a/examples/view.zig b/examples/view.zig --- a/examples/view.zig +++ b/examples/view.zig @@ -349,3 +349,7 @@ @setEvalBranchQuota(100_000); break :mapHeight @intCast(mem.count(u8, lg_world_map, "\n")); }; + +test { + std.testing.refAllDecls(@This()); +} diff --git a/examples/vt.zig b/examples/vt.zig --- a/examples/vt.zig +++ b/examples/vt.zig @@ -102,3 +102,7 @@ try vx.render(writer); } } + +test { + std.testing.refAllDecls(@This()); +} diff --git a/src/Cell.zig b/src/Cell.zig --- a/src/Cell.zig +++ b/src/Cell.zig @@ -219,3 +219,7 @@ } } }; + +test { + std.testing.refAllDecls(@This()); +} diff --git a/src/GraphemeCache.zig b/src/GraphemeCache.zig --- a/src/GraphemeCache.zig +++ b/src/GraphemeCache.zig @@ -18,3 +18,7 @@ // return the slice return self.buf[self.idx .. self.idx + bytes.len]; } + +test { + std.testing.refAllDecls(@This()); +} diff --git a/src/Image.zig b/src/Image.zig --- a/src/Image.zig +++ b/src/Image.zig @@ -186,3 +186,7 @@ .cols = cell_width, }; } + +test { + std.testing.refAllDecls(@This()); +} diff --git a/src/InternalScreen.zig b/src/InternalScreen.zig --- a/src/InternalScreen.zig +++ b/src/InternalScreen.zig @@ -10,10 +10,10 @@ const InternalScreen = @This(); pub const InternalCell = struct { - char: std.ArrayListUnmanaged(u8) = .empty, + char: std.ArrayList(u8) = .empty, style: Style = .{}, - uri: std.ArrayListUnmanaged(u8) = .empty, - uri_id: std.ArrayListUnmanaged(u8) = .empty, + uri: std.ArrayList(u8) = .empty, + uri_id: std.ArrayList(u8) = .empty, // if we got skipped because of a wide character skipped: bool = false, default: bool = true, @@ -48,16 +48,16 @@ mouse_shape: MouseShape = .default, /// sets each cell to the default cell -pub fn init(alloc: std.mem.Allocator, w: u16, h: u16) !InternalScreen { - const arena = try alloc.create(std.heap.ArenaAllocator); - arena.* = .init(alloc); +pub fn init(gpa: std.mem.Allocator, w: u16, h: u16) !InternalScreen { + const arena = try gpa.create(std.heap.ArenaAllocator); + arena.* = .init(gpa); var screen = InternalScreen{ .arena = arena, .buf = try arena.allocator().alloc(InternalCell, @as(usize, @intCast(w)) * h), }; for (screen.buf, 0..) |_, i| { screen.buf[i] = .{ - .char = try std.ArrayListUnmanaged(u8).initCapacity(arena.allocator(), 1), + .char = try .initCapacity(arena.allocator(), 1), .uri = .empty, .uri_id = .empty, }; @@ -92,16 +92,16 @@ const i = (@as(usize, @intCast(row)) * self.width) + col; assert(i < self.buf.len); self.buf[i].char.clearRetainingCapacity(); - self.buf[i].char.appendSlice(self.arena.allocator(), cell.char.grapheme) catch { - log.warn("couldn't write grapheme", .{}); + self.buf[i].char.appendSlice(self.arena.allocator(), cell.char.grapheme) catch |err| { + log.warn("couldn't write grapheme: {t}", .{err}); }; self.buf[i].uri.clearRetainingCapacity(); - self.buf[i].uri.appendSlice(self.arena.allocator(), cell.link.uri) catch { - log.warn("couldn't write uri", .{}); + self.buf[i].uri.appendSlice(self.arena.allocator(), cell.link.uri) catch |err| { + log.warn("couldn't write uri: {t}", .{err}); }; self.buf[i].uri_id.clearRetainingCapacity(); - self.buf[i].uri_id.appendSlice(self.arena.allocator(), cell.link.params) catch { - log.warn("couldn't write uri_id", .{}); + self.buf[i].uri_id.appendSlice(self.arena.allocator(), cell.link.params) catch |err| { + log.warn("couldn't write uri_id: {t}", .{err}); }; self.buf[i].style = cell.style; self.buf[i].default = cell.default; @@ -142,4 +142,8 @@ const read_back = screen.readCell(0, 1) orelse return error.TestUnexpectedResult; try std.testing.expect(std.mem.eql(u8, read_back.char.grapheme, "A")); try std.testing.expect(screen.readCell(2, 0) == null); +} + +test { + std.testing.refAllDecls(@This()); } diff --git a/src/Key.zig b/src/Key.zig --- a/src/Key.zig +++ b/src/Key.zig @@ -456,3 +456,7 @@ test "name_map" { try testing.expectEqual(insert, name_map.get("insert")); } + +test { + std.testing.refAllDecls(@This()); +} diff --git a/src/Loop.zig b/src/Loop.zig --- a/src/Loop.zig +++ b/src/Loop.zig @@ -427,7 +427,7 @@ foo: u8, }; - var tty = try vaxis.Tty.init(&.{}); + var tty = try vaxis.Tty.init(io, &.{}); defer tty.deinit(); var vx = try vaxis.init(io, std.testing.allocator, &env_map, .{}); @@ -441,4 +441,8 @@ // Optionally enter the alternate screen try vx.enterAltScreen(tty.writer()); try vx.queryTerminal(tty.writer(), .fromSeconds(1)); +} + +test { + std.testing.refAllDecls(@This()); } diff --git a/src/Mouse.zig b/src/Mouse.zig --- a/src/Mouse.zig +++ b/src/Mouse.zig @@ -48,3 +48,8 @@ button: Button, mods: Modifiers, type: Type, + +test { + const std = @import("std"); + std.testing.refAllDecls(@This()); +} diff --git a/src/Parser.zig b/src/Parser.zig --- a/src/Parser.zig +++ b/src/Parser.zig @@ -1325,3 +1325,7 @@ try testing.expectEqual(7, result.n); try testing.expectEqualDeep(expected_event, result.event); } + +test { + std.testing.refAllDecls(@This()); +} diff --git a/src/Screen.zig b/src/Screen.zig --- a/src/Screen.zig +++ b/src/Screen.zig @@ -72,3 +72,7 @@ test "refAllDecls" { std.testing.refAllDecls(@This()); } + +test { + std.testing.refAllDecls(@This()); +} diff --git a/src/ctlseqs.zig b/src/ctlseqs.zig --- a/src/ctlseqs.zig +++ b/src/ctlseqs.zig @@ -154,3 +154,8 @@ pub const osc12_query = "\x1b]12;?\x1b\\"; // cursor color pub const osc12_set = "\x1b]12;rgb:{x:0>2}{x:0>2}/{x:0>2}{x:0>2}/{x:0>2}{x:0>2}\x1b\\"; // set terminal cursor color pub const osc12_reset = "\x1b]112\x1b\\"; // reset cursor to terminal default + +test { + const std = @import("std"); + std.testing.refAllDecls(@This()); +} diff --git a/src/event.zig b/src/event.zig --- a/src/event.zig +++ b/src/event.zig @@ -28,3 +28,8 @@ cap_color_scheme_updates, cap_multi_cursor, }; + +test { + const std = @import("std"); + std.testing.refAllDecls(@This()); +} diff --git a/src/gwidth.zig b/src/gwidth.zig --- a/src/gwidth.zig +++ b/src/gwidth.zig @@ -214,3 +214,7 @@ // Should be width 1 (combining mark is zero-width) try testing.expectEqual(1, gwidth("รก", .unicode)); } + +test { + std.testing.refAllDecls(@This()); +} diff --git a/src/queue.zig b/src/queue.zig --- a/src/queue.zig +++ b/src/queue.zig @@ -370,3 +370,7 @@ try t1.await(io); try t2.await(io); } + +test { + std.testing.refAllDecls(@This()); +} diff --git a/src/tty.zig b/src/tty.zig --- a/src/tty.zig +++ b/src/tty.zig @@ -707,7 +707,7 @@ tty_writer: *std.Io.Writer.Allocating, /// Initializes a TestTty. - pub fn init(buffer: []u8) !TestTty { + pub fn init(_: std.Io, buffer: []u8) !TestTty { _ = buffer; if (builtin.os.tag != .linux) return error.SkipZigTest; @@ -761,3 +761,7 @@ return; } }; + +test { + std.testing.refAllDecls(@This()); +} diff --git a/src/unicode.zig b/src/unicode.zig --- a/src/unicode.zig +++ b/src/unicode.zig @@ -63,3 +63,7 @@ pub fn graphemeIterator(str: []const u8) GraphemeIterator { return GraphemeIterator.init(str); } + +test { + std.testing.refAllDecls(@This()); +} diff --git a/src/vxfw/App.zig b/src/vxfw/App.zig --- a/src/vxfw/App.zig +++ b/src/vxfw/App.zig @@ -604,3 +604,7 @@ } } }; + +test { + std.testing.refAllDecls(@This()); +} diff --git a/src/vxfw/Border.zig b/src/vxfw/Border.zig --- a/src/vxfw/Border.zig +++ b/src/vxfw/Border.zig @@ -37,7 +37,7 @@ /// If Border has a bounded maximum size, it will shrink the maximum size to account for the border /// before drawing the child. If the size is unbounded, border will draw the child and then itself -/// around the childs size +/// around the child's size pub fn draw(self: *const Border, ctx: vxfw.DrawContext) Allocator.Error!vxfw.Surface { const max_width: ?u16 = if (ctx.max.width) |width| width -| 2 else null; const max_height: ?u16 = if (ctx.max.height) |height| height -| 2 else null;