//! The frame: how the core builds the canonical Surface every shell draws. //! `render` is the whole order, top to bottom: the page, each pane (its //! grip, tag and body, and the body layer of its compact rows), the notice //! chips, the workspace and column tags, the tag layers, drag overlays, the //! debug box, then the cursor. const std = @import("std"); const vaxis = @import("vaxis"); const pardes = @import("root.zig"); const layout = @import("layout.zig"); const config = @import("config.zig"); const panes = @import("panes.zig"); const modal = @import("modal.zig"); const tracy = @import("tracy.zig"); const colors = @import("colors.zig"); const edit = @import("edit.zig"); const tagline = @import("tagline.zig"); const body_layer = @import("body_layer.zig"); const Messages = @import("Messages.zig"); const TagState = @import("TagState.zig"); const builtins = @import("builtins.zig"); const Color = @import("surface.zig").Color; const FontRole = @import("surface.zig").FontRole; const CellStyle = @import("surface.zig").CellStyle; const Cell = @import("surface.zig").Cell; const Surface = @import("surface.zig").Surface; const Pardes = pardes.Pardes; const Pane = panes.Pane; const Text = panes.Text; const Rect = layout.Rect; const Region = @import("surface.zig").Region; const TagKind = @import("Layer.zig").Kind; const wordBoundsAtCol = tagline.wordBoundsAtCol; const BOX_H = pardes.BOX_H; const TAG_TEXT_INSET = pardes.TAG_TEXT_INSET; const MAX_PANES = pardes.MAX_PANES; const MAX_COLS = pardes.MAX_COLS; const MAX_TAG_ROWS = pardes.MAX_TAG_ROWS; const NOTICE_LAYER_BASE = pardes.NOTICE_LAYER_BASE; /// What a frontend reads beside the cells (surface.Facts), of the live /// view as it is now: with each frame (`place`), and by a runner after an /// input it applied, outside any step, writing nothing of the core's. pub fn facts(p: *const Pardes, f: *pardes.Facts) void { viewFacts(p, f); for (p.panes, 0..) |slot, id| if (slot) |pane| paneFacts(p, f, id, pane); } /// The view's facts; its panes' table emptied (`paneFacts` fills it). fn viewFacts(p: *const Pardes, f: *pardes.Facts) void { const v = p.view(); // Field by field: the panes' table is written only where a pane is. f.screen_w = v.screen_w; f.screen_h = v.screen_h; f.cell_pixels = v.cell_pixels; f.row_metrics = v.row_metrics; f.open = 0; f.active = @intCast(v.active); f.back = v.jumps.n > 0 and v.jumps.cur > 0; f.forward = v.jumps.n > 0 and v.jumps.cur + 1 < v.jumps.n; f.cursor_blink = p.settings.cursor_blink; f.jump_trail = p.settings.jump_trail; f.pet = p.settings.pet; f.inactive_dim = p.settings.inactive_dim; f.tagline_percent = p.settings.font.tagline_percent; } /// Open pane `id`'s facts. fn paneFacts(p: *const Pardes, f: *pardes.Facts, id: usize, pane: *const Pane) void { f.open |= @as(u64, 1) << @intCast(id); f.panes[id] = .{ .rect = p.view().rects[id], .tag_rows = pane.tag_rows, .context_rows = pane.context_rows, .serial = pane.serial, .kind = if (pane.addon != null) .addon else if (pane.isTerminal()) .terminal else .text, .program_wheel = pane.isTerminal() and panes.Term.modes(pane.terminal).wheel, .normal = pane.body.mode == .normal, }; } /// Where every part of the frame goes, decided once before anything is /// painted: the page; each pane's grip, tag, body, rail (with its thumb) /// and notice chips; each column's grip and tag; the workspace tag. The /// painters below and every shell read these rectangles. pub fn place(p: *Pardes, s: *Surface) void { const v = p.view(); s.nregions = 0; s.nrail_ticks = 0; s.lanes.clearRetainingCapacity(); s.heat.clearRetainingCapacity(); // The facts filled in the walk the frame's places take anyway. viewFacts(p, &p.facts); s.facts = &p.facts; s.addRegion(.{ .kind = .page, .rect = .{ .x = 0, .y = 0, .w = s.cols, .h = s.rows } }); for (&p.panes, 0..) |*slot, id| { const pane = slot.* orelse continue; paneFacts(p, &p.facts, id, pane); Messages.collectNotices(p, pane, v.rects[id]); const r = v.rects[id]; if (r.w <= config.GUTTER or r.h == 0) continue; const owner: u8 = @intCast(id); const active = id == v.active; const tag_y = p.tagTop(pane, r); const body_y = p.bodyTop(pane, r); const reading = pardes.Trail.reading(p, pane); s.addRegion(.{ .kind = .grip, .owner = owner, .serial = pane.serial, .active = active, .dirty = tagline.dirty(pane), .reading = reading, .rect = .{ .x = r.x, .y = tag_y, .w = config.GUTTER, .h = pane.tag_rows } }); s.addRegion(.{ .kind = .tag, .owner = owner, .serial = pane.serial, .active = active, .reading = reading, .rect = .{ .x = r.x + config.GUTTER, .y = tag_y, .w = r.w - config.GUTTER, .h = pane.tag_rows } }); // A folded pane is its tag rows and nothing else. if (pane.collapsed) continue; const body_h = r.h -| pane.tag_rows; const line: i32 = if (pane.file) |f| @intCast(@min(f.scroll, std.math.maxInt(i32))) else if (pane.isTerminal()) @intCast(pane.termTop()) else 0; // The text it shows, for a frontend's glide to tell apart: a file's // path, a generated text's revision with it (rows rewritten as its // view moved are another text's); none for a terminal or a picture. const doc: u64 = if (pane.file) |f| std.hash.Wyhash.hash(if (f.output != null) f.revision else 0, f.path) else 0; const row: u32 = if (pane.file != null) panes.File.top(p.settings.tab, pane).row else 0; const held = pane.isTerminal() and panes.Term.modes(pane.terminal).sync; s.addRegion(.{ .kind = .body, .owner = owner, .serial = pane.serial, .active = active, .line = line, .row = row, .doc = doc, .held = held, .rect = .{ .x = r.x + config.GUTTER, .y = body_y, .w = r.w - config.GUTTER, .h = body_h } }); var rail: Region = .{ .kind = .rail, .owner = owner, .serial = pane.serial, .active = active, .rect = .{ .x = r.x, .y = body_y, .w = config.GUTTER, .h = body_h } }; // A kind's rail is its paint's (drawPane); every other pane's is // here. if (pane.addon == null and body_h > 0) { const sb: struct { total: usize, offset: usize, len: usize } = if (pane.file) |*f| .{ .total = panes.File.nlines(p.gpa, f), .offset = f.scroll, .len = pane.rows, } else blk: { const grid = panes.Term.grid(pane.terminal); break :blk .{ .total = grid.total, .offset = pane.termTop(), .len = grid.len }; }; const track_h: usize = body_h; const total = if (sb.total == 0) 1 else sb.total; const len = @max(1, (track_h * @max(1, sb.len)) / total); const pos = (track_h * sb.offset) / total; rail.thumb_y = @intCast(@min(pos, track_h)); rail.thumb_h = @intCast(@min(len, track_h - rail.thumb_y)); rail.thumb_at = @as(f32, @floatFromInt(sb.offset)) / @as(f32, @floatFromInt(total)); rail.thumb_span = @as(f32, @floatFromInt(@max(1, sb.len))) / @as(f32, @floatFromInt(total)); } s.addRegion(rail); if (body_h > 0 and pane.addon == null) pane.places.addTicks(p, pane, s, owner); // Each notice a chip at the right edge, one to a row in the order // collectNotices chose, as wide as its own message and no wider, so // the text beneath the rest of the row keeps reading as the file. if (r.h <= pane.tag_rows) continue; const tx = r.x + config.GUTTER; const tw = r.w - config.GUTTER; for (0..pane.notices.len) |i| { const left = pane.notices.left[i]; if (left == std.math.maxInt(u16)) continue; const chip = tx + tw - left; s.addRegion(.{ .kind = .notice, .owner = owner, .serial = pane.serial, .active = active, .notice = @intCast(i), .rect = .{ .x = tx + tw - chip, .y = body_y + @as(u16, pane.notices.row[i]), .w = chip, .h = BOX_H } }); } } const column_rows = layout.columnBarHeight(p); // A lone column's tag at the window row's left (UX K'2): no grip, a // column's edge has nothing to be dragged against. if (layout.columnInWindowRow(p)) { const inset = @min(TAG_TEXT_INSET, v.screen_w); s.addRegion(.{ .kind = .column_tag, .owner = 0, .serial = v.cols[0].serial, .active = layout.findPane(p, v.active) != null, .rect = .{ .x = inset, .y = 0, .w = tagline.windowRow(p).column, .h = p.topBarHeight() } }); } if (column_rows > 0) for (0..p.view().ncol) |c| { if (v.col_w[c] == 0) continue; const active = if (layout.findPane(p, v.active)) |found| found.col == c else false; const inset = @min(TAG_TEXT_INSET, v.col_w[c]); s.addRegion(.{ .kind = .column_grip, .owner = @intCast(c), .serial = v.cols[c].serial, .active = active, .rect = .{ .x = v.col_x[c], .y = p.topBarHeight(), .w = @min(config.GUTTER, v.col_w[c]), .h = column_rows } }); s.addRegion(.{ .kind = .column_tag, .owner = @intCast(c), .serial = v.cols[c].serial, .active = active, .rect = .{ .x = v.col_x[c] + inset, .y = p.topBarHeight(), .w = v.col_w[c] - inset, .h = column_rows } }); }; const active_col = if (layout.findPane(p, v.active)) |found| found.col else null; for (0..p.view().ncol) |c| { if (v.col_w[c] == 0) continue; s.addRegion(.{ .kind = .column, .owner = @intCast(c), .serial = v.cols[c].serial, .active = active_col == c, .rect = .{ .x = v.col_x[c], .y = p.topBarHeight(), .w = v.col_w[c], .h = s.rows -| (p.topBarHeight() + layout.noticeRowHeight(p)) } }); } if (p.settings.workspace_tag) s.addRegion(.{ .kind = .workspace_tag, .rect = .{ .x = 0, .y = 0, .w = s.cols, .h = p.topBarHeight() } }); const foot = layout.noticeRowHeight(p); if (foot > 0) s.addRegion(.{ .kind = .notice_row, .rect = .{ .x = 0, .y = s.rows - foot, .w = s.cols, .h = foot } }); } const Printed = struct { left: u16, dropped: usize }; /// Prints `text` flush with the right edge of the band, and answers the /// column it started at so a cursor can follow it. Text wider than the /// band loses its HEAD: the end is the part that says something -- a /// path's file name, an error's reason -- and `dropped` is how many /// columns of it went. fn printRight(tab: u16, s: *Surface, x: u16, row: u16, w: u16, text: []const u8, style: CellStyle) Printed { const width = panes.File.displayWidth(tab, text); if (width <= w) { const left: u16 = @intCast(x + w - width); _ = s.print(left, row, @intCast(width), text, style); return .{ .left = left, .dropped = 0 }; } // Whole glyphs only: a cut inside a wide one would leave the rest a // column too wide and clip its LAST glyph instead of the first. var dropped = width - w; var start = panes.File.rawAtDisplay(tab, text, dropped); if (panes.File.rawDisplayCol(tab, text, start) < dropped) { dropped += 1; start = panes.File.rawAtDisplay(tab, text, dropped); } const left: u16 = @intCast(x + w - (width - dropped)); _ = s.print(left, row, @intCast(width - dropped), text[start..], style); return .{ .left = left, .dropped = dropped }; } /// A notice chip, painted once as a tag layer at the tagline's pitch, and the /// grid's chip a copy of its right end, where its text is. fn joinNotice(p: *Pardes, s: *Surface, arena: std.mem.Allocator, region: Region) !void { const v = p.view(); const plain = &p.chrome().tag[0]; const id: usize = region.owner; const pane = p.panes[id].?; const i: usize = region.notice; const kind = pane.notices.kinds[i]; const r = v.rects[id]; const chip = region.rect.w; const text = Messages.noticeText(pane, i); const bg = plain.ground; const fg = plain.text; // Nothing to say, or too narrow for a layer: a blank chip on the grid. if (text.len == 0 or r.w <= TAG_TEXT_INSET) { s.fill(region.rect.x, region.rect.y, chip, 1, .{ .bg = .{ .rgb = bg }, .font_role = .tagline }); if (text.len == 0) return; const printed = printRight(p.settings.tab, s, region.rect.x, region.rect.y, chip -| 1, text, .{ .fg = .{ .rgb = fg }, .bg = .{ .rgb = bg }, .font_role = .tagline }); if (kind != .prompt or id != v.active or pane.prompt == .del_side or pane.prompt == .repl_choice) return; const prompt_col = panes.File.displayWidth(p.settings.tab, text[0..@min(@as(usize, @intCast(@max(0, pane.input.cur_col))), text.len)]); if (prompt_col < printed.dropped) return; s.cursor = .{ .x = @intCast(printed.left + (prompt_col - printed.dropped)), .y = region.rect.y, .bar = pane.input.mode == .insert }; return; } // Right aligned inside the chip, a blank cell short of its edge: a // prompt's caret sits AFTER its last character the moment it opens, and // flush right would put it past the pane and off the grid. Wider than // the chip, the text loses its head: the end is the part that says // something. const capacity = layout.tagCapacity(p, chip); const room = capacity -| 1; const shown = panes.File.displayWidth(p.settings.tab, text); var kept = text; if (shown > room) { var start = panes.File.rawAtDisplay(p.settings.tab, text, shown - room); if (panes.File.rawDisplayCol(p.settings.tab, text, start) < shown - room) start = panes.File.rawAtDisplay(p.settings.tab, text, shown - room + 1); kept = text[start..]; } const pad = room -| panes.File.displayWidth(p.settings.tab, kept); const line = try arena.alloc(u8, pad + kept.len); @memset(line[0..pad], ' '); @memcpy(line[pad..], kept); const layer = &s.tag_layers[NOTICE_LAYER_BASE + id * Pane.Notices.max + i]; try renderHeaderLayer(p, layer, .notice, @intCast(id), region.rect, line, 0, plain, .{ .rgb = bg }, fg); // The caret rides the band, where the prompt's glyphs are. if (kind == .prompt and id == v.active and pane.prompt != .del_side and pane.prompt != .repl_choice) caret: { const typed = panes.File.displayWidth(p.settings.tab, text[0..@min(@as(usize, @intCast(@max(0, pane.input.cur_col))), text.len)]); const dropped = shown - panes.File.displayWidth(p.settings.tab, kept); if (typed < dropped) break :caret; layer.cursor = .{ .x = @intCast(pad + typed - dropped), .y = 0, .bar = pane.input.mode == .insert }; } // The grid shows the chip's last cells of the band, where the text ends // (all of it where the tagline's pitch is the grid's). const skip = capacity -| chip; joinCells(s, region.rect.x, region.rect.y, layer.cells[0..layer.cols], skip, chip, .{ .rgb = fg }); // The band's padding before the text is the chip's fill, which carries // no ink of its own (the layer prints it as spaces in the band's ink). // A notice wider than its band has less padding than the band hides. for (skip..@max(skip, @min(pad, skip + chip))) |col| s.at(region.rect.x + @as(u16, @intCast(col - skip)), region.rect.y).style.fg = .default; if (layer.cursor) |caret| { // Counted wide: a band of 65535 glyphs (Layer.capacity's bound, which // a frontend's row metrics reach) puts its caret past u16, and past // the grid, where it is not drawn. const x = @as(u32, region.rect.x) + caret.x; if (caret.x >= skip and x - skip < s.cols) s.cursor = .{ .x = @intCast(x - skip), .y = region.rect.y, .bar = caret.bar }; } } pub fn render(p: *Pardes, arena: std.mem.Allocator) !*Surface { // By hand, not `taped`: the arena is no input (a replay resets its own, or skips the frame). const v = p.view(); const taped = pardes.tape.enter(p, .render); defer pardes.tape.leave(p, taped); edit.drawing = true; defer edit.drawing = false; panes.File.refreshHighlights(p); const s = &p.surface; const ncells = @as(usize, v.screen_w) * v.screen_h; if (s.cells.len != ncells) { const cells = try p.gpa.alloc(Cell, ncells); p.gpa.free(s.cells); s.cells = cells; } s.cols = v.screen_w; s.rows = v.screen_h; s.tab = p.settings.tab; s.cursor = null; s.pointer_shape = .arrow; // A look-hover preview means the pointed word is a real Look or Exec // operand: the same plumbing a click would act on. Hosts that model // the pointer per-text (the macOS I-beam) read this as "target" and // afford it the arrow instead. if (p.look_hover_preview != null) s.pointer_shape = .target; if (v.drag == .move or v.drag == .column_move) s.pointer_shape = .box; // Over a kind's pane, the pointer its last frame asked for (a PDF's // hand over a link; 2.2). if (v.kind_hover) |id| if (p.panes[id]) |pane| if (pane.addon) |k| { if (k.pointer == pardes.Kind.c.PA_POINTER_LINK) s.pointer_shape = .link; }; for (&s.body_layers) |*layer| layer.rows = 0; for (&s.tag_layers) |*layer| layer.rows = 0; s.placements.clearRetainingCapacity(); @memset(&s.gpu_frames, null); // The window's chrome (DESIGN.md B6): its rules, grips, the tags' two // grounds (G1': a reading pane's is its focus's), and the window and // column rows on the page (H). const chrome = p.chrome(); s.chrome = .{ .page = if (chrome.host_page) null else chrome.page.ground, .fg = if (chrome.host_page) null else chrome.page.text, .border = chrome.rule, .topbar_rule = config.gui_topbar_pane_border_rgb orelse chrome.rule, .tag_bg = chrome.tag[0].ground, .tagline_bg = chrome.page.ground, .tag_focus_bg = chrome.tag[1].ground, .tag_fg = chrome.tag[0].text, .scroll_track = chrome.rail, .scroll_thumb = chrome.rail_thumb, .match_tick = chrome.hit, .match_current = chrome.hit_current, .rule_px = chrome.rule_px, .grip_border = chrome.grip.ring, .grip_focus_ring = chrome.grip.focus, .grip_dirty = chrome.grip.dirty, .grip_mode = chrome.grip.mode, .grip_ink = chrome.grip.ink, .tag_rule = chrome.tag_rule, .rail_px = @intCast(@min(255, @as(u32, chrome.rail_px) * p.settings.grip_width / 100)), }; place(p, s); // Separators have their own role; a scrollbar need not frame the page. const whole = s.regions[0].rect; s.fill(whole.x, whole.y, whole.w, whole.h, .{ .bg = .{ .rgb = chrome.rule } }); for (0..p.view().ncol) |c| if (v.cols[c].n == 0) s.fill(v.col_x[c], p.topBarHeight() + layout.columnBarHeight(p), v.col_w[c], s.rows -| (p.topBarHeight() + layout.columnBarHeight(p) + layout.noticeRowHeight(p)), .{ .bg = .{ .rgb = chrome.empty_column } }); for (&p.panes, 0..) |*slot, id| { const pane = slot.* orelse continue; s.body_layers[id].rows = 0; try renderPane(p, s, arena, pane, v.rects[id], id, id == v.active); } // A tag out of rest after every body (UX DESIGN I1, M2): drawn as at // rest, its head and band hanging under it over the top rows of its // body, which stay where they were; beside them the grip's column on // the tag's ground, and a caret under them hidden until rest. if (TagState.hostOf(p)) |id| if (p.panes[id]) |pane| if (v.rects[id].w > config.GUTTER and v.rects[id].h > 0) { const r = v.rects[id]; const at = pardes.Band.covered(p, pane, id); const y = p.tagTop(pane, r) + at.tag; const rows = @min(at.over, r.h -| at.tag); if (rows > 0) { s.fill(r.x, y, config.GUTTER, rows, .{ .bg = .{ .rgb = chrome.on(if (id == v.active) .focus else .plain).ground }, .font_role = .tagline }); if (s.cursor) |c| if (c.x >= r.x and c.x < r.x + r.w and c.y >= y and c.y < y + rows) { s.cursor = null; }; if (s.body_layers[id].cursor) |c| if (pane.file != null and c.y < panes.File.coveredRows(pane)) { s.body_layers[id].cursor = null; }; } try joinPaneTag(p, s, arena, pane, r, id, id == v.active, at.tag); if (at.over > 0) try joinBand(p, s, arena, pane, r, id, id == v.active, at); }; // Each notice once, as a tag layer, and the grid's chip a copy of it. for (s.regionList()) |region| { if (region.kind == .notice) try joinNotice(p, s, arena, region); } try paintHeaders(p, s); try paintNoticeRow(p, s); // debug overlay: a small stats box drawn last, top-right ("Debug") if (p.settings.debug) if (p.panes[v.active]) |at| { var nlive: usize = 0; for (p.panes) |slot| { if (slot != null) nlive += 1; } var sb_off: usize = undefined; var sb_total: usize = undefined; if (at.file) |*f| { sb_off = f.scroll; sb_total = panes.File.nlines(p.gpa, f); } else if (at.addon != null) { sb_off = 0; sb_total = 0; } else { sb_off = at.termTop(); sb_total = panes.Term.grid(at.terminal).total; } const effective_font = p.settings.font.effective_name.get(); const font_name = if (effective_font.len == 0) "default" else effective_font; var ov_buf: [384]u8 = undefined; // The probe's last second (FrameStats); under a virtual clock its // real-time numbers would make every golden of this box differ. var frames_buf: [pardes.FrameStats.summary_max]u8 = undefined; const virtual = std.c.getenv("PARDES_TEST_CLOCK") != null; const timings = if (virtual) "frame virtual clock\n" else p.frames.summary(&frames_buf); const text = std.fmt.bufPrint( &ov_buf, "pardes resizes={d}\ntheme {s}\nfont {s}\nterms {d} cols {d}\nactive #{d} {d}x{d}\nscroll {d}/{d}\n{s}", .{ p.resize_count, p.theme().name, font_name[0..@min(font_name.len, 64)], nlive, v.ncol, v.active, at.cols, at.rows, sb_off, sb_total, timings }, ) catch ""; // wide enough for the longest theme name compiled in, which is the // only row here whose width is not ours to choose const ow: u16 = 38; const oh: u16 = 13; const ox: u16 = if (s.cols > ow) s.cols - ow else 0; if (ox + ow <= s.cols and p.topBarHeight() + oh <= s.rows) { s.addRegion(.{ .kind = .debug, .rect = .{ .x = ox, .y = p.topBarHeight(), .w = ow, .h = oh } }); // vaxis single_rounded border ring, default style s.set(ox, p.topBarHeight(), "╭", .{}); s.set(ox + ow - 1, p.topBarHeight(), "╮", .{}); s.set(ox, p.topBarHeight() + oh - 1, "╰", .{}); s.set(ox + ow - 1, p.topBarHeight() + oh - 1, "╯", .{}); var bx = ox + 1; while (bx < ox + ow - 1) : (bx += 1) { s.set(bx, p.topBarHeight(), "─", .{}); s.set(bx, p.topBarHeight() + oh - 1, "─", .{}); } var by = p.topBarHeight() + 1; while (by < p.topBarHeight() + oh - 1) : (by += 1) { s.set(ox, by, "│", .{}); s.set(ox + ow - 1, by, "│", .{}); } s.fill(ox + 1, p.topBarHeight() + 1, ow - 2, oh - 2, .{ .bg = .{ .rgb = chrome.rail } }); var lines = std.mem.splitScalar(u8, text, '\n'); var ly: u16 = p.topBarHeight() + 1; while (lines.next()) |line| : (ly += 1) { if (ly >= p.topBarHeight() + oh - 1) break; _ = s.print(ox + 1, ly, ow - 2, line, .{ .fg = .{ .rgb = chrome.tag[0].text } }); } } }; return s; } fn renderPane(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, id: usize, active: bool) !void { const tz = tracy.zone(@src(), "renderPane"); defer tz.end(); if (r.w <= config.GUTTER or r.h == 0) return; const chrome = p.chrome(); const tx = r.x + config.GUTTER; // text area (tag + body), right of the gutter const tw = r.w - config.GUTTER; const tag_y = p.tagTop(pane, r); const body_y = p.bodyTop(pane, r); const pane_bg: Color = if (chrome.host_page) .default else .{ .rgb = chrome.page.ground }; // text area resets to terminal-default cells (vaxis clear semantics); // light themes paint the page over it. { // Two full passes over every cell in the pane, every frame. const tz_clear = tracy.zone(@src(), "paneClear"); defer tz_clear.end(); s.clearRect(tx, r.y, tw, r.h); if (!chrome.host_page) s.fill(tx, r.y, tw, r.h, .{ .bg = pane_bg }); } // The grip is acme's button: the focused pane's filled, the others' // a ring round the tag's ground, a reading one's in the mode's mark // (G1'), a dirty one's the dirty colour inside its ring. A pixel shell // draws the ring (gui.zig gripDecor); a grid, which cannot, fills the // cell: the ring at rest, the mode's mark reading, the focused one's // glyph in the mode's mark where it reads (R5'); and marks dirty in // the grip's second cell with a bold `*` on the dirty colour, a cue // that survives any palette. const band = chrome.on(if (active) .focus else .plain).ground; const dirty = tagline.dirty(pane); const reading = pardes.Trail.reading(p, pane); const grip = &chrome.grip; const fill: colors.Chrome.Fill = if (active) .focus else if (reading) .mode else .ring; const fills = [4][3]u8{ grip.focus, grip.dirty, grip.ring, grip.mode }; const box_bg = fills[@intFromEnum(fill)]; const box_style: CellStyle = .{ .fg = .{ .rgb = if (fill == .ring) chrome.tag[0].text else if (fill == .focus and reading) grip.mode_glyph else grip.ink }, .bg = .{ .rgb = box_bg }, // Grips keep physical grid dimensions, independently of tag text. .font_role = .tagline, }; // On the first row alone, as acme's button is; the rows under it are // the tag band's, and still grab the pane (the region spans them all, // as acme's button column does). s.fill(r.x, tag_y, config.GUTTER, @min(1, pane.tag_rows), box_style); if (dirty and pane.tag_rows > 0 and config.GUTTER > 1) s.set(r.x + config.GUTTER - 1, tag_y, "*", .{ .fg = .{ .rgb = grip.ink }, .bg = .{ .rgb = grip.dirty }, .bold = true, .font_role = .tagline }); // The mark (pick-ui SPEC §2): the status of the most important live // message about the pane, in the box's second cell, on the dirty ground // when there is unsaved text: a cell of the box's, never the tag's // text, so it moves nothing. if (pane.tag_rows > 0 and config.GUTTER > 1) if (p.messages.markOf(pane)) |kind| { const on: colors.Chrome.Fill = if (dirty) .dirty else fill; s.set(r.x + config.GUTTER - 1, tag_y, kind.glyph(), .{ .fg = .{ .rgb = Messages.inkOn(&grip.marks[@intFromEnum(on)], kind) }, .bg = .{ .rgb = if (dirty) grip.dirty else box_bg }, .bold = true, .font_role = .tagline }); }; s.fill(r.x, tag_y + 1, config.GUTTER, pane.tag_rows -| 1, .{ .bg = .{ .rgb = band }, .font_role = .tagline, }); // In window mode the box holds the pane's letter instead of its mode. const label: ?u8 = if (p.view().window_pick != null) layout.windowLabel(p, id) else null; // A tag in a typing state is typed in: its box says so (walk UI SPEC // §3.2), whatever the body's mode; not while it waits for the keyboard // (UX DESIGN O2). const typing = p.view().tag_state.kind != .rest and !p.view().tag_state.waits and TagState.hostOf(p) == id; s.set(r.x, tag_y, if (label) |*letter| @as(*const [1]u8, letter) else if (typing) config.tag_insert else switch (pane.body.mode) { .normal => config.tag_normal, .insert => config.tag_insert, .tty => config.tag_tty, }, box_style); // Out of rest its tag is drawn after the bodies, over its own (render). if (!pardes.Trail.outOfRest(p, pane)) try joinPaneTag(p, s, arena, pane, r, id, active, @min(pane.tag_rows, MAX_TAG_ROWS)); // Folded panes retain their body state, but neither body pixels nor // its cursor belong outside the one-row tag rectangle. if (pane.collapsed) return; const rail = s.region(.rail, id).?; if (pane.addon != null) { // Below the notice chips: a picture is drawn after the cells (the // GUI's image pass, kitty's z-order), so a chip over it would be // painted out. Text gets the overlay; a kind's pane gives up the rows. const shown = pane.notices.span(); const body_h = (r.h -| pane.tag_rows) -| shown; // A picker's preview through a kind (Pick.showKind): its file named // until its kind has drawn it. if (!pardes.Kind.draw(p, s, arena, pane, id, tx, body_y + shown, tw, body_h) and body_h > 0 and pardes.Pick.previewing(p, id)) { const k = pane.addon.?; if (pardes.Pick.nameLine(arena, k.path, k.nameOf(), k.size, "")) |line| { _ = s.print(tx, body_y + shown, tw, line, .{ .fg = if (chrome.host_page) .default else .{ .rgb = chrome.page.text }, .bg = pane_bg }); } else |_| {} } // The same one column as the real scrollbar below, running past // the notice bands so the gutter has no notch in it; its thumb // where this frame's paint said its window is in its whole (a // PDF's strip), none for a kind that said nothing (an image). s.fill(rail.rect.x, rail.rect.y, 1, rail.rect.h, .{ .bg = .{ .rgb = chrome.rail } }); s.fill(rail.rect.x + 1, rail.rect.y, 1, rail.rect.h, .{ .bg = pane_bg }); const k = pane.addon.?; // (a body of no rows has no thumb: a pane squeezed to its tag) if (k.thumb_span > 0 and rail.rect.h > 0) { const track: f32 = @floatFromInt(rail.rect.h); rail.thumb_y = @intFromFloat(@min(track - 1, @floor(k.thumb_at * track))); rail.thumb_h = @intFromFloat(@min(track - @as(f32, @floatFromInt(rail.thumb_y)), @max(1, k.thumb_span * track))); rail.thumb_at = k.thumb_at; rail.thumb_span = k.thumb_span; s.fill(rail.rect.x, rail.rect.y + rail.thumb_y, 1, rail.thumb_h, .{ .bg = .{ .rgb = chrome.rail_thumb } }); } return; } try body_layer.joinBody(p, s, arena, pane, r, id, active, rail.*); // The keyboard is in the head's field: its caret (render), not the // body's. if (typing) { s.body_layers[id].cursor = null; s.cursor = null; } if (pane.layers.heat > 0) @import("jj.zig").railHeat(p, s, pane, id, rail.rect); // A terminal panned to its cursor in a view shorter than it: an arrow // in the gutter's first body row says rows are above (views.zig). if (pane.pan > 0) s.set(r.x, p.bodyTop(pane, r), "↑", .{ .fg = .{ .rgb = chrome.page.key } }); // A query's places' ticks, each in the track's cell of its row: chrome, // painted with the rail (a pixel shell draws its own from the same list). for (s.rail_ticks[0..s.nrail_ticks]) |tick| { if (tick.owner != id or rail.rect.h == 0) continue; const row: u16 = @intCast(@min(rail.rect.h - 1, @as(usize, tick.at) * rail.rect.h / panes.Places.tick_scale)); s.fill(rail.rect.x, rail.rect.y + row, 1, 1, .{ .bg = .{ .rgb = if (tick.current) s.chrome.match_current else s.chrome.match_tick } }); } } /// The workspace and column tags: each line a tag layer, and the grid's /// rows copies of those; the column grips, which are the grid's alone. fn paintHeaders(p: *Pardes, s: *Surface) !void { const v = p.view(); const chrome = p.chrome(); // The window row and the column tags are chrome words on the page, no // band and no focus (DESIGN.md H): `page.word`, every word of them; a // terminal's own page where the theme leaves it to the host. const page = &chrome.page; const page_bg: Color = if (chrome.host_page) .default else .{ .rgb = page.ground }; const column_rows = layout.columnBarHeight(p); // A window draws its underlay under them in the page too (gui.zig). s.chrome.tagline_rows = @intCast(p.topBarHeight() + column_rows); // global tagbar: full width, top rows — unless a native shell has // taken its commands over, in which case the rows are not ours to draw if (s.region(.workspace_tag, Region.none)) |region| { const top = if (v.header != null and p.headerColumn() == null) v.header_top else 0; const layer = &s.tag_layers[MAX_PANES + MAX_COLS]; // The window row (UX K'2), on the page end to end: a lone column's // tag at its left (its region), in the row's inks; the window's at // its right end (tagline.windowRow). s.fill(region.rect.x, region.rect.y, region.rect.w, region.rect.h, .{ .bg = page_bg, .fg = .{ .rgb = page.word }, .font_role = .tagline }); if (s.region(.column_tag, 0)) |column| if (column.rect.y < region.rect.y + region.rect.h) { const column_top = if (v.header != null and p.headerColumn() == 0) v.header_top else 0; const column_layer = &s.tag_layers[MAX_PANES]; try renderHeaderLayer(p, column_layer, .column, 0, column.rect, tagline.columnTag(p, 0), column_top, page, page_bg, page.word); joinHeaderRows(s, column_layer, page.word); }; const words = tagline.windowRow(p).words; const viewport: Rect = .{ .x = words, .y = region.rect.y, .w = region.rect.w -| words, .h = region.rect.h }; const text = tagline.topbar(p); try renderHeaderLayer(p, layer, .workspace, 0, viewport, text, top, page, page_bg, page.word); // A server's progress (a message of no pane) at its first row's // right end, where the row has room for it. if (layer.rows > 0 and top == 0) if (tagline.sessionProgress(p)) |said| { var buf: [Messages.LoggedMessage.cap + 8]u8 = undefined; const shown = std.fmt.bufPrint(&buf, "{s} {s}", .{ Messages.Kind.progress.glyph(), said.words() }) catch ""; const width = panes.File.displayWidth(p.settings.tab, shown); const used = panes.File.displayWidth(p.settings.tab, modal.lineSlice(text, 0)); const room = layout.tagCapacity(p, viewport.w); var row: Surface = .{ .cols = layer.cols, .rows = 1, .cells = layer.cells[0..layer.cols], .tab = p.settings.tab }; if (used + 2 + width < room) _ = row.print(@intCast(room - 1 - width), 0, @intCast(width), shown, .{ .fg = .{ .rgb = Messages.inkOn(page, .progress) }, .bg = page_bg, .font_role = .tagline }); }; joinHeaderRows(s, layer, page.word); } if (column_rows > 0) for (0..p.view().ncol) |c| { const region = s.region(.column_tag, c) orelse continue; if (v.col_x[c] < s.cols) { // A grip is a step quieter than a pane's at rest (2:1, H) until // it is grabbed; then it lights (3:1), so the drag shows what it // holds. It never shows focus. const grip = @min(config.GUTTER, v.col_w[c]); const inset = @min(TAG_TEXT_INSET, v.col_w[c]); const grabbed = v.drag == .column_move and layout.columnBySerial(p, v.drag.column_move.serial) == c; const grip_bg = if (grabbed) chrome.grip.column else chrome.grip.column_rest; s.fill(v.col_x[c], region.rect.y, grip, @min(1, column_rows), .{ .bg = .{ .rgb = grip_bg }, .fg = .{ .rgb = chrome.grip.ink }, .font_role = .tagline }); s.fill(v.col_x[c], region.rect.y + 1, grip, column_rows -| 1, .{ .bg = page_bg, .font_role = .tagline }); s.fill(v.col_x[c] + grip, region.rect.y, inset - grip, column_rows, .{ .bg = page_bg, .font_role = .tagline }); } const top = if (v.header != null and p.headerColumn() == c) v.header_top else 0; const layer = &s.tag_layers[MAX_PANES + c]; try renderHeaderLayer(p, layer, .column, @intCast(c), region.rect, tagline.columnTag(p, c), top, page, page_bg, page.word); if (v.col_x[c] < s.cols) joinHeaderRows(s, layer, page.word); }; } /// The notice row (Messages.noticeRow), the live messages' index at the /// window's foot: a header layer at the tagline's pitch in the workspace /// tag's colours, the glyph in its kind's, a message gone dim and the /// counts at the right end dimmed. fn paintNoticeRow(p: *Pardes, s: *Surface) !void { const region = s.region(.notice_row, Region.none) orelse return; // The notice row's own ground and inks (DESIGN.md H: `status`). const status = &p.chrome().status; var viewport = region.rect; const inset = @min(TAG_TEXT_INSET, viewport.w); s.fill(viewport.x, viewport.y, inset, viewport.h, .{ .bg = .{ .rgb = status.ground }, .font_role = .tagline }); viewport.x += inset; viewport.w -= inset; var buf: [Messages.notice_row_cap]u8 = undefined; const row = Messages.noticeRow(p, layout.tagCapacity(p, viewport.w) -| 1, &buf); const layer = &s.tag_layers[pardes.NOTICE_ROW_LAYER]; try renderHeaderLayer(p, layer, .notice_row, 0, viewport, row.text, 0, status, .{ .rgb = status.ground }, status.text); if (layer.rows == 0) return; const dim: Color = .{ .rgb = status.dim }; const ink: Color = .{ .rgb = status.text }; const cells = layer.cells[0..layer.cols]; const tab = p.settings.tab; const left = @min(cells.len, panes.File.displayWidth(tab, row.text[0..row.left])); const right = @min(cells.len, panes.File.displayWidth(tab, row.text[0..row.right])); // A picker's sentence: dim, its address in the text's ink. if (row.quiet_left) { const lo = panes.File.displayWidth(tab, row.text[0..@min(row.word[0], row.left)]); const hi = panes.File.displayWidth(tab, row.text[0..@min(row.word[1], row.left)]); for (cells[0..left], 0..) |*cell, x| if ((x < lo or x >= hi) and std.meta.eql(cell.style.fg, ink)) { cell.style.fg = dim; }; } if (row.lead) |lead| { if (lead.kind == .@"error" or lead.kind == .warn) if (cells.len > 0 and std.meta.eql(cells[0].style.fg, ink)) { cells[0].style.fg = .{ .rgb = Messages.inkOn(status, lead.kind) }; }; if (lead.faded(p.clock.now_ns, p.settings.message_linger_ms)) for (cells[0..left]) |*cell| { if (std.meta.eql(cell.style.fg, ink)) cell.style.fg = dim; }; } for (cells[right..]) |*cell| if (std.meta.eql(cell.style.fg, ink)) { cell.style.fg = dim; }; joinHeaderRows(s, layer, status.text); } /// Copy a header's layer into the grid under it, row by row, and its caret. fn joinHeaderRows(s: *Surface, layer: *const pardes.Layer, fg: [3]u8) void { const v = layer.viewport; const w = @min(v.w, s.cols -| v.x); for (0..layer.rows) |line| joinCells(s, v.x, v.y + @as(u16, @intCast(line)), layer.cells[line * layer.cols ..][0..layer.cols], 0, w, .{ .rgb = fg }); if (layer.cursor) |caret| { if (caret.x < v.w) s.cursor = .{ .x = v.x + caret.x, .y = v.y + caret.y, .bar = caret.bar }; } } /// Copy `w` cells of a layer row, from `from`, into the grid at (x, y) -- /// the one place the grid gets a tag's, a header's or a notice's cells. A /// wide glyph cut by either end is left as the band's blank, the way the /// grid's own print never drew half of one: no head without its spacer, /// no spacer without its head. `ink` is the band's text colour, which a /// blank cell does not carry. fn joinCells(s: *Surface, x: u16, y: u16, cells: []const Cell, from: usize, w: u16, ink: Color) void { // A tagline wider than the body's cells can lay out fewer than `w`. const n: u16 = @intCast(@min(w, cells.len -| from)); if (n == 0 or y >= s.rows) return; const row = s.cells[@as(usize, y) * s.cols ..][0..s.cols]; @memcpy(row[x..][0..n], cells[from..][0..n]); const blank = struct { fn of(cell: *Cell, band_ink: Color) void { cell.text[0] = ' '; cell.len = 1; if (std.meta.eql(cell.style.fg, band_ink)) cell.style.fg = .default; } }.of; const last = from + n - 1; if (last + 1 < cells.len and !cells[last + 1].default and cells[last + 1].len == 0) blank(&row[x + n - 1], ink); if (!cells[from].default and cells[from].len == 0) blank(&row[x], ink); } fn tagTintRange(p: *const Pardes, pane: *const Pane, tag: []const u8, prefix_len: usize) ?struct { lo: usize, hi: usize } { if (pane.isTerminal()) { // `tag` is the first row; a prefix longer than it ran to a second. var words = std.mem.tokenizeAny(u8, tag[@min(prefix_len, tag.len)..], " \t\r\n"); while (words.next()) |word| { if (!std.mem.eql(u8, word, "Term") and !std.mem.startsWith(u8, word, "Term+")) continue; const lo = @intFromPtr(word.ptr) - @intFromPtr(tag.ptr); return .{ .lo = lo, .hi = lo + word.len }; } return null; } var start: usize = tagline.nameAt(p, pane); const name = if (tagline.named(p, pane)) |file| if (pane.prompt == .name) pane.input.own orelse file.path else file.path else if (pane.addon) |k| blk: { // A kind's tag may put its words before the path (an image's // renderer controls). if (k.path.len == 0) return null; start = std.mem.lastIndexOf(u8, tag[0..@min(prefix_len, tag.len)], k.path) orelse return null; break :blk k.path; } else return null; const basename = if (std.mem.lastIndexOfScalar(u8, name, '/')) |slash| slash + 1 else 0; if (basename == name.len) return null; // A walked file's path cut from its left: a `…` for the bytes it drops // (tagline.folded), never its basename's. const cut = if (pane.file != null) tagline.folded(p, pane).cut else 0; const dots: usize = if (cut > 0) "…".len else 0; return .{ .lo = start + dots + basename - cut, .hi = start + dots + name.len - cut }; } /// A header or a notice band as a tag layer: `viewport.h` rows, line `top` /// of `text` and on, laid out at the tagline's pitch -- as wide as its /// widest line and never narrower than its viewport -- on the ground of /// `inks` (`ground` in a cell: a terminal's own page where the theme leaves /// it to the host), in `fg`. fn renderHeaderLayer(p: *Pardes, layer: *pardes.Layer, kind: TagKind, id: u16, viewport: Rect, text: []const u8, top: usize, inks: *const colors.Chrome.Inks, ground: Color, fg: [3]u8) !void { const v = p.view(); if (viewport.w == 0 or viewport.h == 0) { // No layer, and nothing of an earlier frame's left in it. layer.rows = 0; layer.viewport = viewport; layer.cursor = null; return; } var widest: usize = 0; for (0..viewport.h) |line| widest = @max(widest, panes.File.displayWidth(p.settings.tab, modal.lineSlice(text, line + top))); const columns: u16 = @intCast(@min(std.math.maxInt(u16), @max(layout.tagCapacity(p, viewport.w), widest + 1))); const count = @as(usize, columns) * viewport.h; if (layer.cells.len != count) layer.cells = try p.gpa.realloc(layer.cells, count); layer.kind = kind; layer.id = id; layer.serial = if (kind == .column) v.cols[id].serial else 0; layer.viewport = viewport; layer.cols = columns; layer.rows = viewport.h; layer.context_rows = 0; layer.bg = inks.ground; layer.cursor = null; // A notice band carries a PANE id, not a column index, so it must not // answer to the column being edited -- that would scroll and select // its text with the column tag's offsets and give it a second caret. const editing: ?*Text = if (v.header != null and switch (kind) { .workspace => p.headerColumn() == null, .column => p.headerColumn() == id, .pane, .notice, .body, .band, .notice_row => false, }) tagline.focusedHeader(p) else null; const scroll = if (editing != null) v.header_scroll else 0; for (0..viewport.h) |index| { const line: u16 = @intCast(index); const shown = modal.lineSlice(text, index + top); var surface: Surface = .{ .cols = columns, .rows = 1, .cells = layer.cells[index * columns ..][0..columns], .tab = p.settings.tab }; surface.fill(0, 0, columns, 1, .{ .bg = ground, .font_role = .tagline }); // The scroll is the caret's line's: a shorter line shows nothing. _ = surface.print(0, 0, columns, shown[@min(shown.len, panes.File.rawAtDisplay(p.settings.tab, shown, scroll))..], .{ .fg = .{ .rgb = fg }, .bg = ground, .font_role = .tagline }); // The word under the pointer, a Look or Exec target: on a header even // while it is edited, on a notice, and on the grid's copy of either // (joined from this); not on a column a carried column's pointer is // aiming at a place over; nor under a held middle or right sweep, // which paints what it sweeps instead. const hovered = v.pointer_inside and !(kind == .column and v.drag == .column_move) and v.header_sweep == null; if (hovered) { const tag_hit = v.pointer_tag_hit; const col: ?usize = if (tag_hit) |pointer| (if (pointer.kind == kind and pointer.id == id and pointer.line == line) pointer.col else null) else if (v.hover_row == viewport.y + line and v.hover_col >= viewport.x and v.hover_col - viewport.x < viewport.w) v.hover_col - viewport.x else null; if (col) |hover| if (wordBoundsAtCol(shown, panes.File.rawAtDisplay(p.settings.tab, shown, hover + scroll))) |bounds| { var cell = panes.File.rawDisplayCol(p.settings.tab, shown, bounds.lo) -| scroll; const end = panes.File.rawDisplayCol(p.settings.tab, shown, bounds.hi) -| scroll; while (cell < end and cell < columns) : (cell += 1) surface.at(@intCast(cell), 0).style.bg = .{ .rgb = inks.selection.secondary }; }; } if (editing) |t| { if (tagline.headerSelection(t, try edit.flatSurface(p.editor(), t), line + v.header_top)) |span| { var col = panes.File.rawDisplayCol(p.settings.tab, shown, span.lo) -| scroll; const end = panes.File.rawDisplayCol(p.settings.tab, shown, span.hi) -| scroll; while (col < end and col < columns) : (col += 1) { surface.at(@intCast(col), 0).style.bg = .{ .rgb = inks.selection.ground }; surface.at(@intCast(col), 0).style.fg = .{ .rgb = inks.selection.text }; } } const caret = panes.File.rawDisplayCol(p.settings.tab, shown, @min(@as(usize, @intCast(@max(0, t.cur_col))), shown.len)) -| scroll; const asking = if (p.panes[v.active]) |active| active.prompt != .none else false; if (!asking and t.cur_row == line + v.header_top and caret < columns) layer.cursor = .{ .x = @intCast(caret), .y = line, .bar = t.mode == .insert }; } // A middle or right sweep held over this header, tinted by its // button as a pane tag's is. if (v.header_sweep) |sweep| if (sweep.column(p)) |column| { const mine = switch (kind) { .workspace => column == null, .column => column == id, .pane, .notice, .body, .band, .notice_row => false, }; const swept = sweep.span(text.len); const start = modal.lineStartOffset(text, index + top); const end = start + shown.len; if (mine and swept.lo < swept.hi and swept.hi > start and swept.lo < end) { const button = @backingInt(sweep.button); var col = panes.File.rawDisplayCol(p.settings.tab, shown, @max(swept.lo, start) - start) -| scroll; const stop = panes.File.rawDisplayCol(p.settings.tab, shown, @min(swept.hi, end) - start) -| scroll; while (col < stop and col < columns) : (col += 1) { surface.at(@intCast(col), 0).style.bg = .{ .rgb = p.chrome().sweeps[button].ground }; surface.at(@intCast(col), 0).style.fg = .{ .rgb = p.chrome().sweeps[button].text }; } } } else |_| {}; } } /// A pane's tag's `rows` text rows (MAX_TAG_ROWS at most; out of rest its /// rows at rest, Band.covered), painted once into their layer, a row to a /// line, and copied into the grid; the grid's caret where the layer has it. fn joinPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, id: usize, active: bool, rows: u16) !void { const v = p.view(); const capacity = layout.tagCapacity(p, r.w -| TAG_TEXT_INSET); // Too narrow for a layer: only the grid's gap cell. if (r.w <= TAG_TEXT_INSET) return paintPaneTag(p, s, arena, pane, r, id, active, rows); const inks = p.chrome().on(if (active) .focus else .plain); const region = s.region(.tag, id).?; const tag = (try tagline.shownTag(p, arena, pane)).text; // As wide as the widest row it shows, and never narrower than its // viewport. var widest: usize = 0; var split = std.mem.splitScalar(u8, tag, '\n'); for (0..pane.tag_top) |_| _ = split.next(); for (0..rows) |_| widest = @max(widest, panes.File.displayWidth(p.settings.tab, split.next() orelse "")); const columns: u16 = @intCast(@min(std.math.maxInt(u16) - TAG_TEXT_INSET, @max(capacity, widest + 1))); const pitch = @as(usize, columns) + TAG_TEXT_INSET; const storage = try arena.alloc(Cell, pitch * rows); @memset(storage, .{}); var temporary: Surface = .{ .cols = columns + TAG_TEXT_INSET, .rows = rows, .cells = storage, .tab = p.settings.tab }; try paintPaneTag(p, &temporary, arena, pane, .{ .x = 0, .y = 0, .w = columns + TAG_TEXT_INSET, .h = rows }, id, active, rows); // Window mode (layout.windowKey): the outlined pane's tag in the // selection's colours, the plain tag's whichever pane it is (one // outline, walked from pane to pane). const outline = &p.chrome().tag[0].selection; if (v.window_pick == id) { for (0..rows) |row| temporary.tint(0, @intCast(row), temporary.cols, outline.ground, outline.text); } const tag_fg: Color = .{ .rgb = inks.text }; // One layer, a row to each line the tag shows. const layer = &s.tag_layers[id]; const count = @as(usize, columns) * rows; if (layer.cells.len != count) layer.cells = try p.gpa.realloc(layer.cells, count); layer.kind = .pane; layer.id = @intCast(id); layer.serial = pane.serial; layer.viewport = .{ .x = region.rect.x, .y = region.rect.y, .w = region.rect.w, .h = rows }; layer.cols = columns; layer.rows = rows; layer.context_rows = 0; layer.bg = if (v.window_pick == id) outline.ground else inks.ground; layer.cursor = if (v.header != null) null else if (temporary.cursor) |cursor| .{ .x = cursor.x - TAG_TEXT_INSET, .y = cursor.y, .bar = cursor.bar } else null; for (0..rows) |line| { @memcpy(layer.cells[line * columns ..][0..columns], storage[line * pitch + TAG_TEXT_INSET ..][0..columns]); // The grid's row: the gap cell and the text, as far as the pane. joinCells(s, region.rect.x, region.rect.y + @as(u16, @intCast(line)), storage[line * pitch ..][0..pitch], config.GUTTER, region.rect.w, tag_fg); } // Past a full row's end the layer's caret is at the tag's edge, as // acme's tick is; the grid has no cell there, so it takes the last one. if (temporary.cursor) |cursor| { s.cursor = .{ .x = r.x + @min(cursor.x, r.w - 1), .y = region.rect.y + cursor.y, .bar = cursor.bar }; } } /// The head and the band under pane `id`'s tag (UX DESIGN M2): the head's /// `drawn.head` rows (TagState.head: its keys, the level or the field and /// its caret, the count, the hints), then the band's `drawn.band` /// (Band.zig), painted once into their layer at the tagline's pitch, a row /// to a line, and copied into the grid: the spines and steps in the tag's /// tint, the keys in the key ink, the cursor's row tinted as a selection. fn joinBand(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, id: usize, active: bool, drawn: pardes.Band.Covered) !void { const layer = &s.tag_layers[pardes.BAND_LAYER]; const rows = drawn.over; if (r.w <= TAG_TEXT_INSET) return; const head = (try TagState.head(p, arena, pane)) orelse return; const band = try pardes.Band.of(p, arena, pane, id); const chrome = p.chrome(); const tab = p.settings.tab; const columns = @max(layout.tagCapacity(p, r.w -| TAG_TEXT_INSET), 1); // The inks of its tag's ground, focused or not, whatever its mode // (DESIGN.md B6, G1'), and of its cursor row's selection, a ground of // its own (B1.3). const g: colors.Chrome.Ground = if (active) .focus else .plain; const inks = chrome.on(g); const selected = &inks.selection; const bg = inks.ground; const fg = inks.text; const storage = try arena.alloc(Cell, @as(usize, columns) * rows); @memset(storage, .{}); var t: Surface = .{ .cols = columns, .rows = rows, .cells = storage, .tab = tab }; t.fill(0, 0, columns, rows, .{ .bg = .{ .rgb = bg }, .font_role = .tagline }); // The head's rows, as it laid itself out at the tag's width (UX D2, D3). const shown = try tagline.wrapTag(tab, arena, head.text, columns); var lines: [MAX_TAG_ROWS][]const u8 = @splat(""); var split = std.mem.splitScalar(u8, shown.text, '\n'); for (lines[0..drawn.head], 0..) |*line, y| { line.* = split.next() orelse break; _ = t.print(0, @intCast(y), columns, line.*, .{ .fg = .{ .rgb = fg }, .bg = .{ .rgb = bg }, .font_role = .tagline }); } // Its keys in the key ink, its dimmed, underlined and struck words // (TagState.Span), over whichever rows they wrapped onto. for (head.spans) |span| { const a = modal.positionAt(shown.text, shown.fromReal(span.lo)); const b = modal.positionAt(shown.text, shown.fromReal(span.hi)); var row = a.row; while (row <= b.row and row < drawn.head) : (row += 1) { const line = lines[row]; var col = panes.File.rawDisplayCol(tab, line, @min(if (row == a.row) a.col else 0, line.len)); const end = panes.File.rawDisplayCol(tab, line, @min(if (row == b.row) b.col else line.len, line.len)); while (col < end and col < columns) : (col += 1) { const style = &t.at(@intCast(col), @intCast(row)).style; switch (span.style) { .key => { style.fg = .{ .rgb = inks.key }; style.bold = true; }, .dim => style.fg = .{ .rgb = inks.dim }, .under => style.ul = .single, .strike => style.strikethrough = true, .err => style.fg = .{ .rgb = inks.severity[0] }, .rule => style.fg = .{ .rgb = inks.rule }, } } } } // The band's rows under the head's. if (band) |b| for (0..drawn.band) |y| { const row: u16 = @intCast(drawn.head + y); const ln = try b.line(arena, tab, @intCast(y)); _ = t.print(0, row, columns, ln.text, .{ .fg = .{ .rgb = fg }, .bg = .{ .rgb = bg }, .font_role = .tagline }); // The spines and the rules between steps in the rule ink; a row's // `▶` (the cursor) and `•` (marked) in the key ink. var col: u16 = 0; var at: usize = 0; while (at < ln.text.len and col < columns) : (col += 1) { const next = modal.nextGrapheme(ln.text, at); const glyph = ln.text[at..next]; if (std.mem.eql(u8, glyph, "│")) t.at(col, row).style.fg = .{ .rgb = inks.rule }; if (std.mem.eql(u8, glyph, "▶") or std.mem.eql(u8, glyph, "•")) t.at(col, row).style.fg = .{ .rgb = inks.key }; at = next; } for (ln.parts) |part| { const end = @min(columns, part.col + part.width); const dim = if (part.row.on) selected.dim else inks.dim; const key = if (part.row.on) selected.key else inks.key; // The cursor's row across its step's width, its lead too (UX // D5), the lead's `▶` keeping the key ink (D4). if (part.row.on and part.col < end) { const lead = part.col -| 2; t.tint(lead, row, part.col - lead, selected.ground, null); for (lead..part.col) |x| if (std.meta.eql(t.at(@intCast(x), row).style.fg, Color{ .rgb = inks.key })) { t.at(@intCast(x), row).style.fg = .{ .rgb = selected.key }; }; t.tint(part.col, row, end - part.col, selected.ground, selected.text); } var c = part.col; var i: usize = 0; while (i < part.row.text.len and c < end) : (c += 1) { const next = modal.nextGrapheme(part.row.text, i); const style = &t.at(c, row).style; if (i < part.row.key) { style.fg = .{ .rgb = key }; style.bold = true; } // A chord (Help's `SPC h t`): keys, at the text's weight. if (i < part.row.chord) style.fg = .{ .rgb = key }; if (i < part.row.pre) style.fg = .{ .rgb = chrome.name[@intFromEnum(g)] }; if (part.row.dim or (i >= part.row.pre and i < part.row.pre + part.row.faint) or (i >= part.row.quiet[0] and i < part.row.quiet[1])) style.fg = .{ .rgb = dim }; if (i < part.row.kind) style.fg = .{ .rgb = inks.kind }; for (part.row.keys) |k| if (k == i) { style.fg = .{ .rgb = key }; style.bold = true; }; if (part.row.under) style.ul = .single; // A matched letter: the match ink, bold, on the row's own // ground (never a ground of its own: on the cursor row's // selection one vanished). for (part.row.marks) |m| if (m == i) { style.fg = .{ .rgb = if (part.row.on) selected.match else inks.match }; style.bold = true; }; i = next; } // Its meta, right-aligned in the part (Band.line), dim. if (pardes.Band.metaShown(tab, part.row, part.width) and part.col + part.width <= columns) { const meta: u16 = @intCast(panes.File.displayWidth(tab, part.row.meta)); for (part.col + part.width - meta..part.col + part.width) |x| t.at(@intCast(x), row).style.fg = .{ .rgb = dim }; } } }; const count = @as(usize, columns) * rows; if (layer.cells.len != count) layer.cells = try p.gpa.realloc(layer.cells, count); @memcpy(layer.cells, storage); // Under the tag's rows, over the body's top ones (Band.covered). const y0 = p.tagTop(pane, r) + drawn.tag; layer.kind = .band; layer.id = @intCast(id); layer.serial = pane.serial; layer.viewport = .{ .x = r.x + TAG_TEXT_INSET, .y = y0, .w = r.w - TAG_TEXT_INSET, .h = rows }; layer.cols = columns; layer.rows = rows; layer.context_rows = 0; layer.bg = bg; layer.cursor = null; for (0..rows) |y| joinCells(s, r.x + TAG_TEXT_INSET, y0 + @as(u16, @intCast(y)), storage[y * columns ..][0..columns], 0, r.w - TAG_TEXT_INSET, .{ .rgb = fg }); // A head typed in: its caret a bar at the field's, the keyboard's // where the pane has it. if (head.caret) |c| if (active and !p.view().tag_state.waits) { const at = modal.positionAt(shown.text, shown.fromReal(c)); if (at.row < drawn.head) { const col = panes.File.rawDisplayCol(tab, lines[at.row], @min(at.col, lines[at.row].len)); if (col < columns) { layer.cursor = .{ .x = @intCast(col), .y = @intCast(at.row), .bar = true }; if (col < r.w - TAG_TEXT_INSET) s.cursor = .{ .x = r.x + TAG_TEXT_INSET + @as(u16, @intCast(col)), .y = y0 + @as(u16, @intCast(at.row)), .bar = true }; } } }; } /// Bytes `lo` to `hi` of a tag's text in `ink`, over whichever of its /// shown rows (`lines`, from shown row `top`, drawn at `x`, `y`, `w` /// wide) they wrapped onto. fn inkTag(p: *Pardes, s: *Surface, shown: tagline.Shown, lines: []const []const u8, top: usize, x: u16, y: u16, w: u16, lo: usize, hi: usize, ink: [3]u8, under: bool) void { const a = modal.positionAt(shown.text, shown.fromReal(lo)); const b = modal.positionAt(shown.text, shown.fromReal(hi)); var row = @max(a.row, top); while (row <= b.row and row < top + lines.len) : (row += 1) { const line = lines[row - top]; const from = if (row == a.row) a.col else 0; const to = if (row == b.row) b.col else line.len; var col = panes.File.rawDisplayCol(p.settings.tab, line, @min(from, line.len)); const end = panes.File.rawDisplayCol(p.settings.tab, line, @min(to, line.len)); const row_y = y + @as(u16, @intCast(row - top)); while (col < end and col < w) : (col += 1) { const style = &s.at(x + @as(u16, @intCast(col)), row_y).style; style.fg = .{ .rgb = ink }; if (under) style.ul = .single; } } } /// A pane's tag on its ground, focused or not, whatever its mode (G1': /// a reading pane is marked on its grip): its trail, path, `Keep` and the /// user's text in the full ink, its computed words in the words' ink, the /// basename in the name's tint, hints dim (DESIGN.md H). fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, id: usize, active: bool, rows: u16) !void { const chrome = p.chrome(); const g: colors.Chrome.Ground = if (active) .focus else .plain; const tx = r.x + @min(TAG_TEXT_INSET, r.w); const tw = r.w -| TAG_TEXT_INSET; const tag_y = p.tagTop(pane, r); // Focus is visible along the tag without changing its live text grid. const inks = chrome.on(g); const tag_bg = inks.ground; const tag_style: CellStyle = .{ .fg = .{ .rgb = inks.text }, .bg = .{ .rgb = tag_bg }, .font_role = .tagline }; // Padding belongs to the same font row but keeps its default ink: a // role must not manufacture a foreground color for blank cells. s.fill(tx, tag_y, tw, rows, .{ .bg = .{ .rgb = tag_bg }, .font_role = .tagline, }); const whole = try tagline.tagText(p, arena, pane); // A question in place of the tag's own text (TagState.asking). const question = try TagState.asking(p, arena, pane); const tail = if (question) |h| h.text else tagline.curTail(pane); const prefix_len = whole.len - tail.len; // A row per line shown, from `tag_top`: the tag's first line carries // the prefix, the rest are the tag's own text alone. const top = pane.tag_top; // Its lines wrapped at its width (tagline.shownTag): a row per row shown. const shown = try tagline.shownTag(p, arena, pane); var lines: [MAX_TAG_ROWS][]const u8 = @splat(""); var split = std.mem.splitScalar(u8, shown.text, '\n'); for (0..top) |_| _ = split.next(); for (lines[0..rows]) |*line| line.* = split.next() orelse continue; // The keyboard's caret, as a row and a byte offset in that row's // line: in the tag's own text when the tag has the keyboard, in the // path while a name is drafted. const Caret = struct { row: usize, at: usize }; const caret: ?Caret = if (!active) null else if (pane.prompt == .name) caret: { // A drafted name is the prefix: its caret is on row 0 of the tag, // the rows it wrapped onto included. const at = modal.positionAt(shown.text, shown.fromReal(tagline.nameAt(p, pane) + @as(usize, @intCast(@max(0, pane.input.cur_col))))); if (at.row < top or at.row >= top + rows) break :caret null; break :caret .{ .row = at.row - top, .at = @min(lines[at.row - top].len, at.col) }; } else if (pane.focus == .tag and pane.prompt == .none and question == null) caret: { const real = modal.offsetAt(whole, .{ .row = @intCast(@max(0, pane.tag.cur_row)), .col = @intCast(@max(0, pane.tag.cur_col)) }); const at = modal.positionAt(shown.text, shown.fromReal(real)); if (at.row < top or at.row >= top + rows) break :caret null; const row: usize = at.row - top; break :caret .{ .row = row, .at = @min(lines[row].len, at.col) }; } else null; // Its rows are wrapped to fit: a pane tag never scrolls sideways. pane.tag_scroll = 0; for (lines[0..rows], 0..) |line, row| { // The scroll is the caret's line's: a shorter row shows nothing. const start = @min(line.len, panes.File.rawAtDisplay(p.settings.tab, line, pane.tag_scroll)); _ = s.print(tx, tag_y + @as(u16, @intCast(row)), tw, line[start..], tag_style); } // The pane's progress at its first row's right end, where the row has // room for it (SPEC §10.2). if (top == 0 and rows > 0) for (p.messages.live[0..p.messages.live_len]) |*said| { if (said.kind != .progress or said.pane != pane.serial) continue; var buf: [Messages.LoggedMessage.cap + 8]u8 = undefined; const text = std.fmt.bufPrint(&buf, "{s} {s}", .{ Messages.Kind.progress.glyph(), said.words() }) catch break; const width = panes.File.displayWidth(p.settings.tab, text); const used = panes.File.displayWidth(p.settings.tab, lines[0]); if (used + 2 + width <= tw) _ = s.print(tx + @as(u16, @intCast(tw - width)), tag_y, @intCast(width), text, .{ .fg = .{ .rgb = Messages.inkOn(inks, .progress) }, .bg = .{ .rgb = tag_bg }, .font_role = .tagline }); break; }; // The name's tint, in the tag's first line, over whichever rows it // wrapped onto. const first_line = whole[0 .. std.mem.indexOfScalar(u8, whole, '\n') orelse whole.len]; // Its computed words (UX K'1), between its path and the user's text, // not asking: the words' ink, quieter than the full one. if (question == null) { const words = tagline.computedWords(pane); const end = prefix_len -| 1; inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, end -| words.len, @min(end, first_line.len), inks.word, false); } if (tagTintRange(p, pane, first_line, prefix_len)) |name| if (name.lo < name.hi) inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, name.lo, @min(name.hi, first_line.len), chrome.name[@intFromEnum(g)], false); // While its body previews a file not its own (a picker's: its own set // aside, or a kind shown over it), its path names a file the body is // not showing: the dim ink (the coordinator's ruling, UX DESIGN M7.2). if (pardes.Pick.previewing(p, id)) if (pardes.Pick.live(p)) |pk| { const own = if (pk.aside) |f| f.path else if (pane.file) |f| f.path else ""; const body = if (pane.addon) |k| k.path else if (pane.file) |f| f.path else ""; if (!std.mem.eql(u8, own, body)) if (tagTintRange(p, pane, first_line, prefix_len)) |name| inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, tagline.nameAt(p, pane), @min(name.hi, first_line.len), inks.dim, false); }; // A walk's forward steps, after the name, dimmed (Trail.zig); its fold // `+N` too; the word n walks underlined in the key ink (UX D8). const ahead = tagline.forwardSpan(p, pane); if (ahead[0] < ahead[1]) inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, ahead[0], @min(ahead[1], first_line.len), inks.dim, false); if (pane.file != null and pane.trail.n > 0) { const kept = tagline.folded(p, pane).whole; const t = &pane.trail; // Each run of folded places' `+N` (UX L'1: words never fold). for (0..t.backs()) |i| if (t.isFolded(i, kept) and !(i > 0 and t.isFolded(i - 1, kept))) { const fold = t.spanOf(i, kept); inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, fold[0], @min(fold[1], first_line.len), inks.dim, false); }; if (t.walked()) |i| if (!t.isFolded(i, kept)) { const word = t.spanOf(i, kept); inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, word[0], @min(word[1], first_line.len), inks.key, true); }; } // A search word its places no longer light, dimmed (Prompt.zig). if (pardes.Prompt.unlit(p, pane)) |span| inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, span[0], @min(span[1], first_line.len), inks.dim, false); // A question's keys in the key ink, its dimmed words (TagState.Span), // over whichever rows they wrapped onto. if (question) |h| for (h.spans) |span| { const a = modal.positionAt(shown.text, shown.fromReal(prefix_len + span.lo)); const b = modal.positionAt(shown.text, shown.fromReal(prefix_len + span.hi)); var row = @max(a.row, top); while (row <= b.row and row < top + rows) : (row += 1) { const line = lines[row - top]; const from = if (row == a.row) a.col else 0; const to = if (row == b.row) b.col else line.len; var col = panes.File.rawDisplayCol(p.settings.tab, line, @min(from, line.len)); const end = panes.File.rawDisplayCol(p.settings.tab, line, @min(to, line.len)); const y = tag_y + @as(u16, @intCast(row - top)); while (col < end and col < tw) : (col += 1) { const style = &s.at(tx + @as(u16, @intCast(col)), y).style; switch (span.style) { .key => { style.fg = .{ .rgb = inks.key }; style.bold = true; }, .dim => style.fg = .{ .rgb = inks.dim }, .under => style.ul = .single, .strike => style.strikethrough = true, .err => style.fg = .{ .rgb = inks.severity[0] }, .rule => style.fg = .{ .rgb = inks.rule }, } } } }; // The tag's own selections, while it has the keyboard, a line at a // time. A range of one grapheme is the block cursor, not a selection. if (pane.focus == .tag and question == null) { var ranges: [Text.max_selections]modal.Selection = undefined; const got = pane.tag.ranges(whole, 0, &ranges); for (ranges[0..got.n]) |range| { const lo = @min(range.anchor, range.head); const hi = @max(range.anchor, range.head); if (modal.nextGrapheme(whole, lo) >= hi) continue; const first = modal.positionAt(shown.text, shown.fromReal(lo)); const last = modal.positionAt(shown.text, shown.fromReal(hi)); var row = @max(first.row, top); while (row <= last.row and row < top + rows) : (row += 1) { const line = lines[row - top]; const from = if (row == first.row) first.col else 0; const to = @min(line.len, if (row == last.row) last.col else line.len); const col = panes.File.rawDisplayCol(p.settings.tab, line, from) -| pane.tag_scroll; const end = @min(panes.File.rawDisplayCol(p.settings.tab, line, to) -| pane.tag_scroll, tw); if (col < end) s.tint(tx + @as(u16, @intCast(col)), tag_y + @as(u16, @intCast(row - top)), @intCast(end - col), inks.selection.ground, inks.selection.text); } } } // the caret: bar while typing, block in normal mode (same rule as a body) if (caret) |at| { const col = panes.File.rawDisplayCol(p.settings.tab, lines[at.row], at.at) -| pane.tag_scroll; const mode = if (pane.prompt == .name) pane.input.mode else pane.tag.mode; if (col < tw) s.cursor = .{ .x = tx + @as(u16, @intCast(col)), .y = tag_y + @as(u16, @intCast(at.row)), .bar = mode == .insert }; } // Pointer sweeps over the tag rows. A select sweep is the tag's own, in // its selection colours; the look-hover affordance goes over it, and a // middle or right sweep over both, tinted by its button the way the // body tints it. for (pane.sel, 0..) |selection, button| { if (selection.state == .none or (selection.state == .done and button != 0)) continue; if (button != pardes.sel_slot or @min(selection.r0, selection.r1) >= rows) continue; body_layer.paintPointerSelection(p, s, pane, r, selection, 0, inks.selection.ground, inks.selection.text); } if (p.look_hover_preview) |preview| { if (preview.pane == id and preview.serial == pane.serial) if (preview.sel) |sel| if (@min(sel.r0, sel.r1) < rows) { body_layer.paintPointerSelection(p, s, pane, r, sel, 0, inks.selection.secondary, null); }; } const sweeps = &chrome.sweeps; for (pane.sel, 0..) |selection, button| { if (selection.state == .none or (selection.state == .done and button != 0)) continue; if (button == pardes.sel_slot or @min(selection.r0, selection.r1) >= rows) continue; body_layer.paintPointerSelection(p, s, pane, r, selection, 0, sweeps[button].ground, sweeps[button].text); } } test "a grip in the grid is the chrome's in every theme: focused the accent, at rest its ring, reading the mode's mark (its glyph on a focused one), unsaved a bold `*` on the dirty fill; the four each its own (DESIGN.md R5', R6, G1')" { const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 16 }); defer p.deinit(); const pane = try p.setTestFile("text\n"); p.newScratchBelow(0); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); const Grip = struct { first: Cell, second: Cell }; const State = struct { active: bool, held: bool, dirty: bool }; const states = [_]State{ .{ .active = true, .held = true, .dirty = false }, .{ .active = false, .held = true, .dirty = false }, .{ .active = false, .held = false, .dirty = false }, .{ .active = false, .held = true, .dirty = true }, .{ .active = true, .held = false, .dirty = false }, }; var failed: usize = 0; for (pardes.themes, 0..) |theme, index| { colors.setThemeIndex(p, index); const grip = &p.chrome().grip; var drawn: [states.len]Grip = undefined; for (states, &drawn) |state, *got| { p.view().active = if (state.active) 0 else 1; pane.trail.held = state.held; const saved = pane.file.?.saved_revision; pane.file.?.revision = if (state.dirty) saved + 1 else saved; _ = arena.reset(.retain_capacity); const s = try p.render(arena.allocator()); const r = p.view().rects[0]; const y = p.tagTop(pane, r); got.* = .{ .first = s.at(r.x, y).*, .second = s.at(r.x + 1, y).* }; } pane.file.?.revision = pane.file.?.saved_revision; const want = [_][2][3]u8{ .{ grip.focus, grip.ink }, .{ grip.ring, p.chrome().tag[0].text }, .{ grip.mode, grip.ink }, .{ grip.ring, p.chrome().tag[0].text }, .{ grip.focus, grip.mode_glyph }, }; var ok = true; for (drawn, want) |got, w| ok = ok and std.meta.eql(got.first.style.bg, Color{ .rgb = w[0] }) and std.meta.eql(got.first.style.fg, Color{ .rgb = w[1] }); // Dirty is more than a shade: the grid prints a bold `*` (one grid // for every shell; a pixel shell draws its button over it). const star = drawn[3].second; ok = ok and std.mem.eql(u8, star.grapheme(), "*") and star.style.bold and std.meta.eql(star.style.bg, Color{ .rgb = grip.dirty }) and std.meta.eql(star.style.fg, Color{ .rgb = grip.ink }); for (drawn[0..4], 0..) |a, i| for (drawn[i + 1 .. 4]) |b| { ok = ok and !(std.meta.eql(a.first.style.bg, b.first.style.bg) and a.second.visuallyEqual(&b.second)); }; if (!ok) { std.debug.print("{s}: a grip not the chrome's, or two states alike\n", .{theme.name}); failed += 1; } } pane.trail.held = false; try std.testing.expectEqual(@as(usize, 0), failed); } test "every theme draws a file's name, a +Errors and a Term word in a tint, never the tag's plain ink" { if (pardes.Term.ops == null) return error.SkipZigTest; // a shell's Term word const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .cols = 120, .rows = 40, .tty_only = true }); defer p.deinit(); _ = try p.setTestFile("text\n"); try std.testing.expect(p.executeBuiltinLine(0, "Term+sh")); const terminal = p.view().active; p.acknowledgeShell(.at(terminal), "/bin/sh", false); try @import("Output.zig").openErrors(p, 0, try gpa.dupe(u8, "oops\n")); while (p.nextEffect()) |_| {} const errors = for (p.panes, 0..) |slot, id| { const pane = slot orelse continue; if (pane.file) |f| if (std.mem.endsWith(u8, f.path, "+Errors")) break id; } else return error.NoErrorsPane; const Probe = struct { id: usize, word: []const u8 }; const probes = [_]Probe{ .{ .id = 0, .word = "test.txt" }, .{ .id = terminal, .word = "Term+sh" }, .{ .id = errors, .word = "+Errors" } }; var arena: std.heap.ArenaAllocator = .init(gpa); defer arena.deinit(); for (pardes.themes, 0..) |theme, index| { colors.setThemeIndex(p, index); // Each pane focused in turn: every word on both grounds. for (probes) |focused| { p.view().active = focused.id; _ = arena.reset(.retain_capacity); const s = try p.render(arena.allocator()); for (probes) |probe| { const r = p.view().rects[probe.id]; const y = p.tagTop(p.panes[probe.id].?, r); // The row's text, and the column each byte of it is drawn at. var row: [512]u8 = undefined; var cols: [512]u16 = undefined; var len: usize = 0; for (r.x..r.x + r.w) |x| { const g = s.at(@intCast(x), y).grapheme(); for (if (g.len == 0) " " else g) |byte| if (len < row.len) { row[len] = byte; cols[len] = @intCast(x); len += 1; }; } const name = std.mem.indexOf(u8, row[0..len], probe.word) orelse return error.WordNotDrawn; const tint = s.at(cols[name], y).style.fg; const ink: Color = .{ .rgb = p.chrome().on(if (focused.id == probe.id) .focus else .plain).text }; if (std.meta.eql(tint, ink)) std.debug.print("{s}: {s} drawn in the tag's ink\n", .{ theme.name, probe.word }); try std.testing.expect(!std.meta.eql(tint, ink)); // The whole word, in the one tint. try std.testing.expect(std.meta.eql(s.at(cols[name + probe.word.len - 1], y).style.fg, tint)); } } } } test "a tag of three rows has its grip on the first, and band under it that still grabs the pane" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 30 }); defer p.deinit(); const pane = try p.setTestFile("text\n"); pane.tag.own = try std.testing.allocator.dupe(u8, "Second\nthird\nfourth"); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); p.sync(); _ = arena.reset(.retain_capacity); const s = try p.render(arena.allocator()); const r = p.view().rects[0]; const top = p.tagTop(pane, r); try std.testing.expect(pane.tag_rows >= 3); const chrome = p.chrome(); const band = chrome.on(.focus).ground; for (0..config.GUTTER) |dx| { const x = r.x + @as(u16, @intCast(dx)); try std.testing.expectEqual(Color{ .rgb = chrome.grip.focus }, s.at(x, top).style.bg); for (1..pane.tag_rows) |line| { const cell = s.at(x, top + @as(u16, @intCast(line))); try std.testing.expectEqual(Color{ .rgb = band }, cell.style.bg); try std.testing.expectEqualStrings(" ", cell.grapheme()); } } try std.testing.expectEqual(pane.tag_rows, s.region(.grip, 0).?.rect.h); } test "place puts every part where the painters and layers put it, in every layout" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 30 }); defer p.deinit(); _ = try p.setTestFile(std.mem.asBytes(&@as([80][7]u8, @splat("placed\n".*)))); layout.newColumn(p, 0); layout.newScratchIn(p, 0, layout.activeColumn(p).?); p.newScratchBelow(0); p.sync(); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); for (0..16) |step| { if (step & 4 != 0) try std.testing.expect(p.executeBuiltinLine(p.view().active, "Msg a placed notice")); if (step == 8) try std.testing.expect(p.executeBuiltinLine(1, "Collapse")); if (step == 12) try std.testing.expect(p.executeBuiltinLine(1, "Collapse")); if (step == 10) { const pane = p.panes[p.view().active].?; pane.tag.own = try std.testing.allocator.dupe(u8, "Second\nthird"); } p.sync(); _ = arena.reset(.retain_capacity); const s = try p.render(arena.allocator()); try std.testing.expectEqual(Region.Kind.page, s.regions[0].kind); for (p.panes, 0..) |slot, id| { const pane = slot orelse continue; const r = p.view().rects[id]; if (r.w <= config.GUTTER or r.h == 0) continue; const grip = s.region(.grip, id).?; try std.testing.expectEqual(Rect{ .x = r.x, .y = p.tagTop(pane, r), .w = config.GUTTER, .h = pane.tag_rows }, grip.rect); // The grid's grip cell is the grip's. try std.testing.expectEqual(FontRole.tagline, s.at(grip.rect.x, grip.rect.y).style.font_role); const tag = s.region(.tag, id).?; try std.testing.expectEqual(Rect{ .x = r.x + config.GUTTER, .y = p.tagTop(pane, r), .w = r.w - config.GUTTER, .h = pane.tag_rows }, tag.rect); // One layer, a row to each line of the tag. if (r.w > TAG_TEXT_INSET) { try std.testing.expectEqual(Rect{ .x = r.x + TAG_TEXT_INSET, .y = p.tagTop(pane, r), .w = r.w - TAG_TEXT_INSET, .h = pane.tag_rows }, s.tag_layers[id].viewport); try std.testing.expectEqual(pane.tag_rows, s.tag_layers[id].rows); } if (pane.collapsed) { try std.testing.expect(s.region(.body, id) == null); try std.testing.expect(s.region(.rail, id) == null); continue; } const body = s.region(.body, id).?; try std.testing.expectEqual(Rect{ .x = r.x + config.GUTTER, .y = p.bodyTop(pane, r), .w = r.w - config.GUTTER, .h = r.h - pane.tag_rows }, body.rect); const rail = s.region(.rail, id).?; // The painted thumb is exactly the placed one. for (0..rail.rect.h) |row| { const y = rail.rect.y + @as(u16, @intCast(row)); const thumb = row >= rail.thumb_y and row < rail.thumb_y + rail.thumb_h; const want = if (thumb) p.chrome().rail_thumb else p.chrome().rail; try std.testing.expectEqual(Color{ .rgb = want }, s.at(rail.rect.x, y).style.bg); } var notices: usize = 0; for (s.regionList()) |region| { if (region.kind != .notice or region.owner != id) continue; notices += 1; try std.testing.expectEqual(pane.notices.left[region.notice], region.rect.x); try std.testing.expectEqual(r.x + r.w, region.rect.x + region.rect.w); try std.testing.expectEqual(p.bodyTop(pane, r) + pane.notices.row[region.notice], region.rect.y); try std.testing.expectEqual(region.rect, s.tag_layers[NOTICE_LAYER_BASE + id * Pane.Notices.max + region.notice].viewport); } try std.testing.expectEqual(@as(usize, pane.notices.len), notices); } for (0..p.view().ncol) |c| { const region = s.region(.column_tag, c); if (layout.columnBarHeight(p) == 0) { try std.testing.expect(region == null); continue; } try std.testing.expectEqual(Rect{ .x = p.view().col_x[c] + TAG_TEXT_INSET, .y = p.topBarHeight(), .w = p.view().col_w[c] - TAG_TEXT_INSET, .h = layout.columnBarHeight(p) }, region.?.rect); } } } test "the word under the pointer on the notice row is lit in its layer and in the grid's copy" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 61, .rows = 12 }); defer p.deinit(); _ = try p.setTestFile("text\n"); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); try std.testing.expect(p.executeBuiltinLine(p.view().active, "Msg Look here")); p.sync(); var s = try p.render(arena.allocator()); const foot = s.region(.notice_row, Region.none) orelse return error.TestExpectedNoticeRow; try std.testing.expectEqual(s.rows - 1, foot.rect.y); const row = s.cells[@as(usize, foot.rect.y) * s.cols ..][0..s.cols]; // "here": the message's last word, `· /test.txt Look here`, 17 // columns in. const here: u16 = TAG_TEXT_INSET + 17; try std.testing.expectEqualStrings("h", row[here].grapheme()); p.view().pointer_inside = true; p.view().hover_col = here + 1; p.view().hover_row = foot.rect.y; _ = arena.reset(.retain_capacity); s = try p.render(arena.allocator()); const lit = s.cells[@as(usize, foot.rect.y) * s.cols ..][0..s.cols]; const sel: Color = .{ .rgb = p.chrome().status.selection.secondary }; for (lit[here..][0..4]) |cell| try std.testing.expectEqual(sel, cell.style.bg); try std.testing.expect(!std.meta.eql(sel, lit[here - 2].style.bg)); const layer = &s.tag_layers[pardes.NOTICE_ROW_LAYER]; try std.testing.expectEqual(TagKind.notice_row, layer.kind); const lit_in_layer = for (layer.cells) |cell| { if (std.meta.eql(sel, cell.style.bg)) break true; } else false; try std.testing.expect(lit_in_layer); } test "a message wider than the notice row, as a shader's compile error is, gives way to the row's right end and draws nothing past the row" { const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 20 }); defer p.deinit(); // A gui-shaped core: its tagline is narrower than the body, so the // row's layer holds more columns than the grid has cells. p.update(.{ .resize = .{ .cols = 80, .rows = 20, .row_metrics = .{ .body_w = 10, .body_h = 20, .tagline_w = 6, .tagline_h = 12 } } }); _ = try p.setTestFile("text\n"); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); const long = std.mem.asBytes(&@as([3][126]u8, @splat("Shader /home/someone/shaders/test_shadertoy_invalid.glsl: error: Linking fragment stage: No function definition (body) found: ".*))); for ([_][]const u8{ "Msg " ++ long, "" }) |line| { // The Msg builtin, and the path a failed compile takes (Post.zig). if (line.len > 0) try std.testing.expect(p.executeBuiltinLine(p.view().active, line)) else p.setMessage(.at(p.view().active), long); p.sync(); for (0..30) |_| p.update(.tick); _ = arena.reset(.retain_capacity); const s = try p.render(arena.allocator()); const layer = &s.tag_layers[pardes.NOTICE_ROW_LAYER]; try std.testing.expectEqual(layout.tagCapacity(p, s.cols - TAG_TEXT_INSET), layer.cols); // The counts at the row's end, whole, a blank cell short of it. const cells = layer.cells[0..layer.cols]; try std.testing.expectEqualStrings(" ", cells[cells.len - 1].grapheme()); try std.testing.expectEqualStrings("m", cells[cells.len - 2].grapheme()); } } test "a monkey over notices, prompts, tags and tiny panes at a gui's metrics never panics" { // The snapshot harness runs the tty, whose tagline pitch is the body's, // so it never reaches what a narrower tagline does to a notice's band: // this drives the core at a gui's metrics instead. PARDES_FUZZ_STEPS // runs it longer (the suite's run is short). const steps: usize = if (std.c.getenv("PARDES_FUZZ_STEPS")) |v| std.fmt.parseInt(usize, std.mem.span(v), 10) catch 400 else 400; const seed: u64 = if (std.c.getenv("PARDES_FUZZ_SEED")) |v| std.fmt.parseInt(u64, std.mem.span(v), 10) catch 7 else 7; var prng = std.Random.DefaultPrng.init(seed); const random = prng.random(); const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24 }); defer p.deinit(); _ = try p.setTestFile(std.mem.asBytes(&@as([20][17]u8, @splat("fuzz\n\u{754c}\u{754c} wide\n".*)))); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); const words = [_][]const u8{ "error:", "\u{754c}", "x", "Shader /tmp/a.glsl:", "e\u{301}", "\t", std.mem.asBytes(&@as([20][5]u8, @splat("long-".*))), " " }; const commands = [_][]const u8{ "New", "Newcol", "Collapse", "TreeContext", "Wrap", "Find", "Del", "Debug" }; var text: std.ArrayListUnmanaged(u8) = .empty; defer text.deinit(std.testing.allocator); for (0..steps) |_| { switch (random.uintLessThan(u8, 8)) { // A resize, down to a cell, at a gui's metrics: a tagline no // wider and no taller than the body's cell, often narrower. 0 => { const body_w = random.intRangeAtMost(u16, 6, 12); const body_h = random.intRangeAtMost(u16, 12, 24); const cols = if (random.boolean()) random.intRangeAtMost(u16, 1, 12) else random.intRangeAtMost(u16, 13, 140); const rows = if (random.boolean()) random.intRangeAtMost(u16, 1, 6) else random.intRangeAtMost(u16, 7, 50); p.update(.{ .resize = .{ .cols = cols, .rows = rows, .row_metrics = .{ .body_w = body_w, .body_h = body_h, .tagline_w = random.intRangeAtMost(u16, 2, body_w), .tagline_h = random.intRangeAtMost(u16, 6, body_h) } } }); }, // A message, as long as a compile error or longer. 1, 2 => { text.clearRetainingCapacity(); for (0..random.uintAtMost(usize, 60)) |_| try text.appendSlice(std.testing.allocator, words[random.uintLessThan(usize, words.len)]); if (random.boolean()) p.setMessage(.at(p.view().active), text.items) else { const line = try std.fmt.allocPrint(arena.allocator(), "Msg {s}", .{text.items}); _ = p.executeBuiltinLine(p.view().active, line); } }, 3 => _ = p.executeBuiltinLine(p.view().active, commands[random.uintLessThan(usize, commands.len)]), // Typing, escaping and entering, into a prompt when one is up. 4 => p.update(.{ .key = .{ .cp = switch (random.uintLessThan(u8, 9)) { 0 => pardes.Key.escape, 1 => pardes.Key.enter, 2 => 0x754c, 3 => pardes.Key.end, 4 => pardes.Key.home, 5 => pardes.Key.up, 6 => pardes.Key.down, else => 'a' + random.uintLessThan(u21, 26), } } }), // A tag of several rows. 5 => if (p.panes[p.view().active]) |pane| { text.clearRetainingCapacity(); for (0..random.uintAtMost(usize, 5)) |_| try text.appendSlice(std.testing.allocator, if (random.boolean()) " Look\nmore \u{754c}" else "\n" ++ &@as([150]u8, @splat('x'))); if (pane.tag.own) |own| std.testing.allocator.free(own); pane.tag.own = try std.testing.allocator.dupe(u8, text.items); }, // Hover, and clicks: into a tag, a header, a body, a grip. 6 => { const col = random.uintAtMost(u16, p.view().screen_w); const row = random.uintAtMost(u16, p.view().screen_h); if (random.boolean()) p.update(.{ .mouse = .{ .button = .none, .kind = .motion, .col = col, .row = row } }) else { p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = col, .row = row } }); p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = col, .row = row } }); } }, else => for (0..random.uintAtMost(usize, 20)) |_| p.update(.tick), } p.sync(); _ = arena.reset(.retain_capacity); _ = try p.render(arena.allocator()); p.presented(); } } test "a wide glyph cut by a tag's or a notice's edge is joined into the grid as blank, never half drawn" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 41, .rows = 12 }); defer p.deinit(); _ = try p.setTestFile("wide\n"); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); const pane = p.panes[p.view().active].?; // Both alignments, so one of them puts a wide glyph across each edge. for ([_][]const u8{ std.mem.asBytes(&@as([30][3]u8, @splat("\u{754c}".*))), "x" ++ std.mem.asBytes(&@as([30][3]u8, @splat("\u{754c}".*))) }) |wide| { if (pane.tag.own) |own| std.testing.allocator.free(own); pane.tag.own = try std.testing.allocator.dupe(u8, wide); var message: [200]u8 = undefined; try std.testing.expect(p.executeBuiltinLine(p.view().active, try std.fmt.bufPrint(&message, "Msg {s}", .{wide}))); p.sync(); _ = arena.reset(.retain_capacity); const s = try p.render(arena.allocator()); for (s.regionList()) |region| { if (region.kind != .tag and region.kind != .notice) continue; const row = s.cells[@as(usize, region.rect.y) * s.cols ..][region.rect.x..][0..region.rect.w]; for (row, 0..) |cell, i| { const spacer = !cell.default and cell.len == 0; const head = !cell.default and cell.len > 1 and i + 1 < row.len and !row[i + 1].default and row[i + 1].len == 0; // A spacer has its head before it; a head its spacer after. if (spacer) try std.testing.expect(i > 0 and row[i - 1].len > 1); if (i + 1 == row.len) try std.testing.expect(!head and !(cell.len > 1 and vaxis.gwidth.gwidth(cell.grapheme(), .unicode) == 2)); } // The right edge of each is the band, blank where a glyph was cut. try std.testing.expectEqual(FontRole.tagline, row[row.len - 1].style.font_role); } } } test "a body with context rows is painted once, and the grid's rows are what a plain paint of it gives" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 16 }); defer p.deinit(); const pane = try p.setTestFile("pub const Box = struct {\n pub fn run() void {\n" ++ (std.mem.asBytes(&@as([30][21]u8, @splat(" // body line\n".*)))) ++ " }\n};\n"); p.gpa.free(pane.file.?.path); pane.file.?.path = try p.gpa.dupe(u8, "/test.zig"); if (!panes.File.supportsContext(pane)) return error.SkipZigTest; pane.file.?.tree_context = true; pane.file.?.scroll = 8; var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); // A selection and the cursor on body rows, painted in the one pass. pane.body.cur_row = 12; pane.body.cur_col = 3; pane.body.vsel = .{ .active = true, .row = 10, .col = 2 }; pane.body.mode = .normal; p.sync(); _ = arena.reset(.retain_capacity); const s = try p.render(arena.allocator()); const layer = &s.body_layers[0]; try std.testing.expect(layer.context_rows > 0); const body = s.region(.body, 0).?; // The plain paint the grid used to take, of the same frame. const cells = try arena.allocator().alloc(Cell, s.cells.len); @memset(cells, .{}); var plain: Surface = .{ .cols = s.cols, .rows = s.rows, .cells = cells }; pane.view.layered = false; try body_layer.renderBody(p, &plain, arena.allocator(), pane, p.view().rects[0], 0, true, null); pane.view.layered = true; for (0..body.rect.h) |row| { const y = body.rect.y + row; for (0..body.rect.w) |col| { const x = body.rect.x + col; const got = s.cells[y * s.cols + x]; const want = plain.cells[y * s.cols + x]; try std.testing.expect(got.visuallyEqual(&want)); } } try std.testing.expectEqual(plain.cursor, s.cursor); // The layer's own rows run past the grid's. try std.testing.expect(layer.rows > body.rect.h); } // The class kept closed: a frame changes nothing a step reads. What a frame // writes of a core is `.local` or `.derived` (in no digest and no dump: a // pane's notice bands, the geometry's wrap map and context rows, a markdown // text's tables), so a step that reads one must find what its own state // makes. Here one core draws after every input, each frame at its own time // after its step's, and one joined from its dump never draws: each input's // step leaves the two with one digest. A field a frame leaves that a step // then reads otherwise than a core that did not draw makes it, differs here. // Not seen: a core joined later (its authority's reads are the tape's, which // this has none of: the soak's late joins), and what no input here reaches. test "a frame changes nothing a step reads: a core drawing after every input and one never drawing agree" { const gpa = std.testing.allocator; const steps: usize = if (std.c.getenv("PARDES_FUZZ_STEPS")) |v| std.fmt.parseInt(usize, std.mem.span(v), 10) catch 600 else 600; const seed: u64 = if (std.c.getenv("PARDES_FUZZ_SEED")) |v| std.fmt.parseInt(u64, std.mem.span(v), 10) catch 11 else 11; var prng = std.Random.DefaultPrng.init(seed); const random = prng.random(); var text: std.ArrayList(u8) = .empty; defer text.deinit(gpa); for (0..4500) |i| try text.print(gpa, "line {d} {s}\n", .{ i, std.mem.asBytes(&@as([20][6]u8, @splat("words ".*)))[0 .. 6 * (i % 20)] }); try text.appendSlice(gpa, "\n| name | qty | note |\n|:--|--:|:-:|\n| fig | 1 | ripe |\n| apple || |\n| a longer name | 12345 | x |\n\n## end\n"); const metrics: pardes.RowMetrics = .{ .body_w = 10, .body_h = 20, .tagline_w = 6, .tagline_h = 12 }; const a = try Pardes.init(gpa, .{ .tty_only = true, .cols = 90, .rows = 30 }); defer a.deinit(); a.update(.{ .resize = .{ .cols = 90, .rows = 30, .row_metrics = metrics } }); const pane = try a.setTestFile(text.items); a.gpa.free(pane.file.?.path); pane.file.?.path = try a.gpa.dupe(u8, "/notes.md"); a.newScratchBelow(0); a.view().active = 0; pane.file.?.scroll = 4490; pane.body.cur_row = 4502; a.sync(); const first = try pardes.dump.dumpBytes(a); defer gpa.free(first); const b = try pardes.dump.load(gpa, a.opts, first, null); defer b.deinit(); var arena: std.heap.ArenaAllocator = .init(gpa); defer arena.deinit(); const commands = [_][]const u8{ "Wrap", "Collapse", "Del", "Undo", "Redo" }; var now: u64 = std.time.ns_per_s; for (0..steps) |i| { const v = a.view(); // Most presses on the body around the table at the text's end. const body = v.rects[0]; const ev: pardes.Event = switch (random.uintLessThan(u8, 13)) { 0...4 => .{ .key = .{ .cp = switch (random.uintLessThan(u8, 16)) { 0 => pardes.Key.escape, 1 => 'i', 2 => 'G', 3 => 'g', 4 => ' ', 5 => 'd', 6 => 'w', 7 => 'b', 8 => 'u', 9, 10 => 'j', 11, 12 => 'k', 13 => 'h', 14 => 'l', else => 'x', } } }, 5...7 => .{ .mouse = .{ .button = if (random.uintLessThan(u8, 4) == 0) .wheel_up else if (random.uintLessThan(u8, 4) == 0) .wheel_down else .left, .kind = switch (random.uintLessThan(u8, 3)) { 0 => .press, 1 => .drag, else => .release, }, .col = if (random.boolean()) random.uintLessThan(u16, v.screen_w) else body.x + random.uintLessThan(u16, @max(1, @min(body.w, 40))), .row = if (random.boolean()) random.uintLessThan(u16, v.screen_h) else body.y + random.uintLessThan(u16, @max(1, body.h)), } }, 8 => .{ .command = commands[random.uintLessThan(usize, commands.len)] }, 9 => .{ .paste = "| new | row |\n" }, // The text changed on disk: a step changes it whole. 10 => .{ .file_changed = .{ .pane = .at(0), .bytes = text.items[0 .. text.items.len - random.uintLessThan(usize, 40)] } }, else => blk: { now += random.uintLessThan(u64, 2 * std.time.ns_per_s); break :blk .tick; }, }; for ([_]*Pardes{ a, b }) |p| { p.clock.now_ns = now; p.clock.stepped_ns = now; p.update(ev); } // A frame is drawn at its own time, after its step's. now += random.uintLessThan(u64, std.time.ns_per_s); a.clock.now_ns = now; b.clock.now_ns = now; _ = arena.reset(.retain_capacity); _ = try a.render(arena.allocator()); a.presented(); b.presented(); if (apart(a, b)) { std.debug.print("seed {d}, step {d}: a {s} left them apart\n", .{ seed, i, @tagName(ev) }); return error.DigestsDiffer; } } } /// Whether two cores' digests differ, field by field (the panes' and their /// files' too), the fields that do named; but for the three that differ /// between two cores in one test process whatever they do: a message's /// stamp and a text's mtime, read off the wall clock with no tape, and a /// scratch pane's cwd, inherited by pointer on one and owned bytes on one /// loaded from a dump. fn apart(a: *const Pardes, b: *const Pardes) bool { var any = false; const names = comptime pardes.walk.fieldNames(Pardes); var da: [names.len]u64 = undefined; var db: [names.len]u64 = undefined; _ = pardes.walk.fieldDigests(Pardes, a, &da); _ = pardes.walk.fieldDigests(Pardes, b, &db); for (names, da, db) |name, x, y| if (x != y and !std.mem.eql(u8, name, "panes")) { std.debug.print(" {s}\n", .{name}); any = true; }; const pane_names = comptime pardes.walk.fieldNames(Pane); for (a.panes, b.panes, 0..) |pa, pb, id| { if ((pa == null) != (pb == null)) { std.debug.print(" pane {d} open on one\n", .{id}); any = true; } const x = pa orelse continue; const y = pb orelse continue; var pda: [pane_names.len]u64 = undefined; var pdb: [pane_names.len]u64 = undefined; _ = pardes.walk.fieldDigests(Pane, x, &pda); _ = pardes.walk.fieldDigests(Pane, y, &pdb); for (pane_names, pda, pdb) |name, u, v| if (u != v and !std.mem.eql(u8, name, "cwd")) { if (std.mem.eql(u8, name, "file")) if (x.file) |*fx| if (y.file) |*fy| { const file_names = comptime pardes.walk.fieldNames(panes.File.State); var fda: [file_names.len]u64 = undefined; var fdb: [file_names.len]u64 = undefined; _ = pardes.walk.fieldDigests(panes.File.State, fx, &fda); _ = pardes.walk.fieldDigests(panes.File.State, fy, &fdb); for (file_names, fda, fdb) |file_name, q, r| if (q != r and !std.mem.eql(u8, file_name, "mtime")) { std.debug.print(" pane {d}.file.{s}\n", .{ id, file_name }); any = true; }; continue; }; std.debug.print(" pane {d}.{s}\n", .{ id, name }); any = true; }; } return any; } test "progress is said at the right end of its tag: a pane's at its tag's, a server's at the workspace tag's, and it goes when it ends" { const p = try Pardes.init(std.testing.allocator, .{ .cols = 160, .rows = 20 }); defer p.deinit(); const pane = try p.setTestFile("text\n"); const id = p.view().active; var frame = std.heap.ArenaAllocator.init(std.testing.allocator); defer frame.deinit(); var buf: [512]u8 = undefined; const Row = struct { fn of(s: *const Surface, y: u16, out: []u8) []const u8 { var n: usize = 0; for (0..s.cols) |x| { const g = s.cells[@as(usize, y) * s.cols + x].grapheme(); @memcpy(out[n..][0..g.len], g); n += g.len; } return std.mem.trimEnd(u8, out[0..n], " "); } }; p.setStatus(.at(id), "Changes: jj\u{2026}"); p.setStatus(.at(pardes.MAX_PANES), "--:--:-- lsp zls: indexing 3/10"); p.sync(); var s = try p.render(frame.allocator()); const tag_y = p.tagTop(pane, p.view().rects[id]); try std.testing.expect(std.mem.endsWith(u8, Row.of(s, tag_y, &buf), "\u{22ef} Changes: jj\u{2026}")); try std.testing.expect(std.mem.endsWith(u8, Row.of(s, 0, &buf), "\u{22ef} lsp zls: indexing 3/10")); // Its mark meanwhile, and neither is the notice row's. try std.testing.expectEqualStrings("\u{22ef}", s.at(p.view().rects[id].x + config.GUTTER - 1, tag_y).grapheme()); try std.testing.expect(p.messages.headline() == null); p.setStatus(.at(id), ""); _ = frame.reset(.retain_capacity); s = try p.render(frame.allocator()); try std.testing.expect(std.mem.indexOf(u8, Row.of(s, tag_y, &buf), "\u{22ef}") == null); } test "a tag out of rest draws its keys bold in the key ink and its hint in the dim ink, never faint (UX D4)" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 200, .rows = 30 }); defer p.deinit(); const pane = try p.setTestFile("one\n"); var frame = std.heap.ArenaAllocator.init(std.testing.allocator); defer frame.deinit(); const a = frame.allocator(); const inks = p.chrome().on(.focus); // SPC's level rolled, after its delay: `␣ ▸ … l lsp… … ? help`. p.clock.now_ns = 5 * std.time.ns_per_s; p.clock.stepped_ns = p.clock.now_ns; p.update(.{ .key = .{ .cp = ' ' } }); p.advance(p.clock.now_ns); p.clock.now_ns = p.view().tag_state.due_ns; p.sync(); const s = try p.render(a); const r = p.view().rects[p.view().active]; const drawn = pardes.Band.covered(p, pane, p.view().active); const row = s.cells[@as(usize, r.y + drawn.tag) * s.cols ..][0 .. @as(usize, s.cols) * drawn.head]; const Row = struct { fn find(cells: []const Cell, word: []const u8) ?usize { var at: usize = 0; while (at + word.len <= cells.len) : (at += 1) { for (word, cells[at..][0..word.len]) |c, cell| { if (cell.len != 1 or cell.text[0] != c) break; } else return at; } return null; } }; const hint = Row.find(row, "? help").?; try std.testing.expectEqual(Color{ .rgb = inks.dim }, row[hint].style.fg); for (row) |cell| try std.testing.expect(!cell.style.dim); var keys: usize = 0; for (row) |cell| if (std.meta.eql(cell.style.fg, Color{ .rgb = inks.key })) { try std.testing.expect(cell.style.bold); keys += 1; }; try std.testing.expect(keys > 3); } test "a pane tag's trail, path, Keep and the user's text are in its full ink, its computed words in the words' ink; the window row and the column tags on the page in its words' ink, a column's grip at rest 2:1; the notice row on its own ground (DESIGN.md H)" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 30 }); defer p.deinit(); colors.setThemeIndex(p, colors.themeIndex("dark").?); const pane = try p.setTestFile("one\n"); pane.tag.own = try p.gpa.dupe(u8, "mine"); const id = p.view().active; try std.testing.expect(p.executeBuiltinLine(id, "Newcol")); p.view().active = id; p.sync(); var frame = std.heap.ArenaAllocator.init(std.testing.allocator); defer frame.deinit(); const s = try p.render(frame.allocator()); const chrome = p.chrome(); const Find = struct { fn at(sf: *Surface, y: u16, word: []const u8) ?u16 { var x: u16 = 0; while (x + word.len <= sf.cols) : (x += 1) { for (word, 0..) |c, i| { const cell = sf.at(x + @as(u16, @intCast(i)), y); if (cell.len != 1 or cell.text[0] != c) break; } else return x; } return null; } }; const inks = chrome.on(.focus); try std.testing.expect(!std.mem.eql(u8, &inks.word, &inks.text)); const y = p.tagTop(pane, p.view().rects[id]); for ([_][]const u8{ "Keep", "mine" }) |word| try std.testing.expectEqual(Color{ .rgb = inks.text }, s.at(Find.at(s, y, word).?, y).style.fg); try std.testing.expectEqual(Color{ .rgb = inks.word }, s.at(Find.at(s, y, "Del").?, y).style.fg); try std.testing.expectEqual(Color{ .rgb = inks.text }, s.at(Find.at(s, y, "/test.txt").?, y).style.fg); // The window row's and the column tags' words, on the page (dark's the // terminal's own). const page = &chrome.page; try std.testing.expect(chrome.host_page); try std.testing.expect(layout.columnBarHeight(p) > 0); const column_y = p.topBarHeight(); for ([_]struct { y: u16, word: []const u8 }{ .{ .y = 0, .word = "Exit" }, .{ .y = column_y, .word = "Delcol" } }) |at| { const x = Find.at(s, at.y, at.word).?; try std.testing.expectEqual(Color{ .rgb = page.word }, s.at(x, at.y).style.fg); try std.testing.expectEqual(@as(Color, .default), s.at(x, at.y).style.bg); } // Each column's grip at rest, its active one's too: never focus. for (0..p.view().ncol) |c| try std.testing.expectEqual(Color{ .rgb = chrome.grip.column_rest }, s.at(p.view().col_x[c], column_y).style.bg); if (s.region(.notice_row, Region.none)) |row| try std.testing.expectEqual(Color{ .rgb = chrome.status.ground }, s.at(row.rect.x, row.rect.y).style.bg); } test "a reading pane's tag is on the plain or the focused ground, as a held one's; its grip carries the mode's mark, the held one's its ring (DESIGN.md G1', acme too)" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 30 }); defer p.deinit(); const pane = try p.setTestFile("one\n"); const reader = p.view().active; p.newScratchBelow(reader); const other = p.view().active; try std.testing.expect(other != reader); var frame = std.heap.ArenaAllocator.init(std.testing.allocator); defer frame.deinit(); for ([_][]const u8{ "acme", "orchard", "dark", "neonotte84" }) |name| { colors.setThemeIndex(p, colors.themeIndex(name).?); const chrome = p.chrome(); const r = p.view().rects[reader]; for ([_]bool{ false, true }) |held| for ([_]bool{ false, true }) |active| { pane.trail.held = held; p.view().active = if (active) reader else other; p.sync(); try std.testing.expectEqual(!held, pardes.Trail.reading(p, pane)); _ = frame.reset(.retain_capacity); const s = try p.render(frame.allocator()); const g: colors.Chrome.Ground = if (active) .focus else .plain; try std.testing.expectEqual(Color{ .rgb = chrome.on(g).ground }, s.at(r.x + TAG_TEXT_INSET, r.y).style.bg); const grip = if (active) chrome.grip.focus else if (held) chrome.grip.ring else chrome.grip.mode; for (0..config.GUTTER) |x| try std.testing.expectEqual(Color{ .rgb = grip }, s.at(r.x + @as(u16, @intCast(x)), r.y).style.bg); // The window rings a reading grip in the mode's mark (gui.zig // gripDecor), from the region. try std.testing.expectEqual(!held, s.region(.grip, reader).?.reading); }; } } test "while a pane's body previews a file not its own, its tag's path is dim: it names a file the body is not showing (the coordinator's ruling, UX DESIGN M7.2); its own file previewed, or the picker gone, the path in its inks again" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 140, .rows = 40 }); defer p.deinit(); p.settings.jumps = .walk; var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); for ([_][]const u8{ "alpha.zig", "beta.zig" }) |name| try tmp.dir.writeFile(std.testing.io, .{ .sub_path = name, .data = "one\ntwo\n" }); var base_buf: [std.Io.Dir.max_path_bytes]u8 = undefined; const dir = base_buf[0..try tmp.dir.realPath(std.testing.io, &base_buf)]; const a_path = try std.Io.Dir.path.join(std.testing.allocator, &.{ dir, "alpha.zig" }); defer std.testing.allocator.free(a_path); pardes.look.lookAt(p, p.view().active, a_path); p.sync(); const id = p.view().active; const pane = p.panes[id].?; p.newScratchBelow(id); const other = p.view().active; p.view().active = id; pardes.Pick.open(p, id, .list, "Refs", "", "beta.zig:2:1 two\nalpha.zig:1:1 one\n"); for (0..4) |_| { pardes.Stream.step(p); p.update(.tick); } try std.testing.expect(pardes.Pick.previewing(p, id) and std.mem.endsWith(u8, pane.file.?.path, "beta.zig")); var frame = std.heap.ArenaAllocator.init(std.testing.allocator); defer frame.deinit(); const Path = struct { // The tag at rest (its tag state hosted by another pane, as a // picker under a tag at rest is): the ink of its path's first and // last cells. fn inks(q: *Pardes, a: std.mem.Allocator, at: usize, away: usize) ![2]Color { const host = q.view().tag_state.host; q.view().tag_state.host = q.panes[away].?.serial; defer q.view().tag_state.host = host; const s = try q.render(a); const t = q.panes[at].?; const r = q.view().rects[at]; const lo = tagline.nameAt(q, t); const path = t.file.?.path; const x = r.x + TAG_TEXT_INSET + @as(u16, @intCast(panes.File.displayWidth(q.settings.tab, (try tagline.tagText(q, a, t))[0..lo]))); return .{ s.at(x, r.y).style.fg, s.at(x + @as(u16, @intCast(panes.File.displayWidth(q.settings.tab, path))) - 1, r.y).style.fg }; } }; const inks = p.chrome().on(.focus); for (try Path.inks(p, frame.allocator(), id, other)) |ink| try std.testing.expectEqual(Color{ .rgb = inks.dim }, ink); // Its own file under the cursor: the path names what the body shows. p.update(.{ .key = .{ .cp = pardes.Key.down } }); for (0..4) |_| { pardes.Stream.step(p); p.update(.tick); } try std.testing.expectEqualStrings(a_path, pane.file.?.path); var got = try Path.inks(p, frame.allocator(), id, other); try std.testing.expectEqual(Color{ .rgb = inks.text }, got[0]); try std.testing.expectEqual(Color{ .rgb = p.chrome().name[1] }, got[1]); // The picker gone: its own again. p.update(.{ .key = .{ .cp = pardes.Key.escape } }); try std.testing.expect(!pardes.Pick.previewing(p, id)); got = try Path.inks(p, frame.allocator(), id, other); try std.testing.expectEqual(Color{ .rgb = inks.text }, got[0]); }