╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
760 const transparent = end - i == 1 and rune[0] == ' ' and 761 style.fg == null and style.bg == null and !style.inverted; 762 try row.append(allocator, .{ 763 .rune = if (transparent) null else rune, 764 .style = style, 765 }); 766 i = end; 767 } 768 } 769 // a trailing row with content but no closing newline 770 if (row.items.len > 0) { 771 width = @max(width, row.items.len); 772 try rows.append(allocator, row); 773 } else { 774 row.deinit(allocator); 775 } 776 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 777 778 const height = rows.items.len; 779 if (width == 0 or height == 0) return; 780 781 const clamped_w = @min(width, constraint.max_size.width orelse width); 782 const clamped_h = @min(height, constraint.max_size.height orelse height); 783 784 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 785 errdefer grid.deinit(); 786 for (rows.items[0..clamped_h], 0..) |r, y| { 787 const n = @min(r.items.len, clamped_w); 788 for (r.items[0..n], 0..) |cell, x| { 789 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 790 } 791 } 792 self.grid = grid; 793 } 794 795 pub fn input(self: *AnsiArt, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 796 _ = self; 797 _ = allocator; 798 _ = key; 799 _ = root_focus; 800 } 801 802 pub fn clearGrid(self: *AnsiArt) void { 803 if (self.grid) |*grid| { 804 grid.deinit(); 805 self.grid = null; 806 } 807 } 808 809 pub fn getGrid(self: AnsiArt) ?Grid { 810 return self.grid; 811 } 812 813 pub fn getFocus(self: *AnsiArt) *Focus { 814 return &self.focus; 815 } 816 817 fn applySgr(style: *Grid.Style, params: []const u8) void { 818 var nums: [16]u32 = undefined; 819 var n: usize = 0; 820 var it = std.mem.splitScalar(u8, params, ';'); 821 while (it.next()) |tok| { 822 if (n >= nums.len) break; 823 // an empty parameter (e.g. bare "\x1b[m") means 0 824 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 825 n += 1; 826 } 827 if (n == 0) { 828 style.* = .{}; 829 return; 830 } 831 var i: usize = 0; 832 while (i < n) : (i += 1) { 833 switch (nums[i]) { 834 0 => style.* = .{}, 835 7 => style.inverted = true, 836 27 => style.inverted = false, 837 39 => style.fg = null, 838 49 => style.bg = null, 839 38, 48 => { 840 // truecolor form: 38;2;r;g;b — anything else is ignored 841 if (i + 4 < n and nums[i + 1] == 2) { 842 const c = Grid.Color{ 843 .r = @truncate(nums[i + 2]), 844 .g = @truncate(nums[i + 3]), 845 .b = @truncate(nums[i + 4]), 846 }; 847 if (nums[i] == 38) style.fg = c else style.bg = c; 848 i += 4; 849 } 850 }, 851 else => {}, 852 } 853 } 854 } 855 }; 856 857 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 858 pub const AnsiBackground = struct { 859 grid: ?Grid, 860 child: *Widget, 861 art: AnsiArt, 862 session: *Session, 863 864 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 865 var cw = child_widget; 866 const child = allocator.create(Widget) catch |e| { 867 cw.deinit(allocator); 868 return e; 869 }; 870 child.* = cw; 871 return .{ .grid = null, .child = child, .art = AnsiArt.init(art_content), .session = session }; 872 } 873 874 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 875 if (self.grid) |*grid| grid.deinit(); 876 self.art.deinit(allocator); 877 self.child.deinit(allocator); 878 allocator.destroy(self.child); 879 } 880 881 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 882 self.clearGrid(); 883 884 // make the wrapped page fill the whole available area 885 try self.child.build(allocator, .{ 886 .min_size = .{ 887 .width = constraint.max_size.width orelse constraint.min_size.width, 888 .height = constraint.max_size.height orelse constraint.min_size.height, 889 }, 890 .max_size = constraint.max_size, 891 }, root_focus); 892 893 if (self.session.data.enable_ansi) { 894 if (self.child.getGrid()) |fg| { 895 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 896 } 897 } 898 } 899 900 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 901 try self.child.input(allocator, key, root_focus); 902 } 903 904 pub fn clearGrid(self: *AnsiBackground) void { 905 if (self.grid) |*grid| { 906 grid.deinit(); 907 self.grid = null; 908 } 909 self.child.clearGrid(); 910 self.art.clearGrid(); 911 } 912 913 pub fn getGrid(self: AnsiBackground) ?Grid { 914 return self.grid orelse self.child.getGrid(); 915 } 916 917 pub fn getFocus(self: *AnsiBackground) *Focus { 918 return self.child.getFocus(); 919 } 920 921 // which top corner the art hugs. 922 const ArtAnchor = enum { top_left, top_right }; 923 924 // a foreground cell counts as blank if it's empty or a bare space without styling 925 fn cellIsBlank(cell: Grid.Cell) bool { 926 if (cell.rune) |rune| { 927 return std.mem.eql(u8, rune, " ") and 928 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 929 } 930 return true; 931 } 932 933 // a single representative color for an art cell 934 fn artCellColor(style: Grid.Style) ?Grid.Color { 935 const f = style.fg orelse return style.bg; 936 const b = style.bg orelse return f; 937 return .{ 938 .r = @intCast((@as(u16, f.r) + b.r) / 2), 939 .g = @intCast((@as(u16, f.g) + b.g) / 2), 940 .b = @intCast((@as(u16, f.b) + b.b) / 2), 941 }; 942 } 943 944 // behind a visible glyph the art color is dimmed to this fraction of its 945 // brightness so light terminal text stays legible on top of it 946 const glyph_bg_brightness = 45; // percent 947 948 // a glyph sitting over the art also gets its foreground pinned to this color 949 // (unless it set its own), so the text stays legible 950 const glyph_fg_over_art = Grid.Color{ .r = 235, .g = 235, .b = 235 }; 951 952 fn dimColor(color: ?Grid.Color) ?Grid.Color { 953 const c = color orelse return null; 954 return .{ 955 .r = @intCast(@as(u16, c.r) * glyph_bg_brightness / 100), 956 .g = @intCast(@as(u16, c.g) * glyph_bg_brightness / 100), 957 .b = @intCast(@as(u16, c.b) * glyph_bg_brightness / 100), 958 }; 959 } 960 961 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 962 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 963 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 964 // snap such channels clear of that range so the backdrop doesn't blink or 965 // hide text there; the ≤9/255 shift is imperceptible on terminals that 966 // actually render truecolor. 967 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 968 const c = color orelse return null; 969 const snap = struct { 970 fn f(v: u8) u8 { 971 return if (v >= 1 and v <= 9) 10 else v; 972 } 973 }.f; 974 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 975 } 976 977 // composites ANSI art behind a foreground grid 978 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 979 art.clearGrid(); 980 try art.build(allocator, .{ 981 .min_size = .{ .width = null, .height = null }, 982 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 983 }, root_focus); 984 985 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 986 errdefer out.deinit(); 987 988 if (art.getGrid()) |art_grid| { 989 const anchor_x = switch (anchor) { 990 .top_left => 0, 991 .top_right => foreground.size.width -| art_grid.size.width, 992 }; 993 for (0..art_grid.size.height) |y| { 994 for (0..art_grid.size.width) |x| { 995 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 996 if (src.rune == null) continue; 997 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 998 const dst = &out.cells.items[idx]; 999 if (cellIsBlank(dst.*)) { 1000 dst.* = src; 1001 dst.style.fg = sgrSafe(dst.style.fg); 1002 dst.style.bg = sgrSafe(dst.style.bg); 1003 } else if (dst.style.bg == null) { 1004 dst.style.bg = sgrSafe(dimColor(artCellColor(src.style))); 1005 if (dst.style.fg == null) dst.style.fg = glyph_fg_over_art; 1006 } 1007 } 1008 } 1009 } 1010 return out; 1011 } 1012 }; 1013