From b2432a672f931042d98bb87630c0908d2c762d2d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 21 Aug 2023 08:28:20 -0700 Subject: [PATCH] terminal/kitty-gfx: add debug function to dump image data --- src/terminal/Terminal.zig | 3 +-- src/terminal/kitty/graphics_exec.zig | 5 +++-- src/terminal/kitty/graphics_image.zig | 26 ++++++++++++++++++++++++++ src/termio/Exec.zig | 3 +-- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index 260fcf319..5726e2833 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -1566,10 +1566,9 @@ pub fn getPwd(self: *const Terminal) ?[]const u8 { pub fn kittyGraphics( self: *Terminal, alloc: Allocator, - buf: []u8, cmd: *kitty.graphics.Command, ) ?kitty.graphics.Response { - return kitty.graphics.execute(alloc, self, buf, cmd); + return kitty.graphics.execute(alloc, self, cmd); } /// Full reset diff --git a/src/terminal/kitty/graphics_exec.zig b/src/terminal/kitty/graphics_exec.zig index 8daaf8994..6b0e67786 100644 --- a/src/terminal/kitty/graphics_exec.zig +++ b/src/terminal/kitty/graphics_exec.zig @@ -28,10 +28,8 @@ const log = std.log.scoped(.kitty_gfx); pub fn execute( alloc: Allocator, terminal: *Terminal, - buf: []u8, cmd: *Command, ) ?Response { - _ = buf; log.debug("executing kitty graphics command: {}", .{cmd.control}); const resp_: ?Response = switch (cmd.control) { @@ -246,6 +244,9 @@ fn loadAndAddImage( return img; } + // Dump the image data before it is decompressed + // img.debugDump() catch unreachable; + // Validate and store our image try img.complete(alloc); try storage.addImage(alloc, img); diff --git a/src/terminal/kitty/graphics_image.zig b/src/terminal/kitty/graphics_image.zig index f3c92fa5f..aed2375cc 100644 --- a/src/terminal/kitty/graphics_image.zig +++ b/src/terminal/kitty/graphics_image.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const builtin = @import("builtin"); const assert = std.debug.assert; const Allocator = std.mem.Allocator; const ArenaAllocator = std.heap.ArenaAllocator; @@ -73,6 +74,31 @@ pub const Image = struct { UnsupportedMedium, }; + /// Debug function to write the data to a file. This is useful for + /// capturing some test data for unit tests. + pub fn debugDump(self: Image) !void { + if (comptime builtin.mode != .Debug) @compileError("debugDump in non-debug"); + + var buf: [1024]u8 = undefined; + const filename = try std.fmt.bufPrint( + &buf, + "image-{s}-{s}-{d}x{d}-{}.data", + .{ + @tagName(self.format), + @tagName(self.compression), + self.width, + self.height, + self.id, + }, + ); + const cwd = std.fs.cwd(); + const f = try cwd.createFile(filename, .{}); + defer f.close(); + + const writer = f.writer(); + try writer.writeAll(self.data); + } + /// The length of the data in bytes, uncompressed. While this will /// decompress compressed data to count the bytes it doesn't actually /// store the decompressed data so this doesn't allocate much. diff --git a/src/termio/Exec.zig b/src/termio/Exec.zig index 330dc3d72..dd94e6e5c 100644 --- a/src/termio/Exec.zig +++ b/src/termio/Exec.zig @@ -1087,8 +1087,7 @@ const StreamHandler = struct { // log.warn("APC command: {}", .{cmd}); switch (cmd) { .kitty => |*kitty_cmd| { - var partial_buf: [512]u8 = undefined; - if (self.terminal.kittyGraphics(self.alloc, &partial_buf, kitty_cmd)) |resp| { + if (self.terminal.kittyGraphics(self.alloc, kitty_cmd)) |resp| { var buf: [1024]u8 = undefined; var buf_stream = std.io.fixedBufferStream(&buf); try resp.encode(buf_stream.writer());