diff options
Diffstat (limited to 'src/draw.zig')
| -rw-r--r-- | src/draw.zig | 167 |
1 files changed, 97 insertions, 70 deletions
diff --git a/src/draw.zig b/src/draw.zig index 1dc20477..fc90586e 100644 --- a/src/draw.zig +++ b/src/draw.zig @@ -159,6 +159,103 @@ fn inkOn(bg: [3]u8) [3]u8 { return if (lum > 140) .{ 0x00, 0x00, 0x00 } else .{ 0xff, 0xff, 0xff }; } +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(s: *Surface, x: u16, row: u16, w: u16, text: []const u8, style: CellStyle) Printed { + const width = panes.File.displayWidth(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(text, dropped); + if (panes.File.rawDisplayCol(text, start) < dropped) { + dropped += 1; + start = panes.File.rawAtDisplay(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 chrome = p.chromeTheme(); + 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; + var leader_buf: [16]u8 = undefined; + const text = Messages.noticeText(p, pane, i, &leader_buf); + const bg = chrome.tag_bg; + // Where an exec was sent to a REPL reads in the tag's name tint, set + // apart from what the editor says. + const fg = if (kind == .message and pane.msg_sent) chrome.tag_name_fg else chrome.tag_fg; + // 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(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(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(text); + var kept = text; + if (shown > room) { + var start = panes.File.rawAtDisplay(text, shown - room); + if (panes.File.rawDisplayCol(text, start) < shown - room) start = panes.File.rawAtDisplay(text, shown - room + 1); + kept = text[start..]; + } + const pad = room -| panes.File.displayWidth(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, 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(text[0..@min(@as(usize, @intCast(@max(0, pane.input.cur_col))), text.len)]); + const dropped = shown - panes.File.displayWidth(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| { + if (caret.x >= skip) s.cursor = .{ .x = region.rect.x + caret.x - @as(u16, @intCast(skip)), .y = region.rect.y, .bar = caret.bar }; + } + // Laid out at its full width; only shown short of the rail. + layer.viewport.w -|= region.clip; +} + pub fn render(p: *Pardes, arena: std.mem.Allocator) !*Surface { const v = p.view(); const taped = pardes.tape.enter(p, .render); @@ -733,76 +830,6 @@ fn joinPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r } } -/// 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 chrome = p.chromeTheme(); - 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; - var leader_buf: [16]u8 = undefined; - const text = Messages.noticeText(p, pane, i, &leader_buf); - const bg = chrome.tag_bg; - // Where an exec was sent to a REPL reads in the tag's name tint, set - // apart from what the editor says. - const fg = if (kind == .message and pane.msg_sent) chrome.tag_name_fg else chrome.tag_fg; - // 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 = Messages.printRight(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(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(text); - var kept = text; - if (shown > room) { - var start = panes.File.rawAtDisplay(text, shown - room); - if (panes.File.rawDisplayCol(text, start) < shown - room) start = panes.File.rawAtDisplay(text, shown - room + 1); - kept = text[start..]; - } - const pad = room -| panes.File.displayWidth(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, 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(text[0..@min(@as(usize, @intCast(@max(0, pane.input.cur_col))), text.len)]); - const dropped = shown - panes.File.displayWidth(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| { - if (caret.x >= skip) s.cursor = .{ .x = region.rect.x + caret.x - @as(u16, @intCast(skip)), .y = region.rect.y, .bar = caret.bar }; - } - // Laid out at its full width; only shown short of the rail. - layer.viewport.w -|= region.clip; -} - fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, id: usize, active: bool) !void { const th = p.theme(); const chrome = p.chromeTheme(); |
