╭ Markdown.zig ─────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const ui = @import("../../ui.zig"); 3 const md = @import("../../markdown.zig"); 4 const Files = @import("Files.zig"); 5 const xit = @import("xit"); 6 const xitui = xit.xitui; 7 const wgt = xitui.widget; 8 const layout = xitui.layout; 9 const Key = xitui.input.Key; 10 const Grid = xitui.grid.Grid; 11 const Focus = xitui.focus.Focus; 12 const RichText = ui.widget.RichText; 13 14 // a rendered markdown file: one child per block, word-wrapped to the width. 15 // without `data` (a file outside any repo), repo-relative links aren't clickable. 16 pub const View = struct { 17 box: wgt.Box(ui.Widget), 18 // where focus rests off the links, covering the whole document; it's 19 // registered after the links so they win hit-testing 20 body: *Focus, 21 22 pub fn init(allocator: std.mem.Allocator, doc: md.Document, data: ?*const Files, file_path: []const u8, page_arena: *std.heap.ArenaAllocator) !View { 23 const builder: Builder = .{ 24 .allocator = allocator, 25 .data = data, 26 .dir = if (std.mem.lastIndexOfScalar(u8, file_path, '/')) |slash| file_path[0..slash] else "", 27 .page_arena = page_arena, 28 .fonts = !hasUncoveredHeading(doc.blocks), 29 }; 30 var box = try builder.blocksBox(doc.blocks, true); 31 errdefer box.deinit(allocator); 32 const body = try Focus.create(allocator, .container); 33 body.mode = .all; 34 box.getFocus().child_id = body.id; 35 return .{ .box = box, .body = body }; 36 } 37 38 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 39 self.box.deinit(allocator); 40 self.body.destroy(allocator); 41 } 42 43 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 44 self.clearGrid(); 45 try self.box.build(allocator, constraint, root_focus); 46 const grid = self.box.getGrid() orelse return; 47 try self.box.getFocus().addChild(allocator, self.body, grid.size, 0, 0); 48 } 49 50 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 51 _ = self; 52 _ = allocator; 53 _ = key; 54 _ = root_focus; 55 } 56 57 pub fn clearGrid(self: *View) void { 58 self.box.clearGrid(); 59 } 60 61 pub fn getGrid(self: View) ?Grid { 62 return self.box.getGrid(); 63 } 64 65 pub fn getFocus(self: *View) *Focus { 66 return self.box.getFocus(); 67 } 68 69 // whether `id` is the body or one of the links 70 pub fn owns(self: *View, id: usize) bool { 71 return self.box.getFocus().children.contains(id); 72 } 73 74 // focus the next or previous link in document order, scrolling it into 75 // view; from the body, start at the first link in view. stepping past 76 // either end returns to the body. 77 pub fn stepLink(self: *View, allocator: std.mem.Allocator, root_focus: *Focus, scroll: *wgt.Scroll(ui.Widget), forward: bool) !void { 78 var stops: std.ArrayList(usize) = .empty; 79 defer stops.deinit(allocator); 80 try collectStops(allocator, &self.box, self.box.getFocus(), &stops); 81 const children = self.box.getFocus().children; 82 83 const current = if (root_focus.grandchild_id) |id| std.mem.indexOfScalar(usize, stops.items, id) else null; 84 const target: ?usize = if (current) |c| 85 (if (forward) (if (c + 1 < stops.items.len) c + 1 else null) else (if (c > 0) c - 1 else null)) 86 else entry: { 87 const top: usize = @intCast(@max(scroll.y, 0)); 88 if (forward) { 89 for (stops.items, 0..) |id, i| { 90 if ((children.get(id) orelse unreachable).rect.y >= top) break :entry i; 91 } 92 break :entry null; 93 } 94 const bottom: usize = if (scroll.grid) |g| top + g.size.height - scroll.bar_h else std.math.maxInt(usize); 95 var i = stops.items.len; 96 while (i > 0) { 97 i -= 1; 98 if ((children.get(stops.items[i]) orelse unreachable).rect.y < bottom) break :entry i; 99 } 100 break :entry null; 101 }; 102 103 const id = stops.items[target orelse return root_focus.setFocus(self.body.id)]; 104 root_focus.setFocus(id); 105 const rect = (children.get(id) orelse unreachable).rect; 106 scroll.scrollToRect(.{ .x = @intCast(rect.x), .y = @intCast(rect.y), .size = rect.size }); 107 } 108 }; 109 110 // the focus ids of the laid-out links under `box`, in document order 111 fn collectStops(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), view_focus: *Focus, stops: *std.ArrayList(usize)) !void { 112 for (box.children.values()) |*child| switch (child.widget) { 113 .rich_text => |*rich_text| try addStops(allocator, rich_text, view_focus, stops), 114 .markdown_heading => |*heading| try addStops(allocator, &heading.text, view_focus, stops), 115 .markdown_gutter => |*gutter| try collectStops(allocator, &gutter.child, view_focus, stops), 116 .box => |*inner| try collectStops(allocator, inner, view_focus, stops), 117 else => {}, 118 }; 119 } 120 121 // each link's first piece is its stop 122 fn addStops(allocator: std.mem.Allocator, rich_text: *RichText, view_focus: *Focus, stops: *std.ArrayList(usize)) !void { 123 for (rich_text.link_focuses) |focuses| { 124 if (focuses.items.len > 0 and view_focus.children.contains(focuses.items[0].id)) try stops.append(allocator, focuses.items[0].id); 125 } 126 } 127 128 // turns blocks into widgets, resolving links against the viewed file. 129 const Builder = struct { 130 allocator: std.mem.Allocator, 131 data: ?*const Files, 132 // the viewed file's directory, which relative links resolve against 133 dir: []const u8, 134 page_arena: *std.heap.ArenaAllocator, 135 // whether headings may use the title fonts 136 fonts: bool, 137 138 // a vertical box of `blocks`, with a blank row between them when `gap` 139 fn blocksBox(b: Builder, blocks: []const md.Block, gap: bool) anyerror!wgt.Box(ui.Widget) { 140 var box = try wgt.Box(ui.Widget).init(b.allocator, .{ .border_style = null, .direction = .vert, .gap = @intFromBool(gap) }); 141 errdefer box.deinit(b.allocator); 142 for (blocks) |block| try b.addBlock(&box, block); 143 return box; 144 } 145 146 fn addBlock(b: Builder, box: *wgt.Box(ui.Widget), block: md.Block) !void { 147 const allocator = b.allocator; 148 switch (block) { 149 .heading => |heading| { 150 const spans = try b.runSpans(heading.inlines, .{ .bold = true }); 151 try put(allocator, box, .{ .markdown_heading = try Heading.init(allocator, b.page_arena, heading.level, spans, b.fonts) }); 152 }, 153 .paragraph => |inlines| try put(allocator, box, .{ .rich_text = try RichText.init(allocator, try b.runSpans(inlines, .{})) }), 154 .code => |lines| { 155 const text = try std.mem.join(b.page_arena.allocator(), "\n", lines); 156 try put(allocator, box, .{ .text_box = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .round_corners = true, .wrap_kind = .char }) }); 157 }, 158 .quote => |blocks| { 159 const gutter = gutter: { 160 var inner = try b.blocksBox(blocks, true); 161 errdefer inner.deinit(allocator); 162 break :gutter try Gutter.init(allocator, "│ ", true, .{ .dim = true }, inner); 163 }; 164 try put(allocator, box, .{ .markdown_gutter = gutter }); 165 }, 166 .list => |list| { 167 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 168 errdefer list_box.deinit(allocator); 169 // ordered markers are right-aligned to the widest number 170 const number_width = std.fmt.count("{d}", .{list.start + list.items.len -| 1}); 171 for (list.items, 0..) |item, i| { 172 const marker = if (item.task) |checked| 173 (if (checked) "☑ " else "☐ ") 174 else if (list.ordered) 175 try std.fmt.allocPrint(b.page_arena.allocator(), "{[n]d:>[w]}. ", .{ .n = list.start + i, .w = number_width }) 176 else 177 "• "; 178 const gutter = gutter: { 179 var inner = try b.blocksBox(item.blocks, false); 180 errdefer inner.deinit(allocator); 181 break :gutter try Gutter.init(allocator, marker, false, .{}, inner); 182 }; 183 try put(allocator, &list_box, .{ .markdown_gutter = gutter }); 184 } 185 try put(allocator, box, .{ .box = list_box }); 186 }, 187 // wider than any pane, and clipped to the width 188 .rule => try put(allocator, box, .{ .text_box = try wgt.TextBox.init(allocator, "─" ** 512, .{ .border_style = null, .wrap_kind = .none, .style = .{ .dim = true } }) }), 189 .raw => |lines| { 190 const text = try std.mem.join(b.page_arena.allocator(), "\n", lines); 191 try put(allocator, box, .{ .text_box = try wgt.TextBox.init(allocator, text, .{ .border_style = null, .wrap_kind = .none }) }); 192 }, 193 } 194 } 195 196 // styled spans for `inlines` over `base`, with each link resolved 197 fn runSpans(b: Builder, inlines: []const md.Inline, base: Grid.Style) ![]const RichText.Span { 198 const out = try b.page_arena.allocator().alloc(RichText.Span, inlines.len); 199 for (inlines, out) |run, *span| { 200 const link = if (run.link) |dest| try b.resolveLink(dest) else ""; 201 var style = base; 202 style.bold = style.bold or run.style.bold; 203 style.italic = run.style.italic; 204 style.strikethrough = run.style.strike; 205 if (run.style.code) style.fg = .{ .ansi = .yellow }; 206 if (link.len > 0) { 207 style.fg = .{ .ansi = .cyan }; 208 style.underline = true; 209 } 210 span.* = .{ .text = run.text, .style = style, .link = link }; 211 } 212 return out; 213 } 214 215 // the focus kind a link follows, or "" when it isn't clickable: web links 216 // are raw links, and a repo path is a files route relative to this file 217 fn resolveLink(b: Builder, dest: []const u8) ![]const u8 { 218 const aa = b.page_arena.allocator(); 219 for ([_][]const u8{ "http:", "https:", "mailto:" }) |scheme| { 220 if (std.ascii.startsWithIgnoreCase(dest, scheme)) return std.fmt.allocPrint(aa, "{s}{s}", .{ ui.raw_link_prefix, dest }); 221 } 222 if (hasScheme(dest) or std.mem.startsWith(u8, dest, "//")) return ""; 223 const data = b.data orelse return ""; 224 const end = std.mem.indexOfAny(u8, dest, "?#") orelse dest.len; 225 if (end == 0) return ""; 226 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, dest[0..end])); 227 228 var segments: std.ArrayList([]const u8) = .empty; 229 if (decoded[0] != '/') { 230 var base = std.mem.tokenizeScalar(u8, b.dir, '/'); 231 while (base.next()) |segment| try segments.append(aa, segment); 232 } 233 var parts = std.mem.tokenizeScalar(u8, decoded, '/'); 234 while (parts.next()) |part| { 235 if (std.mem.eql(u8, part, ".")) continue; 236 if (std.mem.eql(u8, part, "..")) { 237 // climbing above the root isn't a repo path 238 if (segments.pop() == null) return ""; 239 continue; 240 } 241 try segments.append(aa, part); 242 } 243 const path = try std.mem.join(aa, "/", segments.items); 244 const route = data.filesRoute(path, 0) orelse return ""; 245 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(b.page_arena)}); 246 } 247 }; 248 249 // whether any heading the title fonts would draw, however nested, has a char 250 // they lack, in which case none use them so they all look alike 251 fn hasUncoveredHeading(blocks: []const md.Block) bool { 252 for (blocks) |block| switch (block) { 253 .heading => |heading| if (heading.level <= 2) for (heading.inlines) |run| { 254 if (!ui.Title.covers(run.text) or !ui.SubTitle.covers(run.text)) return true; 255 }, 256 .quote => |inner| if (hasUncoveredHeading(inner)) return true, 257 .list => |list| for (list.items) |item| { 258 if (hasUncoveredHeading(item.blocks)) return true; 259 }, 260 else => {}, 261 }; 262 return false; 263 } 264 265 // whether `dest` starts with a url scheme 266 fn hasScheme(dest: []const u8) bool { 267 const colon = std.mem.indexOfScalar(u8, dest, ':') orelse return false; 268 if (colon == 0 or !std.ascii.isAlphabetic(dest[0])) return false; 269 for (dest[0..colon]) |c| { 270 if (!std.ascii.isAlphanumeric(c) and c != '+' and c != '.' and c != '-') return false; 271 } 272 return true; 273 } 274 275 fn put(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), widget: ui.Widget) !void { 276 var owned = widget; 277 errdefer owned.deinit(allocator); 278 try box.children.put(allocator, owned.getFocus().id, .{ .widget = owned, .rect = null, .min_size = null }); 279 } 280 281 // a heading in its level's title font, word-wrapped to the width, or bold 282 // text when there's no font for it. 283 pub const Heading = struct { 284 focus: *Focus, 285 text: RichText, 286 // the font this heading draws in, if any, and the text it draws 287 font: ?enum { title, sub_title } = null, 288 plain: []const u8 = "", 289 font_box: wgt.TextBox, 290 291 fn init(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, level: u3, spans: []const RichText.Span, fonts: bool) !Heading { 292 const focus = try Focus.create(allocator, .container); 293 errdefer focus.destroy(allocator); 294 295 // the bold fallback leads with the level's #s, so levels stay apart 296 const marked = try page_arena.allocator().alloc(RichText.Span, spans.len + 1); 297 marked[0] = .{ .text = try std.fmt.allocPrint(page_arena.allocator(), "{s} ", .{"######"[0..level]}), .style = .{ .bold = true } }; 298 @memcpy(marked[1..], spans); 299 var text = try RichText.init(allocator, marked); 300 errdefer text.deinit(allocator); 301 302 var font_box = try wgt.TextBox.init(allocator, "", .{ .border_style = null, .wrap_kind = .none }); 303 errdefer font_box.deinit(allocator); 304 var self: Heading = .{ .focus = focus, .text = text, .font_box = font_box }; 305 306 var plain: std.ArrayList(u8) = .empty; 307 var has_links = false; 308 for (spans) |span| { 309 try plain.appendSlice(page_arena.allocator(), span.text); 310 has_links = has_links or span.link.len > 0; 311 } 312 self.plain = std.mem.trim(u8, plain.items, " "); 313 if (fonts and level <= 2 and !has_links and self.plain.len > 0) self.font = if (level == 1) .title else .sub_title; 314 return self; 315 } 316 317 pub fn deinit(self: *Heading, allocator: std.mem.Allocator) void { 318 self.focus.destroy(allocator); 319 self.text.deinit(allocator); 320 self.font_box.deinit(allocator); 321 } 322 323 pub fn build(self: *Heading, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 324 self.clearGrid(); 325 self.focus.clear(); 326 if (try self.wrapFont(allocator, constraint.max_size.width)) { 327 try self.buildShown(allocator, constraint, root_focus, &self.font_box); 328 } else { 329 try self.buildShown(allocator, constraint, root_focus, &self.text); 330 } 331 } 332 333 // fill the font box with the heading wrapped at `max_width`, or return 334 // false when there's no font 335 fn wrapFont(self: *Heading, allocator: std.mem.Allocator, max_width: ?usize) !bool { 336 const font = self.font orelse return false; 337 // a glyph and the gap after it, so n glyphs take n * char_width - 1 columns 338 const char_width: usize = if (font == .title) 4 else 3; 339 const max_chars = if (max_width) |w| (w + 1) / char_width else null; 340 341 var arena = std.heap.ArenaAllocator.init(allocator); 342 defer arena.deinit(); 343 const aa = arena.allocator(); 344 // the fonts only cover ascii, so each byte is one char 345 const chars = try aa.alloc(u21, self.plain.len); 346 for (self.plain, chars) |c, *char| char.* = c; 347 var lines: std.ArrayList(wgt.Line) = .empty; 348 try wgt.wrapLines(aa, &lines, chars, .word, max_chars); 349 350 // a blank row between lines keeps the glyphs from touching 351 var out: std.ArrayList(u8) = .empty; 352 for (lines.items, 0..) |line, i| { 353 if (i > 0) try out.appendSlice(aa, "\n\n"); 354 const text = std.mem.trimEnd(u8, self.plain[line.start..line.end], " "); 355 try out.appendSlice(aa, switch (font) { 356 .title => (try ui.Title.init(&arena, text, .solid)).content, 357 .sub_title => (try ui.SubTitle.init(&arena, text)).content, 358 }); 359 } 360 try self.font_box.setContent(allocator, out.items); 361 return true; 362 } 363 364 fn buildShown(self: *Heading, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus, widget: anytype) !void { 365 try widget.build(allocator, constraint, root_focus); 366 const grid = widget.getGrid() orelse return; 367 try self.focus.addChild(allocator, widget.getFocus(), grid.size, 0, 0); 368 } 369 370 pub fn input(self: *Heading, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 371 _ = self; 372 _ = allocator; 373 _ = key; 374 _ = root_focus; 375 } 376 377 pub fn clearGrid(self: *Heading) void { 378 self.text.clearGrid(); 379 self.font_box.clearGrid(); 380 } 381 382 pub fn getGrid(self: Heading) ?Grid { 383 // only the one last built has a grid 384 return self.font_box.getGrid() orelse self.text.getGrid(); 385 } 386 387 pub fn getFocus(self: *Heading) *Focus { 388 return self.focus; 389 } 390 }; 391 392 // a marker column beside a child, so wrapped lines hang under the child's 393 // first line: a list item's bullet, or a quote's bar on every row. 394 pub const Gutter = struct { 395 focus: *Focus, 396 grid: ?Grid, 397 child: wgt.Box(ui.Widget), 398 marker: []const u8, 399 // draw the marker on every row rather than only the first 400 repeat: bool, 401 style: Grid.Style, 402 403 // takes ownership of `child` on success 404 fn init(allocator: std.mem.Allocator, marker: []const u8, repeat: bool, style: Grid.Style, child: wgt.Box(ui.Widget)) !Gutter { 405 return .{ .focus = try Focus.create(allocator, .container), .grid = null, .child = child, .marker = marker, .repeat = repeat, .style = style }; 406 } 407 408 pub fn deinit(self: *Gutter, allocator: std.mem.Allocator) void { 409 self.focus.destroy(allocator); 410 self.clearGrid(); 411 self.child.deinit(allocator); 412 } 413 414 pub fn build(self: *Gutter, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 415 self.clearGrid(); 416 self.focus.clear(); 417 const marker_width = try xitui.width.displayWidth(self.marker); 418 if (constraint.max_size.width) |w| if (w <= marker_width) return; 419 if (constraint.max_size.height == 0) return; 420 421 try self.child.build(allocator, .{ 422 .min_size = .{ .width = null, .height = null }, 423 .max_size = .{ .width = if (constraint.max_size.width) |w| w - marker_width else null, .height = constraint.max_size.height }, 424 }, root_focus); 425 const child_grid = self.child.getGrid(); 426 const child_size: layout.Size = if (child_grid) |g| g.size else .{ .width = 0, .height = 0 }; 427 428 var grid = try Grid.init(allocator, .{ .width = marker_width + child_size.width, .height = @max(1, child_size.height) }); 429 errdefer grid.deinit(); 430 for (0..if (self.repeat) grid.size.height else 1) |y| { 431 var x: usize = 0; 432 var utf8 = (try std.unicode.Utf8View.init(self.marker)).iterator(); 433 while (utf8.nextCodepoint()) |rune| { 434 (try grid.cell(x, y)).style = self.style; 435 try grid.setRune(x, y, rune); 436 x += xitui.width.cellWidth(rune); 437 } 438 } 439 if (child_grid) |g| { 440 try grid.drawGrid(g, marker_width, 0); 441 try self.focus.addChild(allocator, self.child.getFocus(), g.size, marker_width, 0); 442 } 443 self.grid = grid; 444 } 445 446 pub fn input(self: *Gutter, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 447 _ = self; 448 _ = allocator; 449 _ = key; 450 _ = root_focus; 451 } 452 453 pub fn clearGrid(self: *Gutter) void { 454 if (self.grid) |*grid| { 455 grid.deinit(); 456 self.grid = null; 457 } 458 self.child.clearGrid(); 459 } 460 461 pub fn getGrid(self: Gutter) ?Grid { 462 return self.grid; 463 } 464 465 pub fn getFocus(self: *Gutter) *Focus { 466 return self.focus; 467 } 468 }; 469