Merge pull request #2186 from ghostty-org/kitty-fix

kitty graphics: respond OK with metadata on display actions
This commit is contained in:
Mitchell Hashimoto
2024-09-03 11:11:33 -07:00
committed by GitHub
3 changed files with 17 additions and 11 deletions

View File

@ -256,16 +256,22 @@ pub const Response = struct {
// We only encode a result if we have either an id or an image number. // We only encode a result if we have either an id or an image number.
if (self.id == 0 and self.image_number == 0) return; if (self.id == 0 and self.image_number == 0) return;
// Used to cheaply keep track if we need to add a comma before
// the next key-value pair.
var prior: bool = false;
try writer.writeAll("\x1b_G"); try writer.writeAll("\x1b_G");
if (self.id > 0) { if (self.id > 0) {
prior = true;
try writer.print("i={}", .{self.id}); try writer.print("i={}", .{self.id});
} }
if (self.image_number > 0) { if (self.image_number > 0) {
if (self.id > 0) try writer.writeByte(','); if (prior) try writer.writeByte(',') else prior = true;
try writer.print("I={}", .{self.image_number}); try writer.print("I={}", .{self.image_number});
} }
if (self.placement_id > 0) { if (self.placement_id > 0) {
try writer.print(",p={}", .{self.placement_id}); if (prior) try writer.writeByte(',') else prior = true;
try writer.print("p={}", .{self.placement_id});
} }
try writer.writeByte(';'); try writer.writeByte(';');
try writer.writeAll(self.message); try writer.writeAll(self.message);

View File

@ -140,12 +140,13 @@ fn transmit(
// If there are more chunks expected we do not respond. // If there are more chunks expected we do not respond.
if (load.more) return .{}; if (load.more) return .{};
// After the image is added, set the ID in case it changed // If our image has no ID or number, we don't respond at all. Conversely,
result.id = load.image.id; // if we have either an ID or number, we always respond.
if (load.image.id == 0 and load.image.number == 0) return .{};
// If the original request had an image number, then we respond. // After the image is added, set the ID in case it changed.
// Otherwise, we don't respond. // The resulting image number and placement ID never change.
if (load.image.number == 0) return .{}; result.id = load.image.id;
return result; return result;
} }
@ -177,7 +178,7 @@ fn display(
else else
storage.imageByNumber(d.image_number); storage.imageByNumber(d.image_number);
const img = img_ orelse { const img = img_ orelse {
result.message = "EINVAL: image not found"; result.message = "ENOENT: image not found";
return result; return result;
}; };
@ -253,8 +254,7 @@ fn display(
}, },
} }
// Display does not result in a response on success return result;
return .{};
} }
/// Display a previously transmitted image. /// Display a previously transmitted image.

View File

@ -304,7 +304,7 @@ pub const StreamHandler = struct {
try resp.encode(buf_stream.writer()); try resp.encode(buf_stream.writer());
const final = buf_stream.getWritten(); const final = buf_stream.getWritten();
if (final.len > 2) { if (final.len > 2) {
// log.warn("kitty graphics response: {s}", .{std.fmt.fmtSliceHexLower(final)}); log.debug("kitty graphics response: {s}", .{std.fmt.fmtSliceHexLower(final)});
self.messageWriter(try termio.Message.writeReq(self.alloc, final)); self.messageWriter(try termio.Message.writeReq(self.alloc, final));
} }
} }