terminal/kitty: calculate cell height more efficiently

This commit is contained in:
Mitchell Hashimoto
2024-03-13 10:27:19 -07:00
parent 0a3f431d1b
commit d7ee705a7a
2 changed files with 31 additions and 24 deletions

View File

@ -222,19 +222,16 @@ fn display(
switch (d.cursor_movement) { switch (d.cursor_movement) {
.none => {}, .none => {},
.after => { .after => {
const rect = p.rect(img, terminal); // We use terminal.index to properly handle scroll regions.
const size = p.gridSize(img, terminal);
// We can do better by doing this with pure internal screen state for (0..size.rows) |_| terminal.index() catch |err| {
// but this handles scroll regions.
const height = rect.bottom_right.y - rect.top_left.y;
for (0..height) |_| terminal.index() catch |err| {
log.warn("failed to move cursor: {}", .{err}); log.warn("failed to move cursor: {}", .{err});
break; break;
}; };
terminal.setCursorPos( terminal.setCursorPos(
terminal.screen.cursor.y, terminal.screen.cursor.y,
rect.bottom_right.x + 1, p.pin.x + size.cols + 1,
); );
}, },
} }

View File

@ -581,23 +581,19 @@ pub const ImageStorage = struct {
s.pages.untrackPin(self.pin); s.pages.untrackPin(self.pin);
} }
/// Returns a selection of the entire rectangle this placement /// Returns the size in grid cells that this placement takes up.
/// occupies within the screen. pub fn gridSize(
pub fn rect(
self: Placement, self: Placement,
image: Image, image: Image,
t: *const terminal.Terminal, t: *const terminal.Terminal,
) Rect { ) struct {
// If we have columns/rows specified we can simplify this whole thing. cols: u32,
if (self.columns > 0 and self.rows > 0) { rows: u32,
var br = switch (self.pin.downOverflow(self.rows)) { } {
.offset => |v| v, if (self.columns > 0 and self.rows > 0) return .{
.overflow => |v| v.end, .cols = self.columns,
}; .rows = self.rows,
br.x = @min(self.pin.x + self.columns, t.cols - 1); };
return .{ .top_left = self.pin.*, .bottom_right = br };
}
// Calculate our cell size. // Calculate our cell size.
const terminal_width_f64: f64 = @floatFromInt(t.width_px); const terminal_width_f64: f64 = @floatFromInt(t.width_px);
@ -617,12 +613,26 @@ pub const ImageStorage = struct {
const width_cells: u32 = @intFromFloat(@ceil(width_f64 / cell_width_f64)); const width_cells: u32 = @intFromFloat(@ceil(width_f64 / cell_width_f64));
const height_cells: u32 = @intFromFloat(@ceil(height_f64 / cell_height_f64)); const height_cells: u32 = @intFromFloat(@ceil(height_f64 / cell_height_f64));
// TODO(paged-terminal): clean this logic up above return .{
var br = switch (self.pin.downOverflow(height_cells)) { .cols = width_cells,
.rows = height_cells,
};
}
/// Returns a selection of the entire rectangle this placement
/// occupies within the screen.
pub fn rect(
self: Placement,
image: Image,
t: *const terminal.Terminal,
) Rect {
const grid_size = self.gridSize(image, t);
var br = switch (self.pin.downOverflow(grid_size.rows)) {
.offset => |v| v, .offset => |v| v,
.overflow => |v| v.end, .overflow => |v| v.end,
}; };
br.x = @min(self.pin.x + width_cells, t.cols - 1); br.x = @min(self.pin.x + grid_size.cols, t.cols - 1);
return .{ return .{
.top_left = self.pin.*, .top_left = self.pin.*,