diff options
| -rw-r--r-- | src/CHANGELOG.md | 4 | ||||
| -rw-r--r-- | src/Core.zig | 5 | ||||
| -rw-r--r-- | src/File.zig | 10 | ||||
| -rw-r--r-- | src/Query.zig | 2 | ||||
| -rw-r--r-- | src/Term.zig | 16 | ||||
| -rw-r--r-- | src/main.zig | 4 | ||||
| -rw-r--r-- | src/ninep/ctl.zig | 6 | ||||
| -rw-r--r-- | src/pager.zig | 595 | ||||
| -rw-r--r-- | src/pardes.zig | 3 | ||||
| -rw-r--r-- | src/term/pager.zig | 371 | ||||
| -rw-r--r-- | src/term/terminal.zig | 4 |
11 files changed, 404 insertions, 616 deletions
diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md index 3297336e..4226e986 100644 --- a/src/CHANGELOG.md +++ b/src/CHANGELOG.md @@ -7,9 +7,7 @@ value), so libvaxis is the tui's and the gui's alone, not the core module's. The 256-colour palette Petscii art and the window draw a palette colour with is pardes's own (`colors.xterm`), ghostty-vt's - default to the entry. A `+Pager` (`pardes -`) reads its program's colour - codes with a parser of its own, which pages what ghostty-vt's did, byte - for byte, about 10% faster. + default to the entry. - Commit patches open as `+Diff-<short-hash>` and reuse only their own commit's pane, so another paged patch no longer replaces one already open. diff --git a/src/Core.zig b/src/Core.zig index 62f54416..4d24dcc7 100644 --- a/src/Core.zig +++ b/src/Core.zig @@ -72,7 +72,6 @@ const memory = pardes.memory; const modal = pardes.modal; const mouse = pardes.mouse; const newer_build_words = pardes.newer_build_words; -const pager = pardes.pager; const platform = pardes.platform; const recent_files = @import("recent.zig"); const sel_slot = pardes.sel_slot; @@ -566,7 +565,7 @@ fn initSession(gpa: std.mem.Allocator, opts: Options) !*Pardes { p.oneColumn(); }, .stdin => { - const page = try pager.parse(gpa, opts.stdin_text.?); + const page = try pardes.Term.page(gpa, opts.stdin_text.?); errdefer gpa.free(page.spans); const content = page.text; errdefer gpa.free(content); @@ -2169,7 +2168,7 @@ pub const Options = struct { file: ?[]const u8 = null, file_line: usize = 0, missing: ?struct { word: []const u8, dir: []const u8 } = null, - /// `pardes -`: stdin as it came, for a +Pager pane at boot (pager.zig + /// `pardes -`: stdin as it came, for a +Pager pane at boot (Term.page /// parses it). stdin_text: ?[]const u8 = null, tty_toggle: u21 = config.tty_toggle_default, diff --git a/src/File.zig b/src/File.zig index 962737d4..336b8526 100644 --- a/src/File.zig +++ b/src/File.zig @@ -128,11 +128,11 @@ pub const State = struct { /// A +Pager whose text is a diff (`git diff`, `show`, `log -p`): drawn /// and looked at as a diff (Pane.colorAlgo). pager_diff: bool = false, - /// A +Pager's colours, as its program printed them (pager.zig): drawn + /// A +Pager's colours, as its program printed them (Term.page): drawn /// over its text by recolorSyntax, never read over 9P. ponytail: any /// edit drops them (setContentSpan); shifting them through the splice, /// as pane.zig's shiftOne moves dot, would keep them. - pager_spans: []pardes.pager.Span = &.{}, + pager_spans: []pardes.Term.Span = &.{}, /// Seconds of the last content change, for the filesystem's stat; zero until edited. mtime: u32 = 0, /// Non-null for a generated output buffer rather than an on-disk file. @@ -1955,11 +1955,11 @@ pub fn recolorSyntax(p: *Pardes, s: *Surface, pane: *Pane, f: *State, r: pardes. const whole = highlights[base - highlight_start] & syntax.diff_whole != 0; if (row_tint or whole) s.tint(tx + @as(u16, @intCast(prefix_width)), body_y + vr, tw -| @as(u16, @intCast(prefix_width)), if (row_tint) t.bg else t.line_bg, null); }; - // A +Pager's own colours (pager.zig) on every row but one the diff + // A +Pager's own colours (Term.page) on every row but one the diff // look tints: an added or removed line is drawn as pardes draws a // diff, and git's red and green on it would only fight the tint. - var span_at = if (tint != null) spans.len else std.sort.partitionPoint(pardes.pager.Span, spans, base + hs, struct { - fn before(at: usize, span: pardes.pager.Span) bool { + var span_at = if (tint != null) spans.len else std.sort.partitionPoint(pardes.Term.Span, spans, base + hs, struct { + fn before(at: usize, span: pardes.Term.Span) bool { return span.end <= at; } }.before); diff --git a/src/Query.zig b/src/Query.zig index caf64d54..27b83e1c 100644 --- a/src/Query.zig +++ b/src/Query.zig @@ -469,7 +469,7 @@ fn makeNameRows(p: *Pardes, id: usize) !void { var out: std.ArrayList(u8) = .empty; defer out.deinit(p.gpa); var rows: std.ArrayList(locations.Row) = .empty; - var spans: std.ArrayList(pardes.pager.Span) = .empty; + var spans: std.ArrayList(pardes.Term.Span) = .empty; errdefer spans.deinit(p.gpa); const chrome = p.bodyChrome(); const tint: pardes.CellStyle = .{ .fg = .{ .rgb = chrome.search_fg }, .bg = .{ .rgb = chrome.search_bg }, .bold = true }; diff --git a/src/Term.zig b/src/Term.zig index 160ed01e..6c9bd4e3 100644 --- a/src/Term.zig +++ b/src/Term.zig @@ -7,6 +7,7 @@ //! the one place Phase 1 turns into the module's registration). const std = @import("std"); const terminal = @import("term/terminal.zig"); +const pager = @import("term/pager.zig"); /// A terminal pane's emulator, its parse and what pardes keeps beside it. pub const State = terminal.State; @@ -195,3 +196,18 @@ pub const Rows = struct { text: []const u8, rows: [][]const u8, }; + +// ---- the pager ---- + +/// A program's output, as a terminal reads it, paged (`pardes -`, a +/// /pager write): its printed text and its SGR colours as spans over it +/// (`Page`), every other control dropped. The emulator's own parse, so a +/// +Pager pages what a terminal pane would print. +pub const page = pager.parse; +pub const Page = pager.Page; +/// Bytes `start..end` of a page's text, drawn in `style`. +pub const Span = pager.Span; + +test { + _ = pager; +} diff --git a/src/main.zig b/src/main.zig index ed20dceb..1e5bf9f4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -133,7 +133,7 @@ const help_text = /// `pardes -` inside a session: `text`, stdin as it came, escapes and all, /// into a new +Pager of the directory (/pager): /// written after the directory on that one open, the session parses it -/// (pager.zig) and puts it in as one undo step, left clean. A text the +/// (Term.page) and puts it in as one undo step, left clean. A text the /// session would not take is never lost: it goes to stderr, a terminal, /// with why, and the exit is 1. fn pageInto(io: std.Io, gpa: std.mem.Allocator, dial: []const u8, text: []const u8) void { @@ -167,7 +167,7 @@ fn pageInto(io: std.Io, gpa: std.mem.Allocator, dial: []const u8, text: []const } /// Stdin to its end, as it came (its escapes are the session's to parse, -/// pager.zig), at most a file's limit, a longer one cut there and said so +/// Term.page), at most a file's limit, a longer one cut there and said so /// at its end. fn readStdinText(io: std.Io, gpa: std.mem.Allocator) ![]u8 { const limit = pardes.memory.limits.max_file_bytes; diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index 2e045a73..2fa24173 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -699,7 +699,7 @@ pub fn writePager(p: *Pardes, req: Req) Reply { /// The text a /pager open wrote after its directory (`held`), a program's /// output for a terminal, put in the +Pager `serial` as `pardes -` once -/// wrote it to the body: through pager.zig's parse, its colours kept +/// wrote it to the body: through the terminal's parse (Term.page), its colours kept /// as the pane's spans, one undo step, and left /// clean. Reads of the pane are its plain text. The held bytes go either way. pub fn fillPager(p: *Pardes, serial: u32, held: *std.ArrayList(u8)) error{OutOfMemory}!void { @@ -708,7 +708,7 @@ pub fn fillPager(p: *Pardes, serial: u32, held: *std.ArrayList(u8)) error{OutOfM const id = p.paneBySerial(serial) orelse return; const pane = p.panes[id].?; const f = if (pane.file) |*file| file else return; - const page = try pardes.pager.parse(p.gpa, held.items); + const page = try pardes.Term.page(p.gpa, held.items); held.clearAndFree(p.gpa); if (page.text.len == 0 and f.content.len == 0) return page.deinit(p.gpa); if (!pane.fs.nomark) panes.File.pushUndo(p, pane) catch |err| { @@ -2648,7 +2648,7 @@ test "what a /pager open writes after its directory is the text: plain in the bo try testing.expectEqual(@as(usize, 0), e.off); try testing.expectEqual(@as(usize, 0), e.removed.len); try testing.expectEqual(f.content.len, e.ins); - // An edit drops the colours (pager.zig's ponytail). + // An edit drops the colours (Term.page's ponytail). try testing.expectEqual(Status.ok, call(p, .{ .tag = 6, .op = .write, .node = Node.of(pn.serial, .body), .data = "more\n" }).reply.status); try testing.expectEqual(@as(usize, 0), f.pager_spans.len); // Undone back to the paged text: the text comes back, its colours diff --git a/src/pager.zig b/src/pager.zig deleted file mode 100644 index dd7e4032..00000000 --- a/src/pager.zig +++ /dev/null @@ -1,595 +0,0 @@ -//! A program's output, paged into a +Pager (`pardes -`): the text it -//! prints, and its SGR colours as spans over that text. It reads the bytes -//! as a terminal does (ECMA-48's parse as ghostty-vt's table has it, UTF-8 -//! with each ill-formed part one U+FFFD) and keeps what is printed, how SGR -//! styles it and the controls that move along a line (CR, LF, BS, HT and -//! CSI's CHT, REP and the return in CNL and CPL); every other sequence is -//! read to its end and dropped (cursor motion, OSC titles and hyperlinks, -//! DCS, APC, modes). The spans are display state of the pane only: no 9P -//! file reads them (File.recolorSyntax draws them). -const std = @import("std"); -const pardes = @import("pardes.zig"); -const CellStyle = @import("surface.zig").CellStyle; -const Color = @import("surface.zig").Color; - -/// Bytes `start..end` of the text, drawn in `style`. A palette colour stays -/// `.index`, resolved through the theme as it is drawn (terminal.palColor), -/// and `.default` is the pane's own. -pub const Span = struct { start: u32, end: u32, style: CellStyle }; - -/// ponytail: past this many spans the rest of a page is plain, rather than -/// ~100 MB of spans for a 256 MiB coloured log; a packed style if it matters. -pub const max_spans = 4 << 20; - -pub const Page = struct { - text: []u8, - spans: []Span, - - pub fn deinit(page: Page, gpa: std.mem.Allocator) void { - gpa.free(page.text); - gpa.free(page.spans); - } -}; - -/// `raw`, a program's output for a terminal, as text and its spans. Both -/// are `gpa`'s. -pub fn parse(gpa: std.mem.Allocator, raw: []const u8) !Page { - var h: Handler = .{ .gpa = gpa }; - errdefer h.text.deinit(gpa); - errdefer h.spans.deinit(gpa); - try h.text.ensureTotalCapacity(gpa, raw.len); - // The printed code points since the last control, printed as one run. - var run: [4096]u32 = undefined; - var n: usize = 0; - var state: State = .ground; - var i: usize = 0; - feed: while (i < raw.len) { - const c = raw[i]; - if (state != .ground or c == 0x1b) { - h.printRun(run[0..n]); - n = 0; - state = h.step(state, c); - i += 1; - continue; - } - // Printable ASCII, most of a page, straight into the run. - if (c >= 0x20 and c < 0x7f) { - const end = @min(raw.len, i + run.len - n); - while (i < end and raw[i] >= 0x20 and raw[i] < 0x7f) : (i += 1) { - run[n] = raw[i]; - n += 1; - } - if (n < run.len) continue; - h.printRun(&run); - n = 0; - continue; - } - // UTF-8: an ill-formed part (a stray byte, or the longest start of - // a sequence that does not go on) is one U+FFFD; one the input ends - // inside may yet go on, and is not printed. - var cp: u21 = c; - var len: usize = 1; - if (c >= 0x80) { - cp = 0xfffd; - if (c >= 0xc2 and c <= 0xf4) { - const want: usize = if (c < 0xe0) 2 else if (c < 0xf0) 3 else 4; - const lo: u8 = if (c == 0xe0) 0xa0 else if (c == 0xf0) 0x90 else 0x80; - const hi: u8 = if (c == 0xed) 0x9f else if (c == 0xf4) 0x8f else 0xbf; - var acc: u21 = c & (@as(u8, 0x7f) >> @intCast(want)); - while (len < want) : (len += 1) { - if (i + len == raw.len) break :feed; - const b = raw[i + len]; - if (b < (if (len == 1) lo else 0x80) or b > (if (len == 1) hi else 0xbf)) break; - acc = acc << 6 | (b & 0x3f); - } - if (len == want) cp = acc; - } - } - i += len; - // A C1 control that came as UTF-8 is dropped, as xterm drops it. - if (cp >= 0x20 and (cp < 0x80 or cp > 0x9f)) { - run[n] = cp; - n += 1; - if (n < run.len) continue; - } - h.printRun(run[0..n]); - n = 0; - if (cp < 0x20) h.execute(@intCast(cp)); - } - h.printRun(run[0..n]); - if (h.failed) return error.OutOfMemory; - const text = try h.text.toOwnedSlice(gpa); - errdefer gpa.free(text); - return .{ .text = text, .spans = try h.spans.toOwnedSlice(gpa) }; -} - -/// Where the parse is in a sequence: ECMA-48's states, the DCS's data and -/// its ignored tail one (`dcs_body`) and SOS, PM and APC one (`apc`). -const State = enum { ground, esc, esc_inter, csi_entry, csi_param, csi_inter, csi_ignore, osc, dcs_entry, dcs_param, dcs_inter, dcs_body, apc }; - -const Handler = struct { - gpa: std.mem.Allocator, - text: std.ArrayList(u8) = .empty, - spans: std.ArrayList(Span) = .empty, - /// What SGR has set so far. - style: CellStyle = .{}, - /// Where the line being printed starts, and its last character. - line_start: usize = 0, - last: usize = 0, - /// A carriage return not yet followed by a newline: the line starts - /// over at the next print, as on a terminal, so a progress line keeps - /// its last state. - cr: bool = false, - /// A backspace: the next print strikes over the character before it. - bs: bool = false, - failed: bool = false, - /// The control sequence being read: its parameters as ghostty-vt keeps - /// them (24 at most, each saturating at 65535, a bit in `colons` for - /// each one a colon ends), and whether it has an intermediate or a - /// private marker, which no sequence kept here takes. - csi: struct { - params: [24]u16 = undefined, - n: u8 = 0, - acc: u16 = 0, - digits: bool = false, - colons: u32 = 0, - inter: bool = false, - } = .{}, - - /// The state after byte `c` in `state` (not ground's printing, which - /// `parse` decodes), acting on the controls and sequences a page keeps. - fn step(h: *Handler, state: State, c: u8) State { - switch (c) { - 0x18, 0x1a => return .ground, - 0x1b => { - h.csi = .{}; - return .esc; - }, - // A C1 control byte, in all but an OSC's or a DCS's data. - 0x80...0x9f => return if (state == .osc or state == .dcs_body) state else switch (c) { - 0x90 => .dcs_entry, - 0x9b => csi: { - h.csi = .{}; - break :csi .csi_entry; - }, - 0x9d => .osc, - 0x98, 0x9e, 0x9f => .apc, - else => .ground, - }, - 0xa0...0xff => return state, - else => {}, - } - switch (state) { - .osc => return if (c == 0x07) .ground else .osc, - .apc, .dcs_body => return state, - .dcs_entry, .dcs_param, .dcs_inter => return switch (c) { - 0...0x1f, 0x7f => state, - 0x20...0x2f => .dcs_inter, - '0'...'9', ';' => if (state == .dcs_inter) .dcs_body else .dcs_param, - '<'...'?' => if (state == .dcs_entry) .dcs_param else .dcs_body, - else => .dcs_body, - }, - else => {}, - } - if (c < 0x20) { - h.execute(c); - return state; - } - if (c == 0x7f) return state; - switch (state) { - .esc => return switch (c) { - '[' => .csi_entry, - ']' => .osc, - 'P' => .dcs_entry, - 'X', '^', '_' => .apc, - 0x20...0x2f => .esc_inter, - else => .ground, - }, - .esc_inter => return if (c < 0x30) state else .ground, - .csi_ignore => return if (c < 0x40) state else .ground, - else => {}, - } - const p = &h.csi; - switch (c) { - 0x20...0x2f => { - p.inter = true; - return .csi_inter; - }, - '0'...'9', ':', ';' => { - if (state == .csi_inter or (c == ':' and state == .csi_entry)) return .csi_ignore; - if (p.n == p.params.len) return .csi_param; - if (c <= '9') { - p.acc = p.acc *| 10 +| (c - '0'); - p.digits = true; - return .csi_param; - } - p.params[p.n] = p.acc; - if (c == ':') p.colons |= @as(u32, 1) << @intCast(p.n); - p.n += 1; - p.acc = 0; - p.digits = false; - return .csi_param; - }, - '<'...'?' => { - if (state != .csi_entry) return .csi_ignore; - p.inter = true; - return .csi_param; - }, - else => { - h.dispatch(c); - return .ground; - }, - } - } - - /// A C0 control: the line feeds (LF, VT and FF), CR, BS and HT. - fn execute(h: *Handler, c: u8) void { - switch (c) { - '\n', 0x0b, 0x0c => { - h.append("\n"); - h.line_start = h.text.items.len; - h.last = h.line_start; - h.cr = false; - h.bs = false; - }, - '\r' => h.cr = true, - 0x08 => h.bs = h.text.items.len > h.line_start, - '\t' => h.print('\t'), - else => {}, - } - } - - /// The control sequence ending in `f`: SGR, and of the rest what moves - /// along a line, dropped as ghostty-vt drops a malformed one. - fn dispatch(h: *Handler, f: u8) void { - const p = &h.csi; - if (p.n == p.params.len) return; - if (p.digits) { - p.params[p.n] = p.acc; - p.n += 1; - } - if (p.inter or (f != 'm' and p.colons != 0)) return; - if (f == 'm') return h.sgr(p.params[0..p.n], p.colons); - if (p.n > 1) return; - const count: u16 = if (p.n == 0) 1 else p.params[0]; - switch (f) { - // CNL and CPL: down or up, which a page has not, and a return. - 'E', 'F' => h.cr = true, - // CHT; and REP, the last character again, as a terminal draws - // it: a terminal stops at its edge, and this at a line's worth. - 'I' => for (0..@min(count, 64)) |_| h.print('\t'), - 'b' => if (h.text.items.len > h.last) { - const cp = std.unicode.utf8Decode(h.text.items[h.last..]) catch return; - for (0..@min(count, 1024)) |_| h.print(cp); - }, - else => {}, - } - } - - /// SGR's parameters into the style, read as ghostty-vt's sgr.Parser - /// reads them: a colour short of its values leaves them to be read as - /// SGRs of their own, and a colon list it does not know is skipped. - fn sgr(h: *Handler, params: []const u16, colons: u32) void { - const s = &h.style; - if (params.len == 0) s.* = .{}; - var i: usize = 0; - while (i < params.len) { - const p = params[i..]; - const colon = colons >> @intCast(i) & 1 != 0; - i += 1; - // The colons on from p[1], short of the last parameter. - var subs: usize = 0; - while (i + subs < params.len - 1 and colons >> @intCast(i + subs) & 1 != 0) subs += 1; - if (colon and p[0] != 4 and p[0] != 38 and p[0] != 48 and p[0] != 58) { - while (colons >> @intCast(i) & 1 != 0) i += 1; - i += 1; - continue; - } - switch (p[0]) { - 0 => s.* = .{}, - 1 => s.bold = true, - 2 => s.dim = true, - 3 => s.italic = true, - 4 => if (!colon) { - s.ul = .single; - } else if (p.len >= 2) { - if (colons >> @intCast(i) & 1 != 0) { - i += subs + 1; - } else { - i += 1; - s.ul = if (p[1] <= 5) @enumFromInt(p[1]) else .single; - } - }, - 5, 6 => s.blink = true, - 7 => s.reverse = true, - 8 => s.invisible = true, - 9 => s.strikethrough = true, - 21 => s.ul = .double, - // SGR 22 ends faint as well as bold. - 22 => { - s.bold = false; - s.dim = false; - }, - 23 => s.italic = false, - 24 => s.ul = .off, - 25 => s.blink = false, - 27 => s.reverse = false, - 28 => s.invisible = false, - 29 => s.strikethrough = false, - 30...37 => s.fg = .{ .index = @intCast(p[0] - 30) }, - 39 => s.fg = .default, - 40...47 => s.bg = .{ .index = @intCast(p[0] - 40) }, - 49 => s.bg = .default, - 90...97 => s.fg = .{ .index = @intCast(p[0] - 82) }, - 100...107 => s.bg = .{ .index = @intCast(p[0] - 92) }, - // A colour: 5 and an index, or 2 and red, green and blue, - // after a colour space's slot when colons give one. A cell - // has no underline colour (58) to take. - 38, 48, 58 => if (p.len >= 2) { - var color: ?Color = null; - if (p[1] == 5 and p.len >= 3) { - i += 2; - color = .{ .index = @truncate(p[2]) }; - } else if (p[1] == 2 and p.len >= 5) { - if (colon and subs != 3 and subs != 4) { - i += subs + 1; - continue; - } - const at: usize = if (colon and subs == 4) 3 else 2; - i += at + 2; - color = .{ .rgb = .{ @truncate(p[at]), @truncate(p[at + 1]), @truncate(p[at + 2]) } }; - } - if (color) |ink| switch (p[0]) { - 38 => s.fg = ink, - 48 => s.bg = ink, - else => {}, - }; - }, - else => {}, - } - } - } - - // ponytail: bytes that are not UTF-8 reach here as U+FFFD (`parse` - // replaces them, as a terminal shows them); a raw-byte pass if a - // +Pager must keep them. - fn print(h: *Handler, cp: u21) void { - // A control a terminal acts on and never draws: DEL comes in a run - // of printed text, as a terminal's parse hands it on. A tab is - // HT's, printed here. - if ((cp < 0x20 and cp != '\t') or (cp >= 0x7f and cp < 0xa0)) return; - if (h.cr) { - h.text.shrinkRetainingCapacity(h.line_start); - h.cut(h.line_start); - h.last = h.line_start; - h.cr = false; - h.bs = false; - } - var buf: [4]u8 = undefined; - const n = std.unicode.utf8Encode(cp, &buf) catch std.unicode.utf8Encode(0xfffd, &buf) catch unreachable; - if (h.bs) { - // A man page's overstrike: a character struck over itself is - // bold, one struck with `_` (either way round) is underlined; - // any other is replaced, as on a terminal. - h.bs = false; - const prev = h.text.items[h.last..]; - var struck = h.style; - if (std.mem.eql(u8, prev, buf[0..n])) { - struck.bold = true; - } else if (std.mem.eql(u8, prev, "_")) { - h.text.shrinkRetainingCapacity(h.last); - h.append(buf[0..n]); - struck.ul = .single; - } else if (cp == '_') { - struck.ul = .single; - } else { - h.text.shrinkRetainingCapacity(h.last); - h.cut(h.last); - h.last = h.text.items.len; - h.append(buf[0..n]); - h.span(h.last, h.style); - return; - } - h.cut(h.last); - h.span(h.last, struck); - return; - } - h.last = h.text.items.len; - h.append(buf[0..n]); - h.span(h.last, h.style); - } - - /// A run of printed characters (most of a page): the first as `print` - /// takes it, a pending return or backspace and all, the rest straight - /// on, as one span. A 256 MiB page goes through here. - fn printRun(h: *Handler, cps: []const u32) void { - if (cps.len == 0) return; - h.print(@intCast(cps[0])); - const rest = cps[1..]; - if (rest.len == 0) return; - var need: usize = 0; - for (rest) |cp| need += if (cp < 0x80) 1 else std.unicode.utf8CodepointSequenceLength(@intCast(cp)) catch 3; - h.text.ensureUnusedCapacity(h.gpa, need) catch { - h.failed = true; - return; - }; - const from = h.text.items.len; - for (rest) |cp| { - if (cp < 0x20 or (cp >= 0x7f and cp < 0xa0)) continue; - h.last = h.text.items.len; - if (cp < 0x80) { - h.text.appendAssumeCapacity(@intCast(cp)); - continue; - } - var buf: [4]u8 = undefined; - const n = std.unicode.utf8Encode(@intCast(cp), &buf) catch std.unicode.utf8Encode(0xfffd, &buf) catch unreachable; - h.text.appendSliceAssumeCapacity(buf[0..n]); - } - h.span(from, h.style); - } - - fn append(h: *Handler, bytes: []const u8) void { - h.text.appendSlice(h.gpa, bytes) catch { - h.failed = true; - }; - } - - /// `from` to the text's end drawn in `style`: the span before it goes - /// on when it ends there in the same style. - fn span(h: *Handler, from: usize, style: CellStyle) void { - if (std.meta.eql(style, CellStyle{})) return; - const end: u32 = @intCast(h.text.items.len); - if (h.spans.items.len > 0) { - const tail = &h.spans.items[h.spans.items.len - 1]; - if (tail.end == from and std.meta.eql(tail.style, style)) { - tail.end = end; - return; - } - } - if (h.spans.items.len >= max_spans) return; - h.spans.append(h.gpa, .{ .start = @intCast(from), .end = end, .style = style }) catch { - h.failed = true; - }; - } - - /// No span reaches past `at` any more: the text there is gone. - fn cut(h: *Handler, at: usize) void { - while (h.spans.items.len > 0) { - const tail = &h.spans.items[h.spans.items.len - 1]; - if (tail.end <= at) return; - if (tail.start < at) { - tail.end = @intCast(at); - return; - } - _ = h.spans.pop(); - } - } -}; - -// ---- tests ---- - -const testing = std.testing; - -/// The text `raw` pages to, and its spans as `text[start..end]=style` lines. -fn expectPage(raw: []const u8, text: []const u8, spans: []const struct { []const u8, CellStyle }) !void { - const page = try parse(testing.allocator, raw); - defer page.deinit(testing.allocator); - try testing.expectEqualStrings(text, page.text); - for (spans, 0..) |want, i| { - if (i >= page.spans.len) { - std.debug.print("missing span {d}: \"{s}\"\n", .{ i, want[0] }); - return error.TestExpectedEqual; - } - const got = page.spans[i]; - try testing.expectEqualStrings(want[0], page.text[got.start..got.end]); - try testing.expectEqualDeep(want[1], got.style); - } - try testing.expectEqual(spans.len, page.spans.len); -} - -test "git log --color: the commit line yellow, the rest plain" { - try expectPage( - "\x1b[33mcommit 0123abc\x1b[m\x1b[33m (\x1b[m\x1b[1;36mHEAD -> \x1b[m\x1b[1;32mmain\x1b[m\x1b[33m)\x1b[m\nAuthor: a <a@x>\n\n subject\n", - "commit 0123abc (HEAD -> main)\nAuthor: a <a@x>\n\n subject\n", - &.{ - .{ "commit 0123abc (", .{ .fg = .{ .index = 3 } } }, - .{ "HEAD -> ", .{ .fg = .{ .index = 6 }, .bold = true } }, - .{ "main", .{ .fg = .{ .index = 2 }, .bold = true } }, - .{ ")", .{ .fg = .{ .index = 3 } } }, - }, - ); -} - -test "ls --color=always: 01;34 directories and 01;32 executables, reset by 0 and none" { - try expectPage( - "\x1b[0m\x1b[01;34mdir\x1b[0m\n\x1b[01;32mrun.sh\x1b[0m\nplain.txt\n", - "dir\nrun.sh\nplain.txt\n", - &.{ - .{ "dir", .{ .fg = .{ .index = 4 }, .bold = true } }, - .{ "run.sh", .{ .fg = .{ .index = 2 }, .bold = true } }, - }, - ); -} - -test "grep --color=always: the match, the file name and the separator, and EL dropped" { - try expectPage( - "\x1b[35m\x1b[Ka.txt\x1b[m\x1b[K\x1b[36m\x1b[K:\x1b[m\x1b[Kone \x1b[01;31m\x1b[Kfoo\x1b[m\x1b[K two\n", - "a.txt:one foo two\n", - &.{ - .{ "a.txt", .{ .fg = .{ .index = 5 } } }, - .{ ":", .{ .fg = .{ .index = 6 } } }, - .{ "foo", .{ .fg = .{ .index = 1 }, .bold = true } }, - }, - ); -} - -test "a man page's overstrike: bold and underline, and a stray backspace" { - try expectPage( - "N\x08NA\x08AM\x08ME\x08E _\x08u_\x08s x\x08_ a\x08b\x08\n", - "NAME us x b\n", - &.{ - .{ "NAME", .{ .bold = true } }, - .{ "us", .{ .ul = .single } }, - .{ "x", .{ .ul = .single } }, - }, - ); -} - -test "truecolor, 256 colours, background, italic, faint, inverse and curly underline" { - try expectPage( - "\x1b[38;2;255;128;0mo\x1b[48;5;236mb\x1b[0;3;2mi\x1b[7mr\x1b[0;4:3mc\x1b[m.\n", - "obirc.\n", - &.{ - .{ "o", .{ .fg = .{ .rgb = .{ 255, 128, 0 } } } }, - .{ "b", .{ .fg = .{ .rgb = .{ 255, 128, 0 } }, .bg = .{ .index = 236 } } }, - .{ "i", .{ .italic = true, .dim = true } }, - .{ "r", .{ .italic = true, .dim = true, .reverse = true } }, - .{ "c", .{ .ul = .curly } }, - }, - ); -} - -test "every other escape is dropped, and a stray or broken one loses no text after it" { - const cases = [_][2][]const u8{ - .{ "a\x1b]8;;http://x\x1b\\link\x1b]8;;\x07b", "alinkb" }, - .{ "\x1b]0;title\x07\x1b[2J\x1b[H\x1b[?1049hdone\n", "done\n" }, - .{ "\x1b(Bdone", "done" }, - .{ "plain\ttext\n", "plain\ttext\n" }, - .{ "a\r\nb\r\n", "a\nb\n" }, - .{ "10%\r50%\r\x1b[K100%\ndone\n", "100%\ndone\n" }, - .{ "x\x00y\x07z\x0e\x0f\n", "xyz\n" }, - // Controls that are dropped: CAN, FS, DEL and a C1 as UTF-8. - .{ "x\x18y\x1cz\x7f!\u{85}?\n", "xyz!?\n" }, - // A broken CSI: its parameter bytes are eaten up to a final byte, as - // a terminal eats them; CAN abandons one, and the text goes on. - .{ "a\x1b[31;\x18b\x1b[m\n", "ab\n" }, - // An unknown SGR changes nothing; an ESC at the very end is dropped. - .{ "\x1b[99mz\x1b", "z" }, - }; - for (cases) |c| try expectPage(c[0], c[1], &.{}); -} - -test "bytes that are not UTF-8 page as U+FFFD, the text around them kept" { - try expectPage("caf\xe9 \xff\xfe ok\n", "caf\u{fffd} \u{fffd}\u{fffd} ok\n", &.{}); -} - -test "a carriage return's rewrite takes the spans of what it rewrote with it" { - try expectPage("\x1b[31m10%\r\x1b[32m100%\x1b[m\n", "100%\n", &.{.{ "100%", .{ .fg = .{ .index = 2 } } }}); -} - -test "colon colours, an 8-bit CSI, REP, CHT, CNL, a colour short of its values and UTF-8 cut short" { - try expectPage("\x1b[38:2::1:2:3mx\x1b[48:5:7my\x1b[m", "xy", &.{ - .{ "x", .{ .fg = .{ .rgb = .{ 1, 2, 3 } } } }, - .{ "y", .{ .fg = .{ .rgb = .{ 1, 2, 3 } }, .bg = .{ .index = 7 } } }, - }); - // 0x9b in text is a stray byte, U+FFFD; in a sequence (after ESC - // here) it is CSI; as UTF-8 (C2 9B) it is a C1 control, dropped. - try expectPage("x\x9b\x1b\x9b31mr\xc2\x9b32mg", "x\u{fffd}r32mg", &.{.{ "r32mg", .{ .fg = .{ .index = 1 } } }}); - // REP and CHT go on along the line; CNL returns, and `d` starts it over. - try expectPage("ab\x1b[3b\x1b[2Ic\n", "abbbb\t\tc\n", &.{}); - try expectPage("ab\x1b[Ed\n", "d\n", &.{}); - // 38;5 with no index: 38 is unknown and 5 is blink. - try expectPage("\x1b[38;5mx", "x", &.{.{ "x", .{ .blink = true } }}); - try expectPage("ok\xe2\x82", "ok", &.{}); - try expectPage("\xe2\x82x", "\u{fffd}x", &.{}); -} diff --git a/src/pardes.zig b/src/pardes.zig index 13456de6..f8f49fb3 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -13,7 +13,6 @@ pub const ctlfs = @import("ninep/tree.zig"); pub const syntax = @import("syntax.zig"); const tracy = @import("tracy.zig"); pub const panes = @import("panes.zig"); -pub const pager = @import("pager.zig"); const locations = @import("locations.zig"); pub const tagline = @import("tagline.zig"); pub const exec = @import("exec.zig"); @@ -394,6 +393,7 @@ test { _ = @import("tape.zig"); _ = @import("replica.zig"); _ = @import("term/echo.zig"); + _ = Term; _ = @import("modal.zig"); _ = @import("tagline.zig"); _ = @import("hunks.zig"); @@ -421,7 +421,6 @@ test { _ = @import("recent.zig"); _ = @import("Store.zig"); _ = @import("Db.zig"); - _ = @import("pager.zig"); _ = @import("Places.zig"); _ = @import("FrameStats.zig"); _ = @import("Query.zig"); diff --git a/src/term/pager.zig b/src/term/pager.zig new file mode 100644 index 00000000..dca57024 --- /dev/null +++ b/src/term/pager.zig @@ -0,0 +1,371 @@ +//! A program's output, paged into a +Pager (`pardes -`): the text it +//! prints, and its SGR colours as spans over that text. ghostty-vt, which +//! terminal panes already drive, parses every escape; this keeps what is +//! printed and how SGR styles it, and drops every other control (cursor +//! motion, OSC titles and hyperlinks, modes). The spans are display state +//! of the pane only: no 9P file reads them (File.recolorSyntax draws them). +const std = @import("std"); +const pardes = @import("../pardes.zig"); +const CellStyle = @import("../surface.zig").CellStyle; +const ghostty_vt = @import("ghostty-vt"); + +/// Bytes `start..end` of the text, drawn in `style`. A palette colour stays +/// `.index`, resolved through the theme as it is drawn (terminal.palColor), +/// and `.default` is the pane's own. +pub const Span = struct { start: u32, end: u32, style: CellStyle }; + +/// ponytail: past this many spans the rest of a page is plain, rather than +/// ~100 MB of spans for a 256 MiB coloured log; a packed style if it matters. +pub const max_spans = 4 << 20; + +pub const Page = struct { + text: []u8, + spans: []Span, + + pub fn deinit(page: Page, gpa: std.mem.Allocator) void { + gpa.free(page.text); + gpa.free(page.spans); + } +}; + +/// `raw`, a program's output for a terminal, as text and its spans. Both +/// are `gpa`'s. +pub fn parse(gpa: std.mem.Allocator, raw: []const u8) !Page { + var h: Handler = .{ .gpa = gpa }; + errdefer h.text.deinit(gpa); + errdefer h.spans.deinit(gpa); + try h.text.ensureTotalCapacity(gpa, raw.len); + var stream: ghostty_vt.Stream(*Handler) = .init(.{ .handler = &h }); + defer stream.deinit(); + stream.nextSlice(raw); + if (h.failed) return error.OutOfMemory; + const text = try h.text.toOwnedSlice(gpa); + errdefer gpa.free(text); + return .{ .text = text, .spans = try h.spans.toOwnedSlice(gpa) }; +} + +const Handler = struct { + gpa: std.mem.Allocator, + text: std.ArrayList(u8) = .empty, + spans: std.ArrayList(Span) = .empty, + /// What SGR has set so far. + style: CellStyle = .{}, + /// Where the line being printed starts, and its last character. + line_start: usize = 0, + last: usize = 0, + /// A carriage return not yet followed by a newline: the line starts + /// over at the next print, as on a terminal, so a progress line keeps + /// its last state. + cr: bool = false, + /// A backspace: the next print strikes over the character before it. + bs: bool = false, + failed: bool = false, + + const Action = ghostty_vt.StreamAction; + + pub fn vt(h: *Handler, comptime action: Action.Tag, value: Action.Value(action)) void { + switch (action) { + .print => h.print(value.cp), + .print_slice => h.printRun(value.cps), + // CSI b repeats the last character, as a terminal draws it; + // a terminal stops at its edge, and this at a line's worth. + .print_repeat => if (h.text.items.len > h.last) { + const cp = std.unicode.utf8Decode(h.text.items[h.last..]) catch return; + for (0..@min(value, 1024)) |_| h.print(cp); + }, + .set_attribute => h.attribute(value), + .linefeed => { + h.append("\n"); + h.line_start = h.text.items.len; + h.last = h.line_start; + h.cr = false; + h.bs = false; + }, + .carriage_return => h.cr = true, + .backspace => h.bs = h.text.items.len > h.line_start, + .horizontal_tab => for (0..@min(value, 64)) |_| h.print('\t'), + else => {}, + } + } + + pub fn deinit(_: *Handler) void {} + + // ponytail: bytes that are not UTF-8 reach here as U+FFFD (ghostty's + // decoder replaces them, as a terminal shows them); a raw-byte pass + // through the stream if a +Pager must keep them. + fn print(h: *Handler, cp: u21) void { + // A control a terminal acts on and never draws: ghostty's fast path + // hands those past 0x0f (CAN, DEL, the C1 set) on as printed text. + // A tab is HT's, printed here. + if ((cp < 0x20 and cp != '\t') or (cp >= 0x7f and cp < 0xa0)) return; + if (h.cr) { + h.text.shrinkRetainingCapacity(h.line_start); + h.cut(h.line_start); + h.last = h.line_start; + h.cr = false; + h.bs = false; + } + var buf: [4]u8 = undefined; + const n = std.unicode.utf8Encode(cp, &buf) catch std.unicode.utf8Encode(0xfffd, &buf) catch unreachable; + if (h.bs) { + // A man page's overstrike: a character struck over itself is + // bold, one struck with `_` (either way round) is underlined; + // any other is replaced, as on a terminal. + h.bs = false; + const prev = h.text.items[h.last..]; + var struck = h.style; + if (std.mem.eql(u8, prev, buf[0..n])) { + struck.bold = true; + } else if (std.mem.eql(u8, prev, "_")) { + h.text.shrinkRetainingCapacity(h.last); + h.append(buf[0..n]); + struck.ul = .single; + } else if (cp == '_') { + struck.ul = .single; + } else { + h.text.shrinkRetainingCapacity(h.last); + h.cut(h.last); + h.last = h.text.items.len; + h.append(buf[0..n]); + h.span(h.last, h.style); + return; + } + h.cut(h.last); + h.span(h.last, struck); + return; + } + h.last = h.text.items.len; + h.append(buf[0..n]); + h.span(h.last, h.style); + } + + /// A run of printed characters (most of a page): the first as `print` + /// takes it, a pending return or backspace and all, the rest straight + /// on, as one span. A 256 MiB page goes through here. + fn printRun(h: *Handler, cps: []const u32) void { + if (cps.len == 0) return; + h.print(@intCast(cps[0])); + const rest = cps[1..]; + if (rest.len == 0) return; + var need: usize = 0; + for (rest) |cp| need += if (cp < 0x80) 1 else std.unicode.utf8CodepointSequenceLength(@intCast(cp)) catch 3; + h.text.ensureUnusedCapacity(h.gpa, need) catch { + h.failed = true; + return; + }; + const from = h.text.items.len; + for (rest) |cp| { + if (cp < 0x20 or (cp >= 0x7f and cp < 0xa0)) continue; + h.last = h.text.items.len; + if (cp < 0x80) { + h.text.appendAssumeCapacity(@intCast(cp)); + continue; + } + var buf: [4]u8 = undefined; + const n = std.unicode.utf8Encode(@intCast(cp), &buf) catch std.unicode.utf8Encode(0xfffd, &buf) catch unreachable; + h.text.appendSliceAssumeCapacity(buf[0..n]); + } + h.span(from, h.style); + } + + fn append(h: *Handler, bytes: []const u8) void { + h.text.appendSlice(h.gpa, bytes) catch { + h.failed = true; + }; + } + + /// `from` to the text's end drawn in `style`: the span before it goes + /// on when it ends there in the same style. + fn span(h: *Handler, from: usize, style: CellStyle) void { + if (std.meta.eql(style, CellStyle{})) return; + const end: u32 = @intCast(h.text.items.len); + if (h.spans.items.len > 0) { + const tail = &h.spans.items[h.spans.items.len - 1]; + if (tail.end == from and std.meta.eql(tail.style, style)) { + tail.end = end; + return; + } + } + if (h.spans.items.len >= max_spans) return; + h.spans.append(h.gpa, .{ .start = @intCast(from), .end = end, .style = style }) catch { + h.failed = true; + }; + } + + /// No span reaches past `at` any more: the text there is gone. + fn cut(h: *Handler, at: usize) void { + while (h.spans.items.len > 0) { + const tail = &h.spans.items[h.spans.items.len - 1]; + if (tail.end <= at) return; + if (tail.start < at) { + tail.end = @intCast(at); + return; + } + _ = h.spans.pop(); + } + } + + fn attribute(h: *Handler, attr: ghostty_vt.Attribute) void { + const s = &h.style; + switch (attr) { + .unset => s.* = .{}, + .bold => s.bold = true, + // SGR 22 ends faint as well as bold. + .reset_bold => { + s.bold = false; + s.dim = false; + }, + .faint => s.dim = true, + .italic => s.italic = true, + .reset_italic => s.italic = false, + // SGR's styles in a cell's order (terminal.zig checks it). + .underline => |u| s.ul = @enumFromInt(@intFromEnum(u)), + .blink => s.blink = true, + .reset_blink => s.blink = false, + .inverse => s.reverse = true, + .reset_inverse => s.reverse = false, + .invisible => s.invisible = true, + .reset_invisible => s.invisible = false, + .strikethrough => s.strikethrough = true, + .reset_strikethrough => s.strikethrough = false, + .direct_color_fg => |c| s.fg = .{ .rgb = .{ c.r, c.g, c.b } }, + .direct_color_bg => |c| s.bg = .{ .rgb = .{ c.r, c.g, c.b } }, + .@"8_fg", .@"8_bright_fg" => |name| s.fg = .{ .index = @backingInt(name) }, + .@"8_bg", .@"8_bright_bg" => |name| s.bg = .{ .index = @backingInt(name) }, + .@"256_fg" => |i| s.fg = .{ .index = i }, + .@"256_bg" => |i| s.bg = .{ .index = i }, + .reset_fg => s.fg = .default, + .reset_bg => s.bg = .default, + // A cell has no overline and no underline colour to take. + .overline, .reset_overline, .underline_color, .@"256_underline_color", .reset_underline_color, .unknown => {}, + } + } +}; + +// ---- tests ---- + +const testing = std.testing; + +/// The text `raw` pages to, and its spans as `text[start..end]=style` lines. +fn expectPage(raw: []const u8, text: []const u8, spans: []const struct { []const u8, CellStyle }) !void { + const page = try parse(testing.allocator, raw); + defer page.deinit(testing.allocator); + try testing.expectEqualStrings(text, page.text); + for (spans, 0..) |want, i| { + if (i >= page.spans.len) { + std.debug.print("missing span {d}: \"{s}\"\n", .{ i, want[0] }); + return error.TestExpectedEqual; + } + const got = page.spans[i]; + try testing.expectEqualStrings(want[0], page.text[got.start..got.end]); + try testing.expectEqualDeep(want[1], got.style); + } + try testing.expectEqual(spans.len, page.spans.len); +} + +test "git log --color: the commit line yellow, the rest plain" { + try expectPage( + "\x1b[33mcommit 0123abc\x1b[m\x1b[33m (\x1b[m\x1b[1;36mHEAD -> \x1b[m\x1b[1;32mmain\x1b[m\x1b[33m)\x1b[m\nAuthor: a <a@x>\n\n subject\n", + "commit 0123abc (HEAD -> main)\nAuthor: a <a@x>\n\n subject\n", + &.{ + .{ "commit 0123abc (", .{ .fg = .{ .index = 3 } } }, + .{ "HEAD -> ", .{ .fg = .{ .index = 6 }, .bold = true } }, + .{ "main", .{ .fg = .{ .index = 2 }, .bold = true } }, + .{ ")", .{ .fg = .{ .index = 3 } } }, + }, + ); +} + +test "ls --color=always: 01;34 directories and 01;32 executables, reset by 0 and none" { + try expectPage( + "\x1b[0m\x1b[01;34mdir\x1b[0m\n\x1b[01;32mrun.sh\x1b[0m\nplain.txt\n", + "dir\nrun.sh\nplain.txt\n", + &.{ + .{ "dir", .{ .fg = .{ .index = 4 }, .bold = true } }, + .{ "run.sh", .{ .fg = .{ .index = 2 }, .bold = true } }, + }, + ); +} + +test "grep --color=always: the match, the file name and the separator, and EL dropped" { + try expectPage( + "\x1b[35m\x1b[Ka.txt\x1b[m\x1b[K\x1b[36m\x1b[K:\x1b[m\x1b[Kone \x1b[01;31m\x1b[Kfoo\x1b[m\x1b[K two\n", + "a.txt:one foo two\n", + &.{ + .{ "a.txt", .{ .fg = .{ .index = 5 } } }, + .{ ":", .{ .fg = .{ .index = 6 } } }, + .{ "foo", .{ .fg = .{ .index = 1 }, .bold = true } }, + }, + ); +} + +test "a man page's overstrike: bold and underline, and a stray backspace" { + try expectPage( + "N\x08NA\x08AM\x08ME\x08E _\x08u_\x08s x\x08_ a\x08b\x08\n", + "NAME us x b\n", + &.{ + .{ "NAME", .{ .bold = true } }, + .{ "us", .{ .ul = .single } }, + .{ "x", .{ .ul = .single } }, + }, + ); +} + +test "truecolor, 256 colours, background, italic, faint, inverse and curly underline" { + try expectPage( + "\x1b[38;2;255;128;0mo\x1b[48;5;236mb\x1b[0;3;2mi\x1b[7mr\x1b[0;4:3mc\x1b[m.\n", + "obirc.\n", + &.{ + .{ "o", .{ .fg = .{ .rgb = .{ 255, 128, 0 } } } }, + .{ "b", .{ .fg = .{ .rgb = .{ 255, 128, 0 } }, .bg = .{ .index = 236 } } }, + .{ "i", .{ .italic = true, .dim = true } }, + .{ "r", .{ .italic = true, .dim = true, .reverse = true } }, + .{ "c", .{ .ul = .curly } }, + }, + ); +} + +test "every other escape is dropped, and a stray or broken one loses no text after it" { + const cases = [_][2][]const u8{ + .{ "a\x1b]8;;http://x\x1b\\link\x1b]8;;\x07b", "alinkb" }, + .{ "\x1b]0;title\x07\x1b[2J\x1b[H\x1b[?1049hdone\n", "done\n" }, + .{ "\x1b(Bdone", "done" }, + .{ "plain\ttext\n", "plain\ttext\n" }, + .{ "a\r\nb\r\n", "a\nb\n" }, + .{ "10%\r50%\r\x1b[K100%\ndone\n", "100%\ndone\n" }, + .{ "x\x00y\x07z\x0e\x0f\n", "xyz\n" }, + // Controls ghostty's fast path prints: CAN, FS, DEL and a C1. + .{ "x\x18y\x1cz\x7f!\u{85}?\n", "xyz!?\n" }, + // A broken CSI: its parameter bytes are eaten up to a final byte, as + // a terminal eats them; CAN abandons one, and the text goes on. + .{ "a\x1b[31;\x18b\x1b[m\n", "ab\n" }, + // An unknown SGR changes nothing; an ESC at the very end is dropped. + .{ "\x1b[99mz\x1b", "z" }, + }; + for (cases) |c| try expectPage(c[0], c[1], &.{}); +} + +test "bytes that are not UTF-8 page as U+FFFD, the text around them kept" { + try expectPage("caf\xe9 \xff\xfe ok\n", "caf\u{fffd} \u{fffd}\u{fffd} ok\n", &.{}); +} + +test "a carriage return's rewrite takes the spans of what it rewrote with it" { + try expectPage("\x1b[31m10%\r\x1b[32m100%\x1b[m\n", "100%\n", &.{.{ "100%", .{ .fg = .{ .index = 2 } } }}); +} + +test "colon colours, an 8-bit CSI, REP, CHT, CNL, a colour short of its values and UTF-8 cut short" { + try expectPage("\x1b[38:2::1:2:3mx\x1b[48:5:7my\x1b[m", "xy", &.{ + .{ "x", .{ .fg = .{ .rgb = .{ 1, 2, 3 } } } }, + .{ "y", .{ .fg = .{ .rgb = .{ 1, 2, 3 } }, .bg = .{ .index = 7 } } }, + }); + // 0x9b in text is a stray byte, U+FFFD; in a sequence (after ESC + // here) it is CSI; as UTF-8 (C2 9B) it is a C1 control, dropped. + try expectPage("x\x9b\x1b\x9b31mr\xc2\x9b32mg", "x\u{fffd}r32mg", &.{.{ "r32mg", .{ .fg = .{ .index = 1 } } }}); + // REP and CHT go on along the line; CNL returns, and `d` starts it over. + try expectPage("ab\x1b[3b\x1b[2Ic\n", "abbbb\t\tc\n", &.{}); + try expectPage("ab\x1b[Ed\n", "d\n", &.{}); + // 38;5 with no index: 38 is unknown and 5 is blink. + try expectPage("\x1b[38;5mx", "x", &.{.{ "x", .{ .blink = true } }}); + try expectPage("ok\xe2\x82", "ok", &.{}); + try expectPage("\xe2\x82x", "\u{fffd}x", &.{}); +} diff --git a/src/term/terminal.zig b/src/term/terminal.zig index 2c05cd3c..70cd4e9d 100644 --- a/src/term/terminal.zig +++ b/src/term/terminal.zig @@ -1397,7 +1397,7 @@ fn cellAt(pages: *const ghostty_vt.PageList, row: ghostty_vt.Pin, x: u16) ?ghost } // A cell's underline styles are SGR's, in SGR's order (`off` its `none`): a -// cast takes one to the other (cellStyle, pager.zig). +// cast takes one to the other (cellStyle, and pager.zig's attribute). comptime { for (@typeInfo(@FieldType(pardes.CellStyle, "ul")).@"enum".field_names, 0..) |name, i| if (@backingInt(@field(ghostty_vt.sgr.Attribute.Underline, if (std.mem.eql(u8, name, "off")) "none" else name)) != i) @compileError("a cell's underline styles are not SGR's"); @@ -1524,7 +1524,7 @@ pub const FilteredColors = struct { return asPardesColor(self.keyedRgb(style.bg(cell, &self.source).?)); } - /// A +Pager span's colour (pager.zig), as `fg` and `bg` map a cell's. + /// A +Pager span's colour (Term.page), as `fg` and `bg` map a cell's. pub fn spanColor(self: *FilteredColors, color: pardes.Color, is_bg: bool) pardes.Color { return switch (color) { .default => color, |
