╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
536 // one wouldn't fit. 537 self.rects.clearRetainingCapacity(); 538 var x: usize = 0; 539 var y: usize = 0; 540 var row_height: usize = 0; 541 var total_width: usize = 0; 542 for (self.text_boxes.items) |*tb| { 543 try tb.build(allocator, .{ 544 .min_size = .{ .width = null, .height = null }, 545 .max_size = .{ .width = max_width, .height = null }, 546 }, root_focus); 547 const tb_grid_maybe = tb.getGrid(); 548 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 549 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 550 if (x > 0 and x + w > max_width) { 551 x = 0; 552 y += row_height; 553 row_height = 0; 554 } 555 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 556 if (h > row_height) row_height = h; 557 x += w; 558 if (x > total_width) total_width = x; 559 } 560 561 const total_height = y + row_height; 562 if (total_width == 0 or total_height == 0) return; 563 564 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 565 errdefer grid.deinit(); 566 567 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 568 const tb_grid = tb.getGrid() orelse continue; 569 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 570 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 571 } 572 573 self.grid = grid; 574 } 575 576 pub fn input(self: *WordFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 577 _ = self; 578 _ = allocator; 579 _ = key; 580 _ = root_focus; 581 } 582 583 pub fn clearGrid(self: *WordFlow) void { 584 if (self.grid) |*grid| { 585 grid.deinit(); 586 self.grid = null; 587 } 588 for (self.text_boxes.items) |*tb| tb.clearGrid(); 589 } 590 591 pub fn getGrid(self: WordFlow) ?Grid { 592 return self.grid; 593 } 594 595 pub fn getFocus(self: *WordFlow) *Focus { 596 return self.focus; 597 } 598 599 pub fn indexOfFocusId(self: WordFlow, focus_id: usize) ?usize { 600 for (self.text_boxes.items, 0..) |tb, i| { 601 if (tb.focus.id == focus_id) return i; 602 } 603 return null; 604 } 605 606 // the first item of the row below (when `downward`) or above, or null at 607 // the flow's edge. items are laid out in order, so y is nondecreasing. 608 pub fn rowStep(self: WordFlow, index: usize, downward: bool) ?usize { 609 const rects = self.rects.items; 610 if (index >= rects.len) return null; 611 const cur_y = rects[index].y; 612 if (downward) { 613 for (rects[index + 1 ..], index + 1..) |r, i| { 614 if (r.y > cur_y) return i; 615 } 616 return null; 617 } 618 // walk the earlier rows; the last one visited is the row above. 619 var prev_first: ?usize = null; 620 var i: usize = 0; 621 while (i < rects.len and rects[i].y < cur_y) { 622 prev_first = i; 623 const y = rects[i].y; 624 while (i < rects.len and rects[i].y == y) i += 1; 625 } 626 return prev_first; 627 } 628 }; 629 630 // a word-wrapped paragraph of styled runs. each line a link touches is a 631 // focusable piece, and a focused link draws all its pieces inverted. 632 pub const RichText = struct { 633 focus: *Focus, 634 grid: ?Grid, 635 // backs the decoded content and the link targets 636 arena: std.heap.ArenaAllocator, 637 runes: []const u21, 638 // each rune's span 639 span_of: []const usize, 640 styles: []const Grid.Style, 641 // each span's link index, null outside a link 642 span_links: []const ?usize, 643 links: []const []const u8, 644 // each link's piece focuses in line order, kept across builds so a link 645 // keeps its ids when the text rewraps 646 link_focuses: []std.ArrayList(*Focus), 647 lines: std.ArrayList(wgt.Line), 648 // scratch for each build's link pieces 649 pieces: std.ArrayList(Piece), 650 651 pub const Span = struct { 652 text: []const u8, 653 style: Grid.Style = .{}, 654 // the focus kind a click or enter follows; empty for plain text 655 link: []const u8 = "", 656 }; 657 658 const Piece = struct { 659 link: usize, 660 rect: layout.URect, 661 }; 662 663 pub fn init(allocator: std.mem.Allocator, spans: []const Span) !RichText { 664 var arena = std.heap.ArenaAllocator.init(allocator); 665 errdefer arena.deinit(); 666 const aa = arena.allocator(); 667 668 var runes: std.ArrayList(u21) = .empty; 669 var span_of: std.ArrayList(usize) = .empty; 670 const styles = try aa.alloc(Grid.Style, spans.len); 671 const span_links = try aa.alloc(?usize, spans.len); 672 var links: std.ArrayList([]const u8) = .empty; 673 for (spans, 0..) |span, i| { 674 styles[i] = span.style; 675 span_links[i] = null; 676 if (span.link.len > 0) { 677 // adjacent spans with the same target are one link 678 const same = i > 0 and spans[i - 1].link.len > 0 and std.mem.eql(u8, spans[i - 1].link, span.link); 679 if (!same) try links.append(aa, try aa.dupe(u8, span.link)); 680 span_links[i] = links.items.len - 1; 681 } 682 var utf8 = (try std.unicode.Utf8View.init(span.text)).iterator(); 683 while (utf8.nextCodepoint()) |codepoint| { 684 try runes.append(aa, codepoint); 685 try span_of.append(aa, i); 686 } 687 } 688 689 const link_focuses = try aa.alloc(std.ArrayList(*Focus), links.items.len); 690 @memset(link_focuses, .empty); 691 692 return .{ 693 .focus = try Focus.create(allocator, .container), 694 .grid = null, 695 .arena = arena, 696 .runes = runes.items, 697 .span_of = span_of.items, 698 .styles = styles, 699 .span_links = span_links, 700 .links = links.items, 701 .link_focuses = link_focuses, 702 .lines = .empty, 703 .pieces = .empty, 704 }; 705 } 706 707 pub fn deinit(self: *RichText, allocator: std.mem.Allocator) void { 708 self.focus.destroy(allocator); 709 self.clearGrid(); 710 for (self.link_focuses) |*focuses| { 711 for (focuses.items) |focus| focus.destroy(allocator); 712 focuses.deinit(allocator); 713 } 714 self.pieces.deinit(allocator); 715 self.lines.deinit(allocator); 716 self.arena.deinit(); 717 } 718 719 // the url a terminal can open for a link: raw links to absolute urls 720 fn terminalLink(link: []const u8) ?[]const u8 { 721 if (!std.mem.startsWith(u8, link, ui.raw_link_prefix)) return null; 722 const url = link[ui.raw_link_prefix.len..]; 723 return if (std.mem.startsWith(u8, url, "/")) null else url; 724 } 725 726 // the link one of whose pieces has focus id `id` 727 fn linkOf(self: *const RichText, id: ?usize) ?usize { 728 for (self.link_focuses, 0..) |focuses, l| { 729 for (focuses.items) |focus| if (focus.id == id) return l; 730 } 731 return null; 732 } 733 734 pub fn build(self: *RichText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 735 self.clearGrid(); 736 self.focus.clear(); 737 const max_width = constraint.max_size.width; 738 if (max_width == 0 or constraint.max_size.height == 0) return; 739 const focused_link = self.linkOf(root_focus.grandchild_id); 740 741 try wgt.wrapLines(allocator, &self.lines, self.runes, .word, max_width); 742 // trailing spaces are dropped so a wrapped link doesn't highlight them 743 for (self.lines.items) |*line| { 744 while (line.end > line.start and self.runes[line.end - 1] == ' ') { 745 line.end -= 1; 746 line.width -= 1; 747 } 748 } 749 const visible = @min(self.lines.items.len, constraint.max_size.height orelse self.lines.items.len); 750 var width: usize = 1; 751 for (self.lines.items[0..visible]) |line| width = @max(width, line.width); 752 if (max_width) |w| width = @min(width, w); 753 754 var grid = try Grid.init(allocator, .{ .width = width, .height = visible }); 755 errdefer grid.deinit(); 756 self.pieces.clearRetainingCapacity(); 757 for (self.lines.items[0..visible], 0..) |line, y| { 758 var x: usize = 0; 759 for (self.runes[line.start..line.end], line.start..) |rune, i| { 760 const rune_width = xitui.width.cellWidth(rune); 761 if (x + rune_width > width) break; 762 const link = self.span_links[self.span_of[i]]; 763 const cell = try grid.cell(x, y); 764 cell.style = self.styles[self.span_of[i]]; 765 if (link) |l| { 766 cell.link = terminalLink(self.links[l]); 767 if (l == focused_link) cell.style.inverted = true; 768 // extend this line's piece of the link, or start one 769 const items = self.pieces.items; 770 if (items.len > 0 and items[items.len - 1].link == l and items[items.len - 1].rect.y == y) { 771 items[items.len - 1].rect.size.width += rune_width; 772 } else { 773 try self.pieces.append(allocator, .{ .link = l, .rect = .{ .x = x, .y = y, .size = .{ .width = rune_width, .height = 1 } } }); 774 } 775 } 776 try grid.setRune(x, y, rune); 777 x += rune_width; 778 } 779 } 780 781 // a link's pieces are consecutive, since its runes are 782 var piece_index: usize = 0; 783 for (self.pieces.items, 0..) |piece, i| { 784 piece_index = if (i > 0 and self.pieces.items[i - 1].link == piece.link) piece_index + 1 else 0; 785 const focuses = &self.link_focuses[piece.link]; 786 if (piece_index == focuses.items.len) { 787 const focus = try Focus.create(allocator, .{ .custom = self.links[piece.link] }); 788 errdefer focus.destroy(allocator); 789 focus.mode = .all; 790 try focuses.append(allocator, focus); 791 } 792 try self.focus.addChild(allocator, focuses.items[piece_index], piece.rect.size, piece.rect.x, piece.rect.y); 793 } 794 795 // focus rests on a link's first piece, which stays while the link shows 796 if (focused_link) |l| if (self.link_focuses[l].items.len > 0) { 797 const first = self.link_focuses[l].items[0].id; 798 if (self.focus.children.contains(first)) { 799 root_focus.grandchild_id = first; 800 self.focus.child_id = first; 801 } 802 }; 803 804 self.grid = grid; 805 } 806 807 pub fn input(self: *RichText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 808 _ = self; 809 _ = allocator; 810 _ = key; 811 _ = root_focus; 812 } 813 814 pub fn clearGrid(self: *RichText) void { 815 if (self.grid) |*grid| { 816 grid.deinit(); 817 self.grid = null; 818 } 819 } 820 821 pub fn getGrid(self: RichText) ?Grid { 822 return self.grid; 823 } 824 825 pub fn getFocus(self: *RichText) *Focus { 826 return self.focus; 827 } 828 }; 829 830 // an invisible widget that fills the horizontal space granted by its parent. 831 // used inside Box(horiz) with a min_size so the box reserves space for the 832 // children that follow, pushing them to the right. 833 pub const Spacer = struct { 834 focus: *Focus, 835 grid: ?Grid, 836 837 pub fn init(allocator: std.mem.Allocator) !Spacer { 838 return .{ 839 .focus = try Focus.create(allocator, .container), 840 .grid = null, 841 }; 842 } 843 844 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 845 self.focus.destroy(allocator); 846 if (self.grid) |*grid| { 847 grid.deinit(); 848 self.grid = null; 849 } 850 } 851 852 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 853 _ = root_focus; 854 self.clearGrid(); 855 const width = constraint.max_size.width orelse return; 856 if (width == 0) return; 857 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 858 } 859 860 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 861 _ = self; 862 _ = allocator; 863 _ = key; 864 _ = root_focus; 865 } 866 867 pub fn clearGrid(self: *Spacer) void { 868 if (self.grid) |*grid| { 869 grid.deinit(); 870 self.grid = null; 871 } 872 } 873 874 pub fn getGrid(self: Spacer) ?Grid { 875 return self.grid; 876 } 877 878 pub fn getFocus(self: *Spacer) *Focus { 879 return self.focus; 880 } 881 }; 882 883 // a sub-header's search box: a labeled text input whose enter the owning view 884 // turns into a navigation. shared by the files, commits and thread tabs. 885 pub const SearchBox = struct { 886 text_input: wgt.TextInput, 887 session: *ui.Session, 888 889 // the room the box asks its row for 890 pub const min_size: layout.MaybeSize = .{ .width = 20, .height = 3 }; 891 892 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, label: []const u8, name: []const u8, text_value: ?[]const u8) !SearchBox { 893 var text_input = try wgt.TextInput.init(allocator, .{ .label = label, .name = name, .round_corners = true, .visible_width = 18, .render_content = session.is_terminal }); 894 errdefer text_input.deinit(allocator); 895 text_input.getFocus().mode = .all; 896 if (text_value) |value| try text_input.setContent(allocator, value); 897 return .{ .text_input = text_input, .session = session }; 898 } 899 900 pub fn deinit(self: *SearchBox, allocator: std.mem.Allocator) void { 901 self.text_input.deinit(allocator); 902 } 903 904 pub fn build(self: *SearchBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 905 self.text_input.options.bottom_label = if (root_focus.grandchild_id == self.getFocus().id) " press enter " else ""; 906 try self.text_input.build(allocator, constraint, root_focus); 907 if (!self.session.is_terminal) { 908 try self.session.text_inputs.put(self.session.arena.allocator(), self.getFocus().id, &self.text_input); 909 } 910 } 911 912 pub fn input(self: *SearchBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 913 try self.text_input.input(allocator, key, root_focus); 914 } 915 916 // the typed text; the caller owns it 917 pub fn text(self: *SearchBox, allocator: std.mem.Allocator) ![]u8 { 918 return self.text_input.text(allocator); 919 } 920 921 // whether the cursor sits past the last character, where right-arrow leaves 922 // the box rather than moving within it 923 pub fn atEnd(self: *SearchBox) bool { 924 return self.text_input.cursor == self.text_input.content.items.len; 925 } 926 927 pub fn clearGrid(self: *SearchBox) void { 928 self.text_input.clearGrid(); 929 } 930 931 pub fn getGrid(self: SearchBox) ?Grid { 932 return self.text_input.getGrid(); 933 } 934 935 pub fn getFocus(self: *SearchBox) *Focus { 936 return self.text_input.getFocus(); 937 } 938 }; 939 940 // a tab's sub-header: an optional search box on the left, then a spacer, then 941 // the clone url on the right. the owning view turns a submitted query into a 942 // route, so route building stays with the tab that knows its own url. 943 pub const SearchHeader = struct { 944 box: wgt.Box(Widget), 945 session: *ui.Session, 946 947 pub fn init( 948 allocator: std.mem.Allocator, 949 session: *ui.Session, 950 location: ui.RoutablePage.RepoLocation, 951 label: []const u8, 952 name: []const u8, 953 text: ?[]const u8, 954 show_input: bool, 955 ) !SearchHeader { 956 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 957 errdefer box.deinit(allocator); 958 959 // the search box only appears where an index can serve it. 960 if (show_input) { 961 var search_box = try SearchBox.init(allocator, session, label, name, text); 962 errdefer search_box.deinit(allocator); 963 box.getFocus().child_id = search_box.getFocus().id; 964 try box.children.put(allocator, search_box.getFocus().id, .{ .widget = .{ .search_box = search_box }, .rect = null, .min_size = SearchBox.min_size }); 965 } 966 967 // the spacer pushes the clone url to the right edge. 968 { 969 var spacer = try Spacer.init(allocator); 970 errdefer spacer.deinit(allocator); 971 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 972 } 973 974 if (session.data.host_kind == .server) { 975 if (try CopyableText.initClone(allocator, session, location)) |value| { 976 var clone_url = value; 977 errdefer clone_url.deinit(allocator); 978 const min_width = clone_url.minWidth(); 979 if (box.getFocus().child_id == null) box.getFocus().child_id = clone_url.getFocus().id; 980 try box.children.put(allocator, clone_url.getFocus().id, .{ .widget = .{ .copyable_text = clone_url }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 981 } 982 } 983 984 return .{ .box = box, .session = session }; 985 } 986 987 pub fn deinit(self: *SearchHeader, allocator: std.mem.Allocator) void { 988 self.box.deinit(allocator); 989 } 990 991 pub fn build(self: *SearchHeader, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 992 self.clearGrid(); 993 try self.box.build(allocator, constraint, root_focus); 994 } 995 996 pub fn input(self: *SearchHeader, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 997 const current = self.box.getFocus().child_id orelse return; 998 if (self.searchBox()) |search_box| { 999 if (current == search_box.getFocus().id) { 1000 // tab, and right-arrow past the last character, step to the 1001 // clone url beside the box 1002 if (key == .tab or (key == .arrow_right and search_box.atEnd())) { 1003 if (self.cloneUrl()) |clone_url| { 1004 root_focus.setFocus(clone_url.getFocus().id); 1005 return; 1006 } 1007 } 1008 return search_box.input(allocator, key, root_focus); 1009 } 1010 } 1011 const clone_url = self.cloneUrl() orelse return; 1012 if (key == .back_tab or (key == .arrow_left and clone_url.selected == 0)) { 1013 if (self.searchBox()) |search_box| { 1014 root_focus.setFocus(search_box.getFocus().id); 1015 return; 1016 } 1017 } 1018 try clone_url.input(allocator, key, root_focus); 1019 } 1020 1021 // the typed query when enter should submit it: the box exists and holds 1022 // focus. the caller owns the returned text. 1023 pub fn submittedText(self: *SearchHeader, allocator: std.mem.Allocator) !?[]u8 { 1024 const search_box = self.searchBox() orelse return null; 1025 if (self.box.getFocus().child_id != search_box.getFocus().id) return null; 1026 return try search_box.text(allocator); 1027 } 1028 1029 // enter the header, preferring the search box. 1030 pub fn focusHeader(self: *SearchHeader, root_focus: *Focus) bool { 1031 if (self.searchBox()) |search_box| { 1032 root_focus.setFocus(search_box.getFocus().id); 1033 return true; 1034 } 1035 const clone_url = self.cloneUrl() orelse return false; 1036 root_focus.setFocus(clone_url.getFocus().id); 1037 return true; 1038 } 1039 1040 pub fn hasFocusable(self: *SearchHeader) bool { 1041 return self.cloneUrl() != null or self.searchBox() != null; 1042 } 1043 1044 fn cloneUrl(self: *SearchHeader) ?*CopyableText { 1045 for (self.box.children.values()) |*child| switch (child.widget) { 1046 .copyable_text => |*copyable_text| return copyable_text, 1047 else => {}, 1048 }; 1049 return null; 1050 } 1051 1052 fn searchBox(self: *SearchHeader) ?*SearchBox { 1053 for (self.box.children.values()) |*child| switch (child.widget) { 1054 .search_box => |*search_box| return search_box, 1055 else => {}, 1056 }; 1057 return null; 1058 } 1059 1060 pub fn clearGrid(self: *SearchHeader) void { 1061 self.box.clearGrid(); 1062 } 1063 1064 pub fn getGrid(self: SearchHeader) ?Grid { 1065 return self.box.getGrid(); 1066 } 1067 1068 pub fn getFocus(self: *SearchHeader) *Focus { 1069 return self.box.getFocus(); 1070 } 1071 }; 1072 1073 // a one-row, non-focusable status bar showing the current page's url 1074 pub const Footer = struct { 1075 focus: *Focus, 1076 grid: ?Grid, 1077 session: *ui.Session, 1078 arena: std.heap.ArenaAllocator, 1079 url: []const u8 = "", 1080 url_width: usize = 0, 1081 1082 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !Footer { 1083 return .{ 1084 .focus = try Focus.create(allocator, .container), 1085 .grid = null, 1086 .session = session, 1087 .arena = std.heap.ArenaAllocator.init(allocator), 1088 }; 1089 } 1090 1091 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1092 self.focus.destroy(allocator); 1093 if (self.grid) |*grid| grid.deinit(); 1094 self.arena.deinit(); 1095 } 1096 1097 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1098 _ = root_focus; 1099 self.clearGrid(); 1100 const width = constraint.max_size.width orelse return; 1101 if (width == 0) return; 1102 1103 _ = self.arena.reset(.retain_capacity); 1104 self.url = ""; 1105 self.url_width = 0; 1106 const aa = self.arena.allocator(); 1107 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 1108 const text = ui.terminalWebUrl(aa, self.session, path) catch return; 1109 self.url = text; 1110 1111 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1112 errdefer grid.deinit(); 1113 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1114 var i: usize = 0; 1115 while (utf8.nextCodepoint()) |ch| { 1116 const rune_width = xitui.width.cellWidth(ch); 1117 if (i + rune_width > width) break; 1118 try grid.setRune(i, 0, ch); 1119 i += rune_width; 1120 } 1121 self.url_width = i; 1122 self.grid = grid; 1123 } 1124 1125 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1126 _ = allocator; 1127 switch (key) { 1128 .mouse => |mouse| { 1129 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 1130 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 1131 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 1132 }, 1133 else => {}, 1134 } 1135 } 1136 1137 pub fn clearGrid(self: *Footer) void { 1138 if (self.grid) |*grid| { 1139 grid.deinit(); 1140 self.grid = null; 1141 } 1142 } 1143 1144 pub fn getGrid(self: Footer) ?Grid { 1145 return self.grid; 1146 } 1147 1148 pub fn getFocus(self: *Footer) *Focus { 1149 return self.focus; 1150 } 1151 }; 1152 1153 // a single-child wrapper that builds the child at its natural size and 1154 // positions its grid in the middle of the area granted by the parent 1155 // a bordered label, indented past the content it precedes 1156 pub const SectionLabel = struct { 1157 box: wgt.Box(Widget), 1158 1159 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 1160 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1161 errdefer box.deinit(allocator); 1162 1163 { 1164 var gap = try wgt.Text.init(allocator, " "); 1165 errdefer gap.deinit(allocator); 1166 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1167 } 1168 1169 { 1170 var tb = try wgt.TextBox.init(allocator, content, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 1171 errdefer tb.deinit(allocator); 1172 tb.getFocus().mode = .all; 1173 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1174 } 1175 1176 box.getFocus().child_id = box.children.keys()[1]; 1177 return .{ .box = box }; 1178 } 1179 1180 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 1181 self.box.deinit(allocator); 1182 } 1183 1184 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1185 try self.box.build(allocator, constraint, root_focus); 1186 } 1187 1188 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1189 _ = self; 1190 _ = allocator; 1191 _ = key; 1192 _ = root_focus; 1193 } 1194 1195 pub fn clearGrid(self: *SectionLabel) void { 1196 self.box.clearGrid(); 1197 } 1198 1199 pub fn getGrid(self: SectionLabel) ?Grid { 1200 return self.box.getGrid(); 1201 } 1202 1203 pub fn getFocus(self: *SectionLabel) *Focus { 1204 return self.box.getFocus(); 1205 } 1206 }; 1207 1208 // a submit button, indented past its form's content. the web form POSTs 1209 // the button's form; terminal hosts detect presses through `buttonId`. 1210 pub const SubmitButton = struct { 1211 box: wgt.Box(Widget), 1212 1213 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 1214 return initLabeled(allocator, "submit"); 1215 } 1216 1217 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 1218 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1219 errdefer box.deinit(allocator); 1220 1221 { 1222 var gap = try wgt.Text.init(allocator, " "); 1223 errdefer gap.deinit(allocator); 1224 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1225 } 1226 1227 { 1228 var button = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 1229 errdefer button.deinit(allocator); 1230 button.getFocus().mode = .all; 1231 // the renderer distinguishes plain clickables from buttons that 1232 // should POST to a server route by this kind. 1233 button.getFocus().kind = .{ .custom = "submit" }; 1234 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1235 } 1236 1237 box.getFocus().child_id = box.children.keys()[1]; 1238 return .{ .box = box }; 1239 } 1240 1241 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 1242 self.box.deinit(allocator); 1243 } 1244 1245 pub fn setLabel(self: *SubmitButton, allocator: std.mem.Allocator, label: []const u8) !void { 1246 const button = &self.box.children.values()[1]; 1247 try button.widget.text_box.setContent(allocator, label); 1248 button.min_size = .{ .width = label.len + 2, .height = null }; 1249 } 1250 1251 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1252 try self.box.build(allocator, constraint, root_focus); 1253 } 1254 1255 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1256 _ = self; 1257 _ = allocator; 1258 _ = key; 1259 _ = root_focus; 1260 } 1261 1262 pub fn clearGrid(self: *SubmitButton) void { 1263 self.box.clearGrid(); 1264 } 1265 1266 pub fn getGrid(self: SubmitButton) ?Grid { 1267 return self.box.getGrid(); 1268 } 1269 1270 pub fn getFocus(self: *SubmitButton) *Focus { 1271 return self.box.getFocus(); 1272 } 1273 1274 // the button's focus id, for click hit-testing 1275 pub fn buttonId(self: *const SubmitButton) usize { 1276 return self.box.children.keys()[1]; 1277 } 1278 }; 1279 1280 pub const Center = struct { 1281 focus: *Focus, 1282 grid: ?Grid, 1283 child: *Widget, 1284 1285 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1286 const child = try allocator.create(Widget); 1287 errdefer allocator.destroy(child); 1288 child.* = child_widget; 1289 return .{ 1290 .focus = try Focus.create(allocator, .container), 1291 .grid = null, 1292 .child = child, 1293 }; 1294 } 1295 1296 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1297 self.focus.destroy(allocator); 1298 if (self.grid) |*grid| { 1299 grid.deinit(); 1300 self.grid = null; 1301 } 1302 self.child.deinit(allocator); 1303 allocator.destroy(self.child); 1304 } 1305 1306 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1307 self.clearGrid(); 1308 self.getFocus().clear(); 1309 1310 // build the child without forcing it to fill min_size; it sizes 1311 // itself to its content within the available max. 1312 try self.child.build(allocator, .{ 1313 .min_size = .{ .width = null, .height = null }, 1314 .max_size = constraint.max_size, 1315 }, root_focus); 1316 1317 const child_grid = self.child.getGrid() orelse return; 1318 1319 // prefer max when given; otherwise grow to max(min, child) so that 1320 // when only a min is set (e.g. the wasm path passes viewport rows 1321 // as min_height with no max), we can still vertically center while 1322 // letting taller content extend past the min. 1323 const width = if (constraint.max_size.width) |w| w else if (constraint.min_size.width) |min_w| @max(min_w, child_grid.size.width) else child_grid.size.width; 1324 const height = if (constraint.max_size.height) |h| h else if (constraint.min_size.height) |min_h| @max(min_h, child_grid.size.height) else child_grid.size.height; 1325 if (width == 0 or height == 0) return; 1326 1327 const offset_x: usize = (width -| child_grid.size.width) / 2; 1328 const offset_y: usize = (height -| child_grid.size.height) / 2; 1329 1330 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1331 errdefer grid.deinit(); 1332 try grid.drawGrid(child_grid, offset_x, offset_y); 1333 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1334 self.getFocus().child_id = self.child.getFocus().id; 1335 1336 self.grid = grid; 1337 } 1338 1339 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1340 try self.child.input(allocator, key, root_focus); 1341 } 1342 1343 pub fn clearGrid(self: *Center) void { 1344 if (self.grid) |*grid| { 1345 grid.deinit(); 1346 self.grid = null; 1347 } 1348 self.child.clearGrid(); 1349 } 1350 1351 pub fn getGrid(self: Center) ?Grid { 1352 return self.grid; 1353 } 1354 1355 pub fn getFocus(self: *Center) *Focus { 1356 return self.focus; 1357 } 1358 }; 1359 1360 // renders truecolor ANSI art 1361 pub const AnsiArt = struct { 1362 focus: *Focus, 1363 grid: ?Grid, 1364 content: []const u8, 1365 1366 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1367 return .{ 1368 .focus = try Focus.create(allocator, .container), 1369 .grid = null, 1370 .content = content, 1371 }; 1372 } 1373 1374 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1375 self.focus.destroy(allocator); 1376 if (self.grid) |*grid| { 1377 grid.deinit(); 1378 self.grid = null; 1379 } 1380 } 1381 1382 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1383 _ = root_focus; 1384 self.clearGrid(); 1385 1386 // parse into rows of cells, then copy into a rectangular grid 1387 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1388 defer { 1389 for (rows.items) |*row| row.deinit(allocator); 1390 rows.deinit(allocator); 1391 } 1392 var row: std.ArrayList(Grid.Cell) = .empty; 1393 errdefer row.deinit(allocator); 1394 1395 var style: Grid.Style = .{}; 1396 var width: usize = 0; 1397 const content = self.content; 1398 var i: usize = 0; 1399 while (i < content.len) { 1400 const byte = content[i]; 1401 if (byte == '\n') { 1402 width = @max(width, row.items.len); 1403 try rows.append(allocator, row); 1404 row = .empty; 1405 i += 1; 1406 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1407 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1408 var j = i + 2; 1409 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1410 if (j >= content.len) { 1411 i = content.len; // malformed trailing escape; stop 1412 } else { 1413 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1414 i = j + 1; 1415 } 1416 } else { 1417 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1418 const end = @min(content.len, i + len); 1419 const rune = content[i..end]; 1420 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 1421 // a foreground color does not make a space visible. only an 1422 // effective background makes it opaque; foreground state often 1423 // remains active through the padding at the end of a row. 1424 const effective_background = if (style.inverted) style.fg else style.bg; 1425 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 1426 try row.append(allocator, .{ 1427 .rune = if (transparent) null else codepoint, 1428 .style = style, 1429 }); 1430 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 1431 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 1432 } 1433 i = end; 1434 } 1435 } 1436 // a trailing row with content but no closing newline 1437 if (row.items.len > 0) { 1438 width = @max(width, row.items.len); 1439 try rows.append(allocator, row); 1440 } else { 1441 row.deinit(allocator); 1442 } 1443 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1444 1445 const height = rows.items.len; 1446 if (width == 0 or height == 0) return; 1447 1448 const clamped_w = @min(width, constraint.max_size.width orelse width); 1449 const clamped_h = @min(height, constraint.max_size.height orelse height); 1450 1451 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1452 errdefer grid.deinit(); 1453 for (rows.items[0..clamped_h], 0..) |r, y| { 1454 const n = @min(r.items.len, clamped_w); 1455 for (r.items[0..n], 0..) |cell, x| { 1456 if (cell.continuation) continue; 1457 (try grid.cell(x, y)).style = cell.style; 1458 try grid.setRune(x, y, cell.rune); 1459 } 1460 } 1461 self.grid = grid; 1462 } 1463 1464 pub fn clearGrid(self: *AnsiArt) void { 1465 if (self.grid) |*grid| { 1466 grid.deinit(); 1467 self.grid = null; 1468 } 1469 } 1470 1471 pub fn getGrid(self: AnsiArt) ?Grid { 1472 return self.grid; 1473 } 1474 1475 pub fn getFocus(self: *AnsiArt) *Focus { 1476 return self.focus; 1477 } 1478 1479 fn applySgr(style: *Grid.Style, params: []const u8) void { 1480 var nums: [16]u32 = undefined; 1481 var n: usize = 0; 1482 var it = std.mem.splitScalar(u8, params, ';'); 1483 while (it.next()) |tok| { 1484 if (n >= nums.len) break; 1485 // an empty parameter (e.g. bare "\x1b[m") means 0 1486 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1487 n += 1; 1488 } 1489 if (n == 0) { 1490 style.* = .{}; 1491 return; 1492 } 1493 var i: usize = 0; 1494 while (i < n) : (i += 1) { 1495 switch (nums[i]) { 1496 0 => style.* = .{}, 1497 7 => style.inverted = true, 1498 27 => style.inverted = false, 1499 39 => style.fg = null, 1500 49 => style.bg = null, 1501 38, 48 => { 1502 // truecolor form: 38;2;r;g;b — anything else is ignored 1503 if (i + 4 < n and nums[i + 1] == 2) { 1504 const c = Grid.Color{ .rgb = .{ 1505 .r = @truncate(nums[i + 2]), 1506 .g = @truncate(nums[i + 3]), 1507 .b = @truncate(nums[i + 4]), 1508 } }; 1509 if (nums[i] == 38) style.fg = c else style.bg = c; 1510 i += 4; 1511 } 1512 }, 1513 else => {}, 1514 } 1515 } 1516 } 1517 }; 1518 1519 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1520 pub const AnsiBackground = struct { 1521 grid: ?Grid, 1522 child: *Widget, 1523 art: AnsiArt, 1524 session: *ui.Session, 1525 1526 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1527 var cw = child_widget; 1528 const child = allocator.create(Widget) catch |e| { 1529 cw.deinit(allocator); 1530 return e; 1531 }; 1532 child.* = cw; 1533 errdefer { 1534 child.deinit(allocator); 1535 allocator.destroy(child); 1536 } 1537 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1538 } 1539 1540 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1541 if (self.grid) |*grid| grid.deinit(); 1542 self.art.deinit(allocator); 1543 self.child.deinit(allocator); 1544 allocator.destroy(self.child); 1545 } 1546 1547 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1548 self.clearGrid(); 1549 1550 // make the wrapped page fill the whole available area 1551 try self.child.build(allocator, .{ 1552 .min_size = .{ 1553 .width = constraint.max_size.width orelse constraint.min_size.width, 1554 .height = constraint.max_size.height orelse constraint.min_size.height, 1555 }, 1556 .max_size = constraint.max_size, 1557 }, root_focus); 1558 1559 if (!self.session.data.enable_ansi) return; 1560 const foreground = self.child.getGrid() orelse return; 1561 self.art.content = self.session.data.ansi_art; 1562 try self.buildArt(allocator, foreground.size, root_focus); 1563 if (!self.session.is_terminal) return; 1564 const art_grid = self.art.getGrid() orelse return; 1565 self.grid = try artBehind(allocator, foreground, art_grid); 1566 } 1567 1568 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1569 try self.child.input(allocator, key, root_focus); 1570 } 1571 1572 pub fn clearGrid(self: *AnsiBackground) void { 1573 if (self.grid) |*grid| { 1574 grid.deinit(); 1575 self.grid = null; 1576 } 1577 self.child.clearGrid(); 1578 self.art.clearGrid(); 1579 } 1580 1581 pub fn getGrid(self: AnsiBackground) ?Grid { 1582 return self.grid orelse self.child.getGrid(); 1583 } 1584 1585 pub fn getFocus(self: *AnsiBackground) *Focus { 1586 return self.child.getFocus(); 1587 } 1588 1589 // a foreground cell counts as blank if it's empty or a bare space without styling 1590 fn cellIsBlank(cell: Grid.Cell) bool { 1591 if (cell.continuation) return false; 1592 if (cell.rune) |rune| { 1593 return rune == ' ' and 1594 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1595 } 1596 return true; 1597 } 1598 1599 // the art is dimmed to this fraction of its brightness so foreground text 1600 // stays legible over it 1601 const art_brightness = 35; // percent 1602 1603 // the art parser only emits truecolor, so a palette color never occurs 1604 fn artRgb(color: ?Grid.Color) ?Grid.Color.Rgb { 1605 const c = color orelse return null; 1606 return switch (c) { 1607 .rgb => |v| v, 1608 else => null, 1609 }; 1610 } 1611 1612 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1613 const v = artRgb(color) orelse return color; 1614 return .{ .rgb = .{ 1615 .r = @intCast(@as(u16, v.r) * art_brightness / 100), 1616 .g = @intCast(@as(u16, v.g) * art_brightness / 100), 1617 .b = @intCast(@as(u16, v.b) * art_brightness / 100), 1618 } }; 1619 } 1620 1621 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 1622 try self.art.build(allocator, .{ 1623 .min_size = .{ .width = null, .height = null }, 1624 .max_size = .{ .width = size.width, .height = size.height }, 1625 }, root_focus); 1626 if (self.art.grid) |*grid| { 1627 for (grid.cells) |*cell| { 1628 cell.style.fg = dimColor(cell.style.fg); 1629 cell.style.bg = dimColor(cell.style.bg); 1630 } 1631 } 1632 } 1633 1634 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1635 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1636 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1637 // snap such channels clear of that range so the backdrop doesn't blink or 1638 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1639 // actually render truecolor. 1640 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1641 const v = artRgb(color) orelse return color; 1642 const snap = struct { 1643 fn f(c: u8) u8 { 1644 return if (c >= 1 and c <= 9) 10 else c; 1645 } 1646 }.f; 1647 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 1648 } 1649 1650 fn applyArtBackground(dst: *Grid.Cell, src: Grid.Cell) void { 1651 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 1652 dst.style.bg = background_maybe; 1653 // change unstyled text to contrast with the art behind it 1654 if (dst.style.fg == null) { 1655 if (artRgb(background_maybe)) |v| { 1656 // use near-black and near-white, which is easier on the eyes 1657 // while retaining high contrast 1658 const luminance = (@as(u32, v.r) * 299 + 1659 @as(u32, v.g) * 587 + 1660 @as(u32, v.b) * 114) / 1000; 1661 dst.style.fg = if (luminance >= 128) 1662 .{ .rgb = .{ .r = 16, .g = 16, .b = 16 } } 1663 else 1664 .{ .rgb = .{ .r = 240, .g = 240, .b = 240 } }; 1665 } 1666 } 1667 } 1668 1669 // composites ANSI art behind a foreground grid 1670 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1671 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1672 errdefer out.deinit(); 1673 1674 const anchor_x = foreground.size.width -| art_grid.size.width; 1675 for (0..art_grid.size.height) |y| { 1676 for (0..art_grid.size.width) |x| { 1677 const src = (try art_grid.cell(x, y)).*; 1678 if (src.rune == null) continue; 1679 const dst = try out.cell(anchor_x + x, y); 1680 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1681 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1682 // blank cells take the art; occupied cells take its background 1683 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1684 dst.style = src.style; 1685 dst.style.fg = sgrSafe(dst.style.fg); 1686 dst.style.bg = sgrSafe(dst.style.bg); 1687 try out.setRune(anchor_x + x, y, src.rune); 1688 } else { 1689 applyArtBackground(dst, src); 1690 if (next_dst) |next| applyArtBackground(next, src); 1691 } 1692 } 1693 } 1694 return out; 1695 } 1696 }; 1697 1698 pub const CopyableText = struct { 1699 pub const Choice = struct { 1700 selector: []const u8 = "", 1701 text: []const u8, 1702 copyable_text: ?[]const u8 = null, 1703 label: []const u8 = "", 1704 bottom_label: []const u8 = "", 1705 }; 1706 1707 box: wgt.Box(Widget), 1708 session: *ui.Session, 1709 choices: []Choice, 1710 selected: usize = 0, 1711 1712 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1713 if (choices.len == 0) return error.MissingCopyableText; 1714 1715 const owned_choices = try allocator.dupe(Choice, choices); 1716 errdefer allocator.free(owned_choices); 1717 1718 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1719 errdefer box.deinit(allocator); 1720 1721 if (choices.len > 1) for (choices) |choice| { 1722 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .round_corners = true, .wrap_kind = .none }); 1723 errdefer selector_box.deinit(allocator); 1724 selector_box.getFocus().mode = .all; 1725 try box.children.put(allocator, selector_box.getFocus().id, .{ .widget = .{ .text_box = selector_box }, .rect = null, .min_size = .{ .width = choice.selector.len + 2, .height = 3 } }); 1726 }; 1727 1728 var text_input = try wgt.TextInput.init(allocator, .{ 1729 .border_style = .single, 1730 .round_corners = true, 1731 .label = choices[0].label, 1732 .bottom_label = choices[0].bottom_label, 1733 .read_only = true, 1734 .render_content = session.is_terminal, 1735 .visible_width = null, 1736 }); 1737 errdefer text_input.deinit(allocator); 1738 text_input.getFocus().mode = .all; 1739 try text_input.setContent(allocator, choices[0].text); 1740 box.getFocus().child_id = text_input.getFocus().id; 1741 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1742 1743 return .{ .box = box, .session = session, .choices = owned_choices }; 1744 } 1745 1746 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1747 const aa = session.page_arena.allocator(); 1748 var choices: [2]Choice = undefined; 1749 var count: usize = 0; 1750 switch (location) { 1751 .repo => |identity| { 1752 if (session.data.git_http_port) |port| { 1753 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1754 choices[count] = .{ 1755 .selector = "http", 1756 .text = url, 1757 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1758 .label = " clone ", 1759 }; 1760 count += 1; 1761 } 1762 if (session.data.git_ssh_port) |port| { 1763 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1764 choices[count] = .{ 1765 .selector = "ssh", 1766 .text = url, 1767 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1768 .label = " clone ", 1769 }; 1770 count += 1; 1771 } 1772 }, 1773 .fork => |f| if (session.data.git_ssh_port) |port| { 1774 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1775 choices[count] = .{ 1776 .text = url, 1777 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1778 .label = " clone ", 1779 }; 1780 count += 1; 1781 }, 1782 } 1783 if (count == 0) return null; 1784 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1785 } 1786 1787 pub fn minWidth(self: *const CopyableText) usize { 1788 var width = maxWidth(self.choices) + 2; 1789 if (self.choices.len > 1) { 1790 for (self.choices) |choice| width += choice.selector.len + 2; 1791 } 1792 return width; 1793 } 1794 1795 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1796 self.box.deinit(allocator); 1797 allocator.free(self.choices); 1798 } 1799 1800 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1801 self.clearGrid(); 1802 1803 if (self.choices.len > 1) { 1804 if (root_focus.grandchild_id) |id| { 1805 for (0..self.choices.len) |i| { 1806 if (id != self.selector(i).getFocus().id) continue; 1807 if (i != self.selected) try self.select(allocator, i); 1808 root_focus.setFocus(self.textInput().getFocus().id); 1809 break; 1810 } 1811 } 1812 1813 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1814 for (0..self.choices.len) |i| { 1815 self.selector(i).options.border_style = if (i == self.selected) 1816 (if (focused) .double else .single) 1817 else 1818 .hidden; 1819 } 1820 } 1821 self.textInput().options.border_style = .single; 1822 self.textInput().cursor = 0; 1823 self.textInput().scroll_offset = 0; 1824 try self.box.build(allocator, constraint, root_focus); 1825 1826 if (!self.session.is_terminal) { 1827 const aa = self.session.arena.allocator(); 1828 const id = self.textInput().getFocus().id; 1829 try self.session.text_inputs.put(aa, id, self.textInput()); 1830 } 1831 } 1832 1833 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1834 switch (key) { 1835 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1836 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1837 .enter => if (self.session.is_terminal) self.show(), 1838 .mouse => |mouse| { 1839 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1840 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1841 try self.select(allocator, i); 1842 root_focus.setFocus(self.textInput().getFocus().id); 1843 return; 1844 }; 1845 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1846 }, 1847 else => {}, 1848 } 1849 } 1850 1851 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1852 self.selected = selected; 1853 const choice = self.choices[selected]; 1854 const text_input = self.textInput(); 1855 text_input.options.label = choice.label; 1856 text_input.options.bottom_label = choice.bottom_label; 1857 try text_input.setContent(allocator, choice.text); 1858 } 1859 1860 fn show(self: *CopyableText) void { 1861 const choice = self.choices[self.selected]; 1862 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1863 } 1864 1865 fn textInput(self: *CopyableText) *wgt.TextInput { 1866 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1867 } 1868 1869 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1870 return &self.box.children.values()[index].widget.text_box; 1871 } 1872 1873 fn maxWidth(choices: []const Choice) usize { 1874 var width: usize = 0; 1875 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1876 return width; 1877 } 1878 1879 pub fn clearGrid(self: *CopyableText) void { 1880 self.box.clearGrid(); 1881 } 1882 1883 pub fn getGrid(self: CopyableText) ?Grid { 1884 return self.box.getGrid(); 1885 } 1886 1887 pub fn getFocus(self: *CopyableText) *Focus { 1888 return self.box.getFocus(); 1889 } 1890 }; 1891