terminal/kitty-gfx: add debug function to dump image data

This commit is contained in:
Mitchell Hashimoto
2023-08-21 08:28:20 -07:00
parent 89dfe85740
commit b2432a672f
4 changed files with 31 additions and 6 deletions

View File

@ -1566,10 +1566,9 @@ pub fn getPwd(self: *const Terminal) ?[]const u8 {
pub fn kittyGraphics( pub fn kittyGraphics(
self: *Terminal, self: *Terminal,
alloc: Allocator, alloc: Allocator,
buf: []u8,
cmd: *kitty.graphics.Command, cmd: *kitty.graphics.Command,
) ?kitty.graphics.Response { ) ?kitty.graphics.Response {
return kitty.graphics.execute(alloc, self, buf, cmd); return kitty.graphics.execute(alloc, self, cmd);
} }
/// Full reset /// Full reset

View File

@ -28,10 +28,8 @@ const log = std.log.scoped(.kitty_gfx);
pub fn execute( pub fn execute(
alloc: Allocator, alloc: Allocator,
terminal: *Terminal, terminal: *Terminal,
buf: []u8,
cmd: *Command, cmd: *Command,
) ?Response { ) ?Response {
_ = buf;
log.debug("executing kitty graphics command: {}", .{cmd.control}); log.debug("executing kitty graphics command: {}", .{cmd.control});
const resp_: ?Response = switch (cmd.control) { const resp_: ?Response = switch (cmd.control) {
@ -246,6 +244,9 @@ fn loadAndAddImage(
return img; return img;
} }
// Dump the image data before it is decompressed
// img.debugDump() catch unreachable;
// Validate and store our image // Validate and store our image
try img.complete(alloc); try img.complete(alloc);
try storage.addImage(alloc, img); try storage.addImage(alloc, img);

View File

@ -1,4 +1,5 @@
const std = @import("std"); const std = @import("std");
const builtin = @import("builtin");
const assert = std.debug.assert; const assert = std.debug.assert;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const ArenaAllocator = std.heap.ArenaAllocator; const ArenaAllocator = std.heap.ArenaAllocator;
@ -73,6 +74,31 @@ pub const Image = struct {
UnsupportedMedium, 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 /// The length of the data in bytes, uncompressed. While this will
/// decompress compressed data to count the bytes it doesn't actually /// decompress compressed data to count the bytes it doesn't actually
/// store the decompressed data so this doesn't allocate much. /// store the decompressed data so this doesn't allocate much.

View File

@ -1087,8 +1087,7 @@ const StreamHandler = struct {
// log.warn("APC command: {}", .{cmd}); // log.warn("APC command: {}", .{cmd});
switch (cmd) { switch (cmd) {
.kitty => |*kitty_cmd| { .kitty => |*kitty_cmd| {
var partial_buf: [512]u8 = undefined; if (self.terminal.kittyGraphics(self.alloc, kitty_cmd)) |resp| {
if (self.terminal.kittyGraphics(self.alloc, &partial_buf, kitty_cmd)) |resp| {
var buf: [1024]u8 = undefined; var buf: [1024]u8 = undefined;
var buf_stream = std.io.fixedBufferStream(&buf); var buf_stream = std.io.fixedBufferStream(&buf);
try resp.encode(buf_stream.writer()); try resp.encode(buf_stream.writer());