//! Lays out styled text, such as what a tool's renderers returned. The core //! renders each call and result into blocks, so the view knows nothing about //! what a tool says. const std = @import("std"); const vaxis = @import("vaxis"); const vxfw = vaxis.vxfw; const Block = @import("../content.zig").Block; const testing = std.testing; /// The block on the given background, or null when the block is empty. A /// newline starts a new line, and each line wraps at the width it is given. pub fn layout(arena: std.mem.Allocator, block: Block, background: vaxis.Style) std.mem.Allocator.Error!?vxfw.Widget { if (block.runs.len == 0) { return null; } const segments = try arena.alloc(vaxis.Segment, block.runs.len); for (block.runs, segments) |run, *segment| { segment.* = .{ .text = block.slice(run), .style = .{ .fg = if (run.style.color) |color| .{ .index = color } else .default, .bg = background.bg, .bold = run.style.bold, .italic = run.style.italic, }, }; } return try text(arena, segments, background, true); } /// A vxfw surface holds at most 65535 cells, so long text is drawn as a column /// of chunks. A chunk ends after 32 lines or once it passes 8 KiB, so it holds /// at most about 16 KiB, which stays under the limit up to about 1500 columns. const chunk_lines = 32; const chunk_bytes = 8 * 1024; /// The segments as one column of text that fills the width it is given. pub fn text(arena: std.mem.Allocator, segments: []const vaxis.Segment, base: vaxis.Style, softwrap: bool) std.mem.Allocator.Error!vxfw.Widget { var chunks: std.ArrayList(vxfw.Widget) = .empty; var current: std.ArrayList(vaxis.Segment) = .empty; var lines: usize = 0; var bytes: usize = 0; var line_bytes: usize = 0; for (segments) |segment| { var rest = segment.text; while (rest.len > 0) { const window = rest[0..@min(rest.len, chunk_bytes - line_bytes + 1)]; const end = if (std.mem.indexOfScalar(u8, window, '\n')) |n| n + 1 else rest.len; if (line_bytes + end > chunk_bytes) { // Only a line longer than a whole chunk is cut. var cut = chunk_bytes - line_bytes; while (cut > 0 and rest[cut] & 0xc0 == 0x80) cut -= 1; if (cut == 0 and line_bytes == 0) { cut = chunk_bytes; } try current.append(arena, .{ .text = rest[0..cut], .style = segment.style }); rest = rest[cut..]; line_bytes = 0; } else { const newline = rest[end - 1] == '\n'; try current.append(arena, .{ .text = rest[0..end], .style = segment.style }); bytes += end; rest = rest[end..]; if (!newline) { line_bytes += end; continue; } line_bytes = 0; lines += 1; if (lines < chunk_lines and bytes < chunk_bytes) { continue; } } try chunks.append(arena, try richText(arena, try current.toOwnedSlice(arena), base, softwrap)); lines = 0; bytes = 0; } } if (current.items.len > 0 or chunks.items.len == 0) { try chunks.append(arena, try richText(arena, current.items, base, softwrap)); } if (chunks.items.len == 1) { return chunks.items[0]; } const column = try arena.create(Column); column.* = .{ .children = chunks.items }; return column.widget(); } fn richText(arena: std.mem.Allocator, segments: []const vaxis.Segment, base: vaxis.Style, softwrap: bool) std.mem.Allocator.Error!vxfw.Widget { const rich = try arena.create(vxfw.RichText); rich.* = .{ .text = segments, .base_style = base, .softwrap = softwrap, .width_basis = .parent }; return rich.widget(); } /// Widgets stacked top to bottom at the full width. pub const Column = struct { children: []const vxfw.Widget, pub fn widget(self: *const Column) vxfw.Widget { return .{ .userdata = @constCast(self), .drawFn = typeErasedDrawFn }; } fn typeErasedDrawFn(ptr: *anyopaque, ctx: vxfw.DrawContext) std.mem.Allocator.Error!vxfw.Surface { const self: *const Column = @ptrCast(@alignCast(ptr)); return self.draw(ctx); } /// Leaves room in a u16 height for the rows that a transcript item adds. const max_rows = std.math.maxInt(u16) - 3; /// Draws the children until the height limit, or `max_rows` without one. pub fn draw(self: *const Column, ctx: vxfw.DrawContext) std.mem.Allocator.Error!vxfw.Surface { const width = ctx.max.width orelse return vxfw.Surface.empty(self.widget()); const limit = @min(ctx.max.height orelse max_rows, max_rows); var children: std.ArrayList(vxfw.SubSurface) = .empty; var height: u16 = 0; for (self.children) |child| { if (height == limit) { break; } const surface = try child.draw(ctx.withConstraints( .{ .width = width, .height = 0 }, .{ .width = width, .height = limit - height }, )); try children.append(ctx.arena, .{ .origin = .{ .row = height, .col = 0 }, .surface = surface }); height += surface.size.height; } if (height == 0) { return vxfw.Surface.empty(self.widget()); } return .{ .size = .{ .width = width, .height = height }, .widget = self.widget(), .buffer = &.{}, .children = children.items, }; } }; fn context(arena: std.mem.Allocator) vxfw.DrawContext { return .{ .arena = arena, .min = .{}, .max = .{ .width = 20, .height = null }, .cell_size = .{}, }; } test "a block is laid out on the background the view gives it" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); vxfw.DrawContext.init(.wcwidth); const block: Block = try .init(arena, &.{.{ .text = "one\ntwo" }}); const surface = try (try layout(arena, block, .{ .bg = .{ .index = 2 } })).?.draw(context(arena)); try testing.expectEqual(@as(u16, 2), surface.size.height); try testing.expectEqual(@as(u8, 2), surface.readCell(0, 0).style.bg.index); try testing.expectEqualStrings("o", surface.readCell(0, 0).char.grapheme); try testing.expectEqualStrings("t", surface.readCell(0, 1).char.grapheme); } test "a run keeps its own styling and takes the view's background" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); vxfw.DrawContext.init(.wcwidth); const block: Block = try .init(arena, &.{ .{ .text = "bold", .style = .{ .bold = true, .italic = true, .color = 3 } }, .{ .text = " plain" }, }); const surface = try (try layout(arena, block, .{ .bg = .{ .index = 2 } })).?.draw(context(arena)); const bold = surface.readCell(0, 0).style; try testing.expect(bold.bold); try testing.expect(bold.italic); try testing.expectEqual(@as(u8, 3), bold.fg.index); try testing.expectEqual(@as(u8, 2), bold.bg.index); const plain = surface.readCell(4, 0).style; try testing.expect(!plain.bold); try testing.expectEqual(@as(u8, 2), plain.bg.index); } test "an empty block draws nothing" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); try testing.expect(try layout(arena, .{}, .{}) == null); } test "an empty column draws nothing" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); vxfw.DrawContext.init(.wcwidth); const column: Column = .{ .children = &.{} }; try testing.expectEqual(@as(u16, 0), (try column.draw(context(arena))).size.height); }