╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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