╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
729 } 730 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 731 732 const height = rows.items.len; 733 if (width == 0 or height == 0) return; 734 735 const clamped_w = @min(width, constraint.max_size.width orelse width); 736 const clamped_h = @min(height, constraint.max_size.height orelse height); 737 738 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 739 errdefer grid.deinit(); 740 for (rows.items[0..clamped_h], 0..) |r, y| { 741 const n = @min(r.items.len, clamped_w); 742 for (r.items[0..n], 0..) |cell, x| { 743 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 744 } 745 } 746 self.grid = grid; 747 } 748 749 pub fn input(self: *AnsiArt, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 750 _ = self; 751 _ = allocator; 752 _ = key; 753 _ = root_focus; 754 } 755 756 pub fn clearGrid(self: *AnsiArt) void { 757 if (self.grid) |*grid| { 758 grid.deinit(); 759 self.grid = null; 760 } 761 } 762 763 pub fn getGrid(self: AnsiArt) ?Grid { 764 return self.grid; 765 } 766 767 pub fn getFocus(self: *AnsiArt) *Focus { 768 return &self.focus; 769 } 770 771 fn applySgr(style: *Grid.Style, params: []const u8) void { 772 var nums: [16]u32 = undefined; 773 var n: usize = 0; 774 var it = std.mem.splitScalar(u8, params, ';'); 775 while (it.next()) |tok| { 776 if (n >= nums.len) break; 777 // an empty parameter (e.g. bare "\x1b[m") means 0 778 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 779 n += 1; 780 } 781 if (n == 0) { 782 style.* = .{}; 783 return; 784 } 785 var i: usize = 0; 786 while (i < n) : (i += 1) { 787 switch (nums[i]) { 788 0 => style.* = .{}, 789 7 => style.inverted = true, 790 27 => style.inverted = false, 791 39 => style.fg = null, 792 49 => style.bg = null, 793 38, 48 => { 794 // truecolor form: 38;2;r;g;b — anything else is ignored 795 if (i + 4 < n and nums[i + 1] == 2) { 796 const c = Grid.Color{ 797 .r = @truncate(nums[i + 2]), 798 .g = @truncate(nums[i + 3]), 799 .b = @truncate(nums[i + 4]), 800 }; 801 if (nums[i] == 38) style.fg = c else style.bg = c; 802 i += 4; 803 } 804 }, 805 else => {}, 806 } 807 } 808 } 809 }; 810 811 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 812 pub const AnsiBackground = struct { 813 grid: ?Grid, 814 child: *Widget, 815 art: AnsiArt, 816 817 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8) !AnsiBackground { 818 var cw = child_widget; 819 const child = allocator.create(Widget) catch |e| { 820 cw.deinit(allocator); 821 return e; 822 }; 823 child.* = cw; 824 return .{ .grid = null, .child = child, .art = AnsiArt.init(art_content) }; 825 } 826 827 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 828 if (self.grid) |*grid| grid.deinit(); 829 self.art.deinit(allocator); 830 self.child.deinit(allocator); 831 allocator.destroy(self.child); 832 } 833 834 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 835 self.clearGrid(); 836 837 // make the wrapped page fill the whole available area 838 try self.child.build(allocator, .{ 839 .min_size = .{ 840 .width = constraint.max_size.width orelse constraint.min_size.width, 841 .height = constraint.max_size.height orelse constraint.min_size.height, 842 }, 843 .max_size = constraint.max_size, 844 }, root_focus); 845 846 if (self.child.getGrid()) |fg| { 847 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 848 } 849 } 850 851 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 852 try self.child.input(allocator, key, root_focus); 853 } 854 855 pub fn clearGrid(self: *AnsiBackground) void { 856 if (self.grid) |*grid| { 857 grid.deinit(); 858 self.grid = null; 859 } 860 self.child.clearGrid(); 861 self.art.clearGrid(); 862 } 863 864 pub fn getGrid(self: AnsiBackground) ?Grid { 865 return self.grid orelse self.child.getGrid(); 866 } 867 868 pub fn getFocus(self: *AnsiBackground) *Focus { 869 return self.child.getFocus(); 870 } 871 872 // which top corner the art hugs. 873 const ArtAnchor = enum { top_left, top_right }; 874 875 // a foreground cell counts as blank if it's empty or a bare space without styling 876 fn cellIsBlank(cell: Grid.Cell) bool { 877 if (cell.rune) |rune| { 878 return std.mem.eql(u8, rune, " ") and 879 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 880 } 881 return true; 882 } 883 884 // a single representative color for an art cell 885 fn artCellColor(style: Grid.Style) ?Grid.Color { 886 const f = style.fg orelse return style.bg; 887 const b = style.bg orelse return f; 888 return .{ 889 .r = @intCast((@as(u16, f.r) + b.r) / 2), 890 .g = @intCast((@as(u16, f.g) + b.g) / 2), 891 .b = @intCast((@as(u16, f.b) + b.b) / 2), 892 }; 893 } 894 895 // behind a visible glyph the art color is dimmed to this fraction of its 896 // brightness so light terminal text stays legible on top of it 897 const glyph_bg_brightness = 45; // percent 898 899 // a glyph sitting over the art also gets its foreground pinned to this color 900 // (unless it set its own), so the text stays legible 901 const glyph_fg_over_art = Grid.Color{ .r = 235, .g = 235, .b = 235 }; 902 903 fn dimColor(color: ?Grid.Color) ?Grid.Color { 904 const c = color orelse return null; 905 return .{ 906 .r = @intCast(@as(u16, c.r) * glyph_bg_brightness / 100), 907 .g = @intCast(@as(u16, c.g) * glyph_bg_brightness / 100), 908 .b = @intCast(@as(u16, c.b) * glyph_bg_brightness / 100), 909 }; 910 } 911 912 // composites ANSI art behind a foreground grid 913 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 914 art.clearGrid(); 915 try art.build(allocator, .{ 916 .min_size = .{ .width = null, .height = null }, 917 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 918 }, root_focus); 919 920 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 921 errdefer out.deinit(); 922 923 if (art.getGrid()) |art_grid| { 924 const anchor_x = switch (anchor) { 925 .top_left => 0, 926 .top_right => foreground.size.width -| art_grid.size.width, 927 }; 928 for (0..art_grid.size.height) |y| { 929 for (0..art_grid.size.width) |x| { 930 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 931 if (src.rune == null) continue; 932 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 933 const dst = &out.cells.items[idx]; 934 if (cellIsBlank(dst.*)) { 935 dst.* = src; 936 } else if (dst.style.bg == null) { 937 dst.style.bg = dimColor(artCellColor(src.style)); 938 if (dst.style.fg == null) dst.style.fg = glyph_fg_over_art; 939 } 940 } 941 } 942 } 943 return out; 944 } 945 }; 946