╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
510 if (self.grid) |*grid| { 511 grid.deinit(); 512 self.grid = null; 513 } 514 for (self.text_boxes.items) |*tb| tb.clearGrid(); 515 } 516 517 pub fn getGrid(self: TagFlow) ?Grid { 518 return self.grid; 519 } 520 521 pub fn getFocus(self: *TagFlow) *Focus { 522 return self.focus; 523 } 524 525 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 526 for (self.text_boxes.items, 0..) |tb, i| { 527 if (tb.focus.id == focus_id) return i; 528 } 529 return null; 530 } 531 532 // the first item of the row below (when `downward`) or above, or null at 533 // the flow's edge. items are laid out in order, so y is nondecreasing. 534 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 535 const rects = self.rects.items; 536 if (index >= rects.len) return null; 537 const cur_y = rects[index].y; 538 if (downward) { 539 for (rects[index + 1 ..], index + 1..) |r, i| { 540 if (r.y > cur_y) return i; 541 } 542 return null; 543 } 544 // walk the earlier rows; the last one visited is the row above. 545 var prev_first: ?usize = null; 546 var i: usize = 0; 547 while (i < rects.len and rects[i].y < cur_y) { 548 prev_first = i; 549 const y = rects[i].y; 550 while (i < rects.len and rects[i].y == y) i += 1; 551 } 552 return prev_first; 553 } 554 }; 555 556 // an invisible widget that fills the horizontal space granted by its parent. 557 // used inside Box(horiz) with a min_size so the box reserves space for the 558 // children that follow, pushing them to the right. 559 pub const Spacer = struct { 560 focus: *Focus, 561 grid: ?Grid, 562 563 pub fn init(allocator: std.mem.Allocator) !Spacer { 564 return .{ 565 .focus = try Focus.create(allocator, .container), 566 .grid = null, 567 }; 568 } 569 570 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 571 self.focus.destroy(allocator); 572 if (self.grid) |*grid| { 573 grid.deinit(); 574 self.grid = null; 575 } 576 } 577 578 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 579 _ = root_focus; 580 self.clearGrid(); 581 const width = constraint.max_size.width orelse return; 582 if (width == 0) return; 583 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 584 } 585 586 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 587 _ = self; 588 _ = allocator; 589 _ = key; 590 _ = root_focus; 591 } 592 593 pub fn clearGrid(self: *Spacer) void { 594 if (self.grid) |*grid| { 595 grid.deinit(); 596 self.grid = null; 597 } 598 } 599 600 pub fn getGrid(self: Spacer) ?Grid { 601 return self.grid; 602 } 603 604 pub fn getFocus(self: *Spacer) *Focus { 605 return self.focus; 606 } 607 }; 608 609 // a one-row, non-focusable status bar showing the current page's url 610 pub const Footer = struct { 611 focus: *Focus, 612 grid: ?Grid, 613 session: *ui.Session, 614 arena: std.heap.ArenaAllocator, 615 url: []const u8 = "", 616 url_width: usize = 0, 617 618 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !Footer { 619 return .{ 620 .focus = try Focus.create(allocator, .container), 621 .grid = null, 622 .session = session, 623 .arena = std.heap.ArenaAllocator.init(allocator), 624 }; 625 } 626 627 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 628 self.focus.destroy(allocator); 629 if (self.grid) |*grid| grid.deinit(); 630 self.arena.deinit(); 631 } 632 633 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 634 _ = root_focus; 635 self.clearGrid(); 636 const width = constraint.max_size.width orelse return; 637 if (width == 0) return; 638 639 _ = self.arena.reset(.retain_capacity); 640 self.url = ""; 641 self.url_width = 0; 642 const aa = self.arena.allocator(); 643 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 644 const text = ui.terminalWebUrl(aa, self.session, path) catch return; 645 self.url = text; 646 647 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 648 errdefer grid.deinit(); 649 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 650 var i: usize = 0; 651 while (utf8.nextCodepoint()) |ch| { 652 const rune_width = xitui.width.cellWidth(ch); 653 if (i + rune_width > width) break; 654 try grid.setRune(i, 0, ch); 655 i += rune_width; 656 } 657 self.url_width = i; 658 self.grid = grid; 659 } 660 661 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 662 _ = allocator; 663 switch (key) { 664 .mouse => |mouse| { 665 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 666 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 667 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 668 }, 669 else => {}, 670 } 671 } 672 673 pub fn clearGrid(self: *Footer) void { 674 if (self.grid) |*grid| { 675 grid.deinit(); 676 self.grid = null; 677 } 678 } 679 680 pub fn getGrid(self: Footer) ?Grid { 681 return self.grid; 682 } 683 684 pub fn getFocus(self: *Footer) *Focus { 685 return self.focus; 686 } 687 }; 688 689 // a single-child wrapper that builds the child at its natural size and 690 // positions its grid in the middle of the area granted by the parent 691 // a bordered label, indented past the content it precedes 692 pub const SectionLabel = struct { 693 box: wgt.Box(Widget), 694 695 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 696 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 697 errdefer box.deinit(allocator); 698 699 { 700 var gap = try wgt.Text.init(allocator, " "); 701 errdefer gap.deinit(allocator); 702 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 703 } 704 705 { 706 var tb = try wgt.TextBox.init(allocator, content, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 707 errdefer tb.deinit(allocator); 708 tb.getFocus().focusable = true; 709 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 710 } 711 712 box.getFocus().child_id = box.children.keys()[1]; 713 return .{ .box = box }; 714 } 715 716 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 717 self.box.deinit(allocator); 718 } 719 720 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 721 try self.box.build(allocator, constraint, root_focus); 722 } 723 724 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 725 _ = self; 726 _ = allocator; 727 _ = key; 728 _ = root_focus; 729 } 730 731 pub fn clearGrid(self: *SectionLabel) void { 732 self.box.clearGrid(); 733 } 734 735 pub fn getGrid(self: SectionLabel) ?Grid { 736 return self.box.getGrid(); 737 } 738 739 pub fn getFocus(self: *SectionLabel) *Focus { 740 return self.box.getFocus(); 741 } 742 }; 743 744 // a submit button, indented past its form's content. the web form POSTs 745 // the button's form; terminal hosts detect presses through `buttonId`. 746 pub const SubmitButton = struct { 747 box: wgt.Box(Widget), 748 749 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 750 return initLabeled(allocator, "submit"); 751 } 752 753 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 754 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 755 errdefer box.deinit(allocator); 756 757 { 758 var gap = try wgt.Text.init(allocator, " "); 759 errdefer gap.deinit(allocator); 760 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 761 } 762 763 { 764 var button = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 765 errdefer button.deinit(allocator); 766 button.getFocus().focusable = true; 767 // the renderer distinguishes plain clickables from buttons that 768 // should POST to a server route by this kind. 769 button.getFocus().kind = .{ .custom = "submit" }; 770 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 771 } 772 773 box.getFocus().child_id = box.children.keys()[1]; 774 return .{ .box = box }; 775 } 776 777 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 778 self.box.deinit(allocator); 779 } 780 781 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 782 try self.box.build(allocator, constraint, root_focus); 783 } 784 785 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 786 _ = self; 787 _ = allocator; 788 _ = key; 789 _ = root_focus; 790 } 791 792 pub fn clearGrid(self: *SubmitButton) void { 793 self.box.clearGrid(); 794 } 795 796 pub fn getGrid(self: SubmitButton) ?Grid { 797 return self.box.getGrid(); 798 } 799 800 pub fn getFocus(self: *SubmitButton) *Focus { 801 return self.box.getFocus(); 802 } 803 804 // the button's focus id, for click hit-testing 805 pub fn buttonId(self: *const SubmitButton) usize { 806 return self.box.children.keys()[1]; 807 } 808 }; 809 810 pub const Center = struct { 811 focus: *Focus, 812 grid: ?Grid, 813 child: *Widget, 814 815 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 816 const child = try allocator.create(Widget); 817 errdefer allocator.destroy(child); 818 child.* = child_widget; 819 return .{ 820 .focus = try Focus.create(allocator, .container), 821 .grid = null, 822 .child = child, 823 }; 824 } 825 826 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 827 self.focus.destroy(allocator); 828 if (self.grid) |*grid| { 829 grid.deinit(); 830 self.grid = null; 831 } 832 self.child.deinit(allocator); 833 allocator.destroy(self.child); 834 } 835 836 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 837 self.clearGrid(); 838 self.getFocus().clear(); 839 840 // build the child without forcing it to fill min_size; it sizes 841 // itself to its content within the available max. 842 try self.child.build(allocator, .{ 843 .min_size = .{ .width = null, .height = null }, 844 .max_size = constraint.max_size, 845 }, root_focus); 846 847 const child_grid = self.child.getGrid() orelse return; 848 849 // prefer max when given; otherwise grow to max(min, child) so that 850 // when only a min is set (e.g. the wasm path passes viewport rows 851 // as min_height with no max), we can still vertically center while 852 // letting taller content extend past the min. 853 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; 854 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; 855 if (width == 0 or height == 0) return; 856 857 const offset_x: usize = (width -| child_grid.size.width) / 2; 858 const offset_y: usize = (height -| child_grid.size.height) / 2; 859 860 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 861 errdefer grid.deinit(); 862 try grid.drawGrid(child_grid, offset_x, offset_y); 863 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 864 self.getFocus().child_id = self.child.getFocus().id; 865 866 self.grid = grid; 867 } 868 869 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 870 try self.child.input(allocator, key, root_focus); 871 } 872 873 pub fn clearGrid(self: *Center) void { 874 if (self.grid) |*grid| { 875 grid.deinit(); 876 self.grid = null; 877 } 878 self.child.clearGrid(); 879 } 880 881 pub fn getGrid(self: Center) ?Grid { 882 return self.grid; 883 } 884 885 pub fn getFocus(self: *Center) *Focus { 886 return self.focus; 887 } 888 }; 889 890 // renders truecolor ANSI art 891 pub const AnsiArt = struct { 892 focus: *Focus, 893 grid: ?Grid, 894 content: []const u8, 895 896 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 897 return .{ 898 .focus = try Focus.create(allocator, .container), 899 .grid = null, 900 .content = content, 901 }; 902 } 903 904 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 905 self.focus.destroy(allocator); 906 if (self.grid) |*grid| { 907 grid.deinit(); 908 self.grid = null; 909 } 910 } 911 912 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 913 _ = root_focus; 914 self.clearGrid(); 915 916 // parse into rows of cells, then copy into a rectangular grid 917 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 918 defer { 919 for (rows.items) |*row| row.deinit(allocator); 920 rows.deinit(allocator); 921 } 922 var row: std.ArrayList(Grid.Cell) = .empty; 923 errdefer row.deinit(allocator); 924 925 var style: Grid.Style = .{}; 926 var width: usize = 0; 927 const content = self.content; 928 var i: usize = 0; 929 while (i < content.len) { 930 const byte = content[i]; 931 if (byte == '\n') { 932 width = @max(width, row.items.len); 933 try rows.append(allocator, row); 934 row = .empty; 935 i += 1; 936 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 937 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 938 var j = i + 2; 939 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 940 if (j >= content.len) { 941 i = content.len; // malformed trailing escape; stop 942 } else { 943 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 944 i = j + 1; 945 } 946 } else { 947 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 948 const end = @min(content.len, i + len); 949 const rune = content[i..end]; 950 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 951 // a foreground color does not make a space visible. only an 952 // effective background makes it opaque; foreground state often 953 // remains active through the padding at the end of a row. 954 const effective_background = if (style.inverted) style.fg else style.bg; 955 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 956 try row.append(allocator, .{ 957 .rune = if (transparent) null else codepoint, 958 .style = style, 959 }); 960 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 961 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 962 } 963 i = end; 964 } 965 } 966 // a trailing row with content but no closing newline 967 if (row.items.len > 0) { 968 width = @max(width, row.items.len); 969 try rows.append(allocator, row); 970 } else { 971 row.deinit(allocator); 972 } 973 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 974 975 const height = rows.items.len; 976 if (width == 0 or height == 0) return; 977 978 const clamped_w = @min(width, constraint.max_size.width orelse width); 979 const clamped_h = @min(height, constraint.max_size.height orelse height); 980 981 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 982 errdefer grid.deinit(); 983 for (rows.items[0..clamped_h], 0..) |r, y| { 984 const n = @min(r.items.len, clamped_w); 985 for (r.items[0..n], 0..) |cell, x| { 986 if (cell.continuation) continue; 987 (try grid.cell(x, y)).style = cell.style; 988 try grid.setRune(x, y, cell.rune); 989 } 990 } 991 self.grid = grid; 992 } 993 994 pub fn clearGrid(self: *AnsiArt) void { 995 if (self.grid) |*grid| { 996 grid.deinit(); 997 self.grid = null; 998 } 999 } 1000 1001 pub fn getGrid(self: AnsiArt) ?Grid { 1002 return self.grid; 1003 } 1004 1005 pub fn getFocus(self: *AnsiArt) *Focus { 1006 return self.focus; 1007 } 1008 1009 fn applySgr(style: *Grid.Style, params: []const u8) void { 1010 var nums: [16]u32 = undefined; 1011 var n: usize = 0; 1012 var it = std.mem.splitScalar(u8, params, ';'); 1013 while (it.next()) |tok| { 1014 if (n >= nums.len) break; 1015 // an empty parameter (e.g. bare "\x1b[m") means 0 1016 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1017 n += 1; 1018 } 1019 if (n == 0) { 1020 style.* = .{}; 1021 return; 1022 } 1023 var i: usize = 0; 1024 while (i < n) : (i += 1) { 1025 switch (nums[i]) { 1026 0 => style.* = .{}, 1027 7 => style.inverted = true, 1028 27 => style.inverted = false, 1029 39 => style.fg = null, 1030 49 => style.bg = null, 1031 38, 48 => { 1032 // truecolor form: 38;2;r;g;b — anything else is ignored 1033 if (i + 4 < n and nums[i + 1] == 2) { 1034 const c = Grid.Color{ 1035 .r = @truncate(nums[i + 2]), 1036 .g = @truncate(nums[i + 3]), 1037 .b = @truncate(nums[i + 4]), 1038 }; 1039 if (nums[i] == 38) style.fg = c else style.bg = c; 1040 i += 4; 1041 } 1042 }, 1043 else => {}, 1044 } 1045 } 1046 } 1047 }; 1048 1049 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1050 pub const AnsiBackground = struct { 1051 grid: ?Grid, 1052 child: *Widget, 1053 art: AnsiArt, 1054 session: *ui.Session, 1055 1056 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1057 var cw = child_widget; 1058 const child = allocator.create(Widget) catch |e| { 1059 cw.deinit(allocator); 1060 return e; 1061 }; 1062 child.* = cw; 1063 errdefer { 1064 child.deinit(allocator); 1065 allocator.destroy(child); 1066 } 1067 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1068 } 1069 1070 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1071 if (self.grid) |*grid| grid.deinit(); 1072 self.art.deinit(allocator); 1073 self.child.deinit(allocator); 1074 allocator.destroy(self.child); 1075 } 1076 1077 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1078 self.clearGrid(); 1079 1080 // make the wrapped page fill the whole available area 1081 try self.child.build(allocator, .{ 1082 .min_size = .{ 1083 .width = constraint.max_size.width orelse constraint.min_size.width, 1084 .height = constraint.max_size.height orelse constraint.min_size.height, 1085 }, 1086 .max_size = constraint.max_size, 1087 }, root_focus); 1088 1089 if (!self.session.data.enable_ansi) return; 1090 const foreground = self.child.getGrid() orelse return; 1091 self.art.content = self.session.data.ansi_art; 1092 try self.buildArt(allocator, foreground.size, root_focus); 1093 if (!self.session.is_terminal) return; 1094 const art_grid = self.art.getGrid() orelse return; 1095 self.grid = try artBehind(allocator, foreground, art_grid); 1096 } 1097 1098 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1099 try self.child.input(allocator, key, root_focus); 1100 } 1101 1102 pub fn clearGrid(self: *AnsiBackground) void { 1103 if (self.grid) |*grid| { 1104 grid.deinit(); 1105 self.grid = null; 1106 } 1107 self.child.clearGrid(); 1108 self.art.clearGrid(); 1109 } 1110 1111 pub fn getGrid(self: AnsiBackground) ?Grid { 1112 return self.grid orelse self.child.getGrid(); 1113 } 1114 1115 pub fn getFocus(self: *AnsiBackground) *Focus { 1116 return self.child.getFocus(); 1117 } 1118 1119 // a foreground cell counts as blank if it's empty or a bare space without styling 1120 fn cellIsBlank(cell: Grid.Cell) bool { 1121 if (cell.continuation) return false; 1122 if (cell.rune) |rune| { 1123 return rune == ' ' and 1124 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1125 } 1126 return true; 1127 } 1128 1129 // the art is dimmed to this fraction of its brightness so foreground text 1130 // stays legible over it 1131 const art_brightness = 35; // percent 1132 1133 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1134 const c = color orelse return null; 1135 return .{ 1136 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1137 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1138 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1139 }; 1140 } 1141 1142 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 1143 try self.art.build(allocator, .{ 1144 .min_size = .{ .width = null, .height = null }, 1145 .max_size = .{ .width = size.width, .height = size.height }, 1146 }, root_focus); 1147 if (self.art.grid) |*grid| { 1148 for (grid.cells) |*cell| { 1149 cell.style.fg = dimColor(cell.style.fg); 1150 cell.style.bg = dimColor(cell.style.bg); 1151 } 1152 } 1153 } 1154 1155 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1156 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1157 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1158 // snap such channels clear of that range so the backdrop doesn't blink or 1159 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1160 // actually render truecolor. 1161 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1162 const c = color orelse return null; 1163 const snap = struct { 1164 fn f(v: u8) u8 { 1165 return if (v >= 1 and v <= 9) 10 else v; 1166 } 1167 }.f; 1168 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1169 } 1170 1171 fn applyArtBackground(dst: *Grid.Cell, src: Grid.Cell) void { 1172 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 1173 dst.style.bg = background_maybe; 1174 // change unstyled text to contrast with the art behind it 1175 if (dst.style.fg == null) { 1176 if (background_maybe) |background| { 1177 // use near-black and near-white, which is easier on the eyes 1178 // while retaining high contrast 1179 const luminance = (@as(u32, background.r) * 299 + 1180 @as(u32, background.g) * 587 + 1181 @as(u32, background.b) * 114) / 1000; 1182 dst.style.fg = if (luminance >= 128) 1183 .{ .r = 16, .g = 16, .b = 16 } 1184 else 1185 .{ .r = 240, .g = 240, .b = 240 }; 1186 } 1187 } 1188 } 1189 1190 // composites ANSI art behind a foreground grid 1191 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1192 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1193 errdefer out.deinit(); 1194 1195 const anchor_x = foreground.size.width -| art_grid.size.width; 1196 for (0..art_grid.size.height) |y| { 1197 for (0..art_grid.size.width) |x| { 1198 const src = (try art_grid.cell(x, y)).*; 1199 if (src.rune == null) continue; 1200 const dst = try out.cell(anchor_x + x, y); 1201 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1202 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1203 // blank cells take the art; occupied cells take its background 1204 if (cellIsBlank(dst.*) and (next_dst == null or cellIsBlank(next_dst.?.*))) { 1205 dst.style = src.style; 1206 dst.style.fg = sgrSafe(dst.style.fg); 1207 dst.style.bg = sgrSafe(dst.style.bg); 1208 try out.setRune(anchor_x + x, y, src.rune); 1209 } else { 1210 applyArtBackground(dst, src); 1211 if (next_dst) |next| applyArtBackground(next, src); 1212 } 1213 } 1214 } 1215 return out; 1216 } 1217 }; 1218 1219 pub const CopyableText = struct { 1220 pub const Choice = struct { 1221 selector: []const u8 = "", 1222 text: []const u8, 1223 copyable_text: ?[]const u8 = null, 1224 label: []const u8 = "", 1225 bottom_label: []const u8 = "", 1226 }; 1227 1228 box: wgt.Box(Widget), 1229 session: *ui.Session, 1230 choices: []Choice, 1231 selected: usize = 0, 1232 1233 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1234 if (choices.len == 0) return error.MissingCopyableText; 1235 1236 const owned_choices = try allocator.dupe(Choice, choices); 1237 errdefer allocator.free(owned_choices); 1238 1239 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1240 errdefer box.deinit(allocator); 1241 1242 if (choices.len > 1) for (choices) |choice| { 1243 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .wrap_kind = .none }); 1244 errdefer selector_box.deinit(allocator); 1245 selector_box.getFocus().focusable = true; 1246 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 } }); 1247 }; 1248 1249 var text_input = try wgt.TextInput.init(allocator, .{ 1250 .border_style = .single, 1251 .label = choices[0].label, 1252 .bottom_label = choices[0].bottom_label, 1253 .read_only = true, 1254 .render_content = session.is_terminal, 1255 .visible_width = null, 1256 }); 1257 errdefer text_input.deinit(allocator); 1258 text_input.getFocus().focusable = true; 1259 try text_input.setContent(allocator, choices[0].text); 1260 box.getFocus().child_id = text_input.getFocus().id; 1261 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1262 1263 return .{ .box = box, .session = session, .choices = owned_choices }; 1264 } 1265 1266 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1267 const aa = session.page_arena.allocator(); 1268 var choices: [2]Choice = undefined; 1269 var count: usize = 0; 1270 switch (location) { 1271 .repo => |identity| { 1272 if (session.data.git_http_port) |port| { 1273 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1274 choices[count] = .{ 1275 .selector = "http", 1276 .text = url, 1277 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1278 .label = " clone ", 1279 }; 1280 count += 1; 1281 } 1282 if (session.data.git_ssh_port) |port| { 1283 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1284 choices[count] = .{ 1285 .selector = "ssh", 1286 .text = url, 1287 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1288 .label = " clone ", 1289 }; 1290 count += 1; 1291 } 1292 }, 1293 .fork => |f| if (f.target_branch.len != 0) if (session.data.git_ssh_port) |port| { 1294 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}/patch:{s}/branch:{s}", .{ port, f.identity, f.id, try ui.urlEncodeRef(aa, f.target_branch) }); 1295 choices[count] = .{ 1296 .text = url, 1297 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1298 .label = " clone ", 1299 }; 1300 count += 1; 1301 }, 1302 } 1303 if (count == 0) return null; 1304 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1305 } 1306 1307 pub fn minWidth(self: *const CopyableText) usize { 1308 var width = maxWidth(self.choices) + 2; 1309 if (self.choices.len > 1) { 1310 for (self.choices) |choice| width += choice.selector.len + 2; 1311 } 1312 return width; 1313 } 1314 1315 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1316 self.box.deinit(allocator); 1317 allocator.free(self.choices); 1318 } 1319 1320 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1321 self.clearGrid(); 1322 1323 if (self.choices.len > 1) { 1324 if (root_focus.grandchild_id) |id| { 1325 for (0..self.choices.len) |i| { 1326 if (id != self.selector(i).getFocus().id) continue; 1327 if (i != self.selected) try self.select(allocator, i); 1328 root_focus.setFocus(self.textInput().getFocus().id); 1329 break; 1330 } 1331 } 1332 1333 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1334 for (0..self.choices.len) |i| { 1335 self.selector(i).options.border_style = if (i == self.selected) 1336 (if (focused) .double else .single) 1337 else 1338 .hidden; 1339 } 1340 } 1341 self.textInput().options.border_style = .single; 1342 self.textInput().cursor = 0; 1343 self.textInput().scroll_offset = 0; 1344 try self.box.build(allocator, constraint, root_focus); 1345 1346 if (!self.session.is_terminal) { 1347 const aa = self.session.arena.allocator(); 1348 const id = self.textInput().getFocus().id; 1349 try self.session.text_inputs.put(aa, id, self.textInput()); 1350 } 1351 } 1352 1353 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1354 switch (key) { 1355 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1356 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1357 .enter => if (self.session.is_terminal) self.show(), 1358 .mouse => |mouse| { 1359 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1360 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1361 try self.select(allocator, i); 1362 root_focus.setFocus(self.textInput().getFocus().id); 1363 return; 1364 }; 1365 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1366 }, 1367 else => {}, 1368 } 1369 } 1370 1371 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1372 self.selected = selected; 1373 const choice = self.choices[selected]; 1374 const text_input = self.textInput(); 1375 text_input.options.label = choice.label; 1376 text_input.options.bottom_label = choice.bottom_label; 1377 try text_input.setContent(allocator, choice.text); 1378 } 1379 1380 fn show(self: *CopyableText) void { 1381 const choice = self.choices[self.selected]; 1382 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1383 } 1384 1385 fn textInput(self: *CopyableText) *wgt.TextInput { 1386 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1387 } 1388 1389 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1390 return &self.box.children.values()[index].widget.text_box; 1391 } 1392 1393 fn maxWidth(choices: []const Choice) usize { 1394 var width: usize = 0; 1395 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1396 return width; 1397 } 1398 1399 pub fn clearGrid(self: *CopyableText) void { 1400 self.box.clearGrid(); 1401 } 1402 1403 pub fn getGrid(self: CopyableText) ?Grid { 1404 return self.box.getGrid(); 1405 } 1406 1407 pub fn getFocus(self: *CopyableText) *Focus { 1408 return self.box.getFocus(); 1409 } 1410 }; 1411