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(
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

View File

@ -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);

View File

@ -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.

View File

@ -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());