From 43f73eb895d44b9df77f5fe6dffa9854d6e69243 Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Wed, 31 Jan 2024 07:18:16 -0600 Subject: [PATCH] image: implement deleting images from memory Signed-off-by: Tim Culverhouse --- examples/image.zig | 2 ++ src/vaxis.zig | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/examples/image.zig b/examples/image.zig index aba084d..8a0ae15 100644 --- a/examples/image.zig +++ b/examples/image.zig @@ -32,6 +32,8 @@ pub fn main() !void { try vx.loadImage(alloc, .{ .path = "examples/zig.png" }), try vx.loadImage(alloc, .{ .path = "examples/vaxis.png" }), }; + defer vx.freeImage(imgs[0].id); + defer vx.freeImage(imgs[1].id); var n: usize = 0; diff --git a/src/vaxis.zig b/src/vaxis.zig index 24790ba..95ef9fd 100644 --- a/src/vaxis.zig +++ b/src/vaxis.zig @@ -646,6 +646,19 @@ pub fn Vaxis(comptime T: type) type { .height = img.height, }; } + + /// deletes an image from the terminal's memory + pub fn freeImage(self: Self, id: u32) void { + var tty = self.tty orelse return; + const writer = tty.buffered_writer.writer(); + std.fmt.format(writer, "\x1b_Ga=d,d=I,i={d};\x1b\\", .{id}) catch |err| { + log.err("couldn't delete image {d}: {}", .{ id, err }); + return; + }; + tty.buffered_writer.flush() catch |err| { + log.err("couldn't flush writer: {}", .{err}); + }; + } }; }