1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
//! A program's colours on a pane's body: a terminal's cells over the rows
//! the body shows (its paint, handed over as plain cells: Term.paint), and
//! its kitty images over them (Term.images). The terminal gives its view's
//! cells in the theme's colours; which of them stand under which of the
//! body's rows, the edit copy's among them, is worked out here.
const std = @import("std");
const pardes = @import("pardes.zig");
const panes = @import("panes.zig");
const modal = @import("modal.zig");
const tracy = @import("tracy.zig");
const Term = @import("Term.zig");
const Pardes = pardes.Pardes;
const Pane = panes.Pane;
const Surface = pardes.Surface;
const BodyWalk = @import("body_layer.zig").BodyWalk;
const EditBuffer = panes.Text.EditBuffer;
/// The theme's colours as a terminal's paint takes them.
pub fn inkOf(theme: *const pardes.Theme) Term.Ink {
return .{
.fg = theme.fg,
.bg = theme.bg,
.tag_fg = theme.tag_fg,
.tag_bg = theme.tag_bg,
.palette = theme.palette,
.base = theme.palette orelse .{
theme.bg orelse theme.tag_bg,
theme.kw,
theme.str,
theme.num,
theme.box,
theme.sel_bg,
theme.comment,
theme.fg orelse theme.tag_fg,
theme.lineno,
theme.kw,
theme.str,
theme.num,
theme.scroll_thumb,
theme.sel_fg,
theme.tag_fg,
theme.fg orelse theme.tag_fg,
},
};
}
/// Its program's colours over the body's rows, through the theme's filter
/// when the pane has it on.
pub fn recolor(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, tx: u16, tw: u16, body_h: u16, body: []const u8) void {
const state = pane.terminal orelse return;
const body_y = p.bodyTop(pane, r);
const g = Term.grid(state);
const cols = g.cols;
const arena = p.scratch.allocator();
const cells = arena.alloc(Term.Painted, g.len * @as(usize, cols)) catch return;
const rows = arena.alloc(Term.PaintedRow, g.len) catch return;
{
const tz_paint = tracy.zone(@src(), "termPaint");
defer tz_paint.end();
const ink = inkOf(p.theme());
Term.paint(state, &ink, &p.tty_filter_palette, pane.tty_filter, cols, cells, rows);
}
const filtered = pane.tty_filter;
const vp_rows: i32 = @intCast(g.len);
// Which buffer lines the user has not actually changed, so a row swallowed
// by a growing buffer keeps the colour it still stands over.
var anchors: ?EditAnchors = null;
var walk: BodyWalk = .init(pane);
var lines = std.mem.splitScalar(u8, body, '\n');
var vr: u16 = 0;
while (walk.next()) |row| : (vr += 1) {
if (vr >= body_h) break;
// Before any early exit below, or the lines fall out of step with rows.
const text = lines.next() orelse "";
const anchor: Anchor = switch (row) {
.edit => |e| blk: {
if (anchors == null) anchors = .init(p, pane, pane.ovl.?);
var an = anchors.?.shellRow(e.idx) orelse continue;
an.row -= walk.goff;
break :blk an;
},
.grid => |v| .{ .row = v },
};
const vi = anchor.row;
if (vi < 0 or vi >= vp_rows) continue;
const prow = rows[@intCast(vi)];
const grid_row = cells[@as(usize, @intCast(vi)) * cols ..][0..prow.len];
const cut: u16 = if (pane.body.mode == .tty) 0 else cut: {
const prompt = prow.prompt orelse break :cut 0;
break :cut switch (prompt) {
.keep => 0,
// Blanked end to end: the row shows nothing of the grid, so
// projecting the prompt's own colours onto it would be a lie.
.blank => continue,
.cut => |n| std.math.cast(u16, n) orelse continue,
};
};
if (cut > 0 and text.len == 0) continue;
var at: usize = 0;
var sc: u16 = 0;
var gc: u16 = cut;
var sb: usize = 0;
const mine_from = @min(anchor.prefix, text.len);
const mine_to = text.len - @min(anchor.suffix, text.len);
var crossed = false;
while (at < text.len and sc < tw) {
const stop = modal.nextGrapheme(text, at);
if (stop <= at) break;
const span: u16 = if (sc + 1 < tw and s.at(tx + sc + 1, body_y + vr).len == 0) 2 else 1;
if (at >= mine_from and at < mine_to) {
sc += span;
at = stop;
continue;
}
if (at >= mine_to and !crossed) {
crossed = true;
const upto = anchor.shell_len - @min(anchor.suffix, anchor.shell_len);
while (sb < upto) {
if (gc >= grid_row.len) break;
sb += grid_row[gc].bytes;
gc = std.math.add(u16, gc, 1) catch break;
}
}
const want = stop - at;
// Consume every cell that contributed to this grapheme. A cluster
// ghostty split across several cells is still ONE printed glyph.
var covered: usize = 0;
var style: ?pardes.CellStyle = null;
while (covered < want) {
if (gc >= grid_row.len) break;
const c = &grid_row[gc];
if (style == null and c.wide != .spacer_tail and c.wide != .spacer_head)
style = c.style;
covered += c.bytes;
gc = std.math.add(u16, gc, 1) catch break;
// A spacer contributes no bytes; without this the loop would
// spin on a row that ends in one.
if (covered == 0 and gc >= pane.cols) break;
}
while (gc < grid_row.len and grid_row[gc].wide == .spacer_tail)
gc = std.math.add(u16, gc, 1) catch break;
if (style) |st| for (0..span) |k| {
const cell = s.at(tx + sc + @as(u16, @intCast(k)), body_y + vr);
if (cell.default and !filtered) continue;
cell.default = false;
cell.style = st;
};
sb += covered;
sc += span;
at = stop;
}
var tail: ?pardes.CellStyle = null;
if (anchor.whole() or anchor.suffix > 0) {
while (sc < tw) {
if (gc >= grid_row.len) break;
const c = &grid_row[gc];
gc = std.math.add(u16, gc, 1) catch break;
if (c.wide == .spacer_tail) continue;
const cell = s.at(tx + sc, body_y + vr);
sc += 1;
tail = c.style;
if (cell.default and !filtered) continue;
cell.default = false;
cell.style = c.style;
}
if (tail) |style| while (sc < tw) : (sc += 1) {
const cell = s.at(tx + sc, body_y + vr);
if (cell.default and !filtered) continue;
cell.default = false;
cell.style = style;
};
}
}
}
/// Its kitty images over the body's rows, as placements (after the rows
/// and their colours: placeholder cells are blanked by it).
pub fn images(p: *Pardes, s: *Surface, pane: *Pane, id: usize, tx: u16, tw: u16, body_y: u16, body_h: u16) void {
const state = pane.terminal orelse return;
Term.cellPixels(state, p.view().cell_pixels.w, p.view().cell_pixels.h);
// A body taller than this draws its images on the rows that fit.
var rows_of: [1024]?u16 = undefined;
const vp_rows: usize = @min(Term.grid(state).len, rows_of.len);
@memset(rows_of[0..vp_rows], null);
var walk: BodyWalk = .init(pane);
var vr: u16 = 0;
while (walk.next()) |row| : (vr += 1) {
if (vr >= body_h) break;
const vi = switch (row) {
.grid => |v| v,
.edit => continue,
};
if (vi < 0 or vi >= vp_rows) continue;
rows_of[@intCast(vi)] = vr;
}
Term.images(state, p.gpa, .{
.pane = @intCast(id),
.serial = pane.serial,
.tx = tx,
.body_y = body_y,
.cols = @min(tw, pane.cols),
.rows_of = rows_of[0..vp_rows],
.tty_mode = pane.body.mode == .tty,
.now_ms = p.clock.stepped_ns / std.time.ns_per_ms,
.cell_w = @max(1, p.view().cell_pixels.w),
.cell_h = @max(1, p.view().cell_pixels.h),
}, &p.surface.placements, s.cells, s.cols);
}
// Match surviving edit-buffer rows to their original terminal styles.
const EditAnchors = struct {
/// the buffer's own text, walked in order: a line the VIEWPORT skipped still
/// consumes the row it came from, so the lines below it stay aligned
text: []const u8 = &.{},
at: usize = 0,
shell: []const []const u8 = &.{},
/// the covered span, absolute grid rows, as `[first, end)`
first: usize = 0,
end: usize = 0,
lines: usize = 0,
/// the line `at` names, and the first row still unclaimed
idx: usize = 0,
cursor: usize = 0,
budget: usize = 0,
active: bool = false,
fn init(p: *Pardes, pane: *Pane, o: EditBuffer) EditAnchors {
if (o.rows <= 0 or o.row < 0) return .{};
const first: usize = @intCast(o.row);
const total = Term.grid(pane.terminal).total;
if (first >= total) return .{};
const covered: usize = @intCast(o.rows);
const count = @min(covered, total - first);
const shell = Term.editRows(pane.terminal.?, p.scratch.allocator(), first, count) catch return .{};
const lines = std.mem.count(u8, o.text, "\n") + 1;
return .{
.text = o.text,
.shell = shell,
.first = first,
.end = first + count,
.lines = lines,
.cursor = first,
.budget = covered + 4 * lines,
.active = true,
};
}
fn shellRow(a: *EditAnchors, idx: usize) ?Anchor {
if (!a.active or idx >= a.lines) return null;
var found: ?Anchor = null;
while (a.idx <= idx) : (a.idx += 1) found = a.claim(a.nextLine() orelse return null);
return found;
}
fn nextLine(a: *EditAnchors) ?[]const u8 {
if (a.at > a.text.len) return null;
const rest = a.text[a.at..];
if (std.mem.indexOfScalar(u8, rest, '\n')) |n| {
a.at += n + 1;
return rest[0..n];
}
// The last line has no terminator; one past the end ends the walk.
a.at = a.text.len + 1;
return rest;
}
/// Where this line still stands over the grid, if anywhere.
fn claim(a: *EditAnchors, line: []const u8) ?Anchor {
const end = if (line.len == 0) @min(a.cursor + 1, a.end) else a.end;
var k = a.cursor;
while (k < end) : (k += 1) {
if (a.budget == 0) return null;
a.budget -= 1;
if (!std.mem.eql(u8, line, a.shell[k - a.first])) continue;
a.cursor = k + 1;
return .{ .row = @intCast(k) };
}
if (a.cursor >= a.end) return null;
const shell = a.shell[a.cursor - a.first];
var p: usize = 0;
while (p < line.len and p < shell.len and line[p] == shell[p]) p += 1;
var s: usize = 0;
const room = @min(line.len, shell.len) - p;
while (s < room and line[line.len - 1 - s] == shell[shell.len - 1 - s]) s += 1;
if (p + s == 0) return null;
if (line.len != p + s and shell.len - (p + s) > shell.len / 2) return null;
const row = a.cursor;
if (s > 0 or p >= shell.len) a.cursor += 1;
return .{ .row = @intCast(row), .prefix = p, .suffix = s, .shell_len = shell.len };
}
};
const Anchor = struct {
/// the row, absolute while it comes from `EditAnchors`, viewport once
/// `recolorAnsi` has subtracted the walk's offset
row: i32,
prefix: usize = std.math.maxInt(usize),
suffix: usize = 0,
/// the row's own dumped length — what the suffix is measured from on the
/// GRID side, where the edit may have changed the byte count
shell_len: usize = 0,
fn whole(an: Anchor) bool {
return an.prefix == std.math.maxInt(usize);
}
};
|