╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
3097 3098 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *Session) !AnsiBackground { 3099 var cw = child_widget; 3100 const child = allocator.create(Widget) catch |e| { 3101 cw.deinit(allocator); 3102 return e; 3103 }; 3104 child.* = cw; 3105 errdefer { 3106 child.deinit(allocator); 3107 allocator.destroy(child); 3108 } 3109 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 3110 } 3111 3112 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 3113 if (self.grid) |*grid| grid.deinit(); 3114 self.art.deinit(allocator); 3115 self.child.deinit(allocator); 3116 allocator.destroy(self.child); 3117 } 3118 3119 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 3120 self.clearGrid(); 3121 3122 // make the wrapped page fill the whole available area 3123 try self.child.build(allocator, .{ 3124 .min_size = .{ 3125 .width = constraint.max_size.width orelse constraint.min_size.width, 3126 .height = constraint.max_size.height orelse constraint.min_size.height, 3127 }, 3128 .max_size = constraint.max_size, 3129 }, root_focus); 3130 3131 if (self.session.data.enable_ansi) { 3132 self.art.content = self.session.data.ansi_art; 3133 if (self.child.getGrid()) |fg| { 3134 self.grid = try artBehind(allocator, fg, &self.art, .top_right, self.session.is_terminal, root_focus); 3135 } 3136 } 3137 } 3138 3139 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 3140 try self.child.input(allocator, key, root_focus); 3141 } 3142 3143 pub fn clearGrid(self: *AnsiBackground) void { 3144 if (self.grid) |*grid| { 3145 grid.deinit(); 3146 self.grid = null; 3147 } 3148 self.child.clearGrid(); 3149 self.art.clearGrid(); 3150 } 3151 3152 pub fn getGrid(self: AnsiBackground) ?Grid { 3153 return self.grid orelse self.child.getGrid(); 3154 } 3155 3156 pub fn getFocus(self: *AnsiBackground) *Focus { 3157 return self.child.getFocus(); 3158 } 3159 3160 // which top corner the art hugs. 3161 const ArtAnchor = enum { top_left, top_right }; 3162 3163 // a foreground cell counts as blank if it's empty or a bare space without styling 3164 fn cellIsBlank(cell: Grid.Cell) bool { 3165 if (cell.rune) |rune| { 3166 return std.mem.eql(u8, rune, " ") and 3167 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 3168 } 3169 return true; 3170 } 3171 3172 // the art is dimmed to this fraction of its brightness so foreground text 3173 // stays legible over it 3174 const art_brightness = 35; // percent 3175 3176 fn dimColor(color: ?Grid.Color) ?Grid.Color { 3177 const c = color orelse return null; 3178 return .{ 3179 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 3180 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 3181 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 3182 }; 3183 } 3184 3185 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 3186 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 3187 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 3188 // snap such channels clear of that range so the backdrop doesn't blink or 3189 // hide text there; the ≤9/255 shift is imperceptible on terminals that 3190 // actually render truecolor. 3191 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 3192 const c = color orelse return null; 3193 const snap = struct { 3194 fn f(v: u8) u8 { 3195 return if (v >= 1 and v <= 9) 10 else v; 3196 } 3197 }.f; 3198 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 3199 } 3200 3201 // composites ANSI art behind a foreground grid 3202 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, is_terminal: bool, root_focus: *Focus) !Grid { 3203 art.clearGrid(); 3204 try art.build(allocator, .{ 3205 .min_size = .{ .width = null, .height = null }, 3206 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 3207 }, root_focus); 3208 3209 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 3210 errdefer out.deinit(); 3211 3212 if (art.getGrid()) |art_grid| { 3213 const anchor_x = switch (anchor) { 3214 .top_left => 0, 3215 .top_right => foreground.size.width -| art_grid.size.width, 3216 }; 3217 for (0..art_grid.size.height) |y| { 3218 for (0..art_grid.size.width) |x| { 3219 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 3220 if (src.rune == null) continue; 3221 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 3222 const dst = &out.cells.items[idx]; 3223 // composite the dimmed art behind every cell so it shows 3224 // through the whole UI: a blank cell becomes the art, while a 3225 // cell with a glyph keeps the glyph but takes the dimmed art as 3226 // its background (replacing whatever background it had, which 3227 // would otherwise obscure the art). 3228 if (cellIsBlank(dst.*)) { 3229 dst.* = src; 3230 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 3231 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 3232 } else { 3233 const background_maybe = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 3234 dst.style.bg = background_maybe; 3235 // change the color of text overlaying ANSI art to ensure 3236 // it is readable. don't do this in the web UI, since we 3237 // have direct control of the theme there (also, inner 3238 // scroll panes allow text to move without a re-render 3239 // by the TUI, which would break this). also don't do this 3240 // if the text is explicitly styled. 3241 if (is_terminal and dst.style.fg == null) { 3242 if (background_maybe) |background| { 3243 // use near-black and near-white, which is easier on the eyes 3244 // while retaining high contrast 3245 const luminance = (@as(u32, background.r) * 299 + 3246 @as(u32, background.g) * 587 + 3247 @as(u32, background.b) * 114) / 1000; 3248 dst.style.fg = if (luminance >= 128) 3249 .{ .r = 16, .g = 16, .b = 16 } 3250 else 3251 .{ .r = 240, .g = 240, .b = 240 }; 3252 } 3253 } 3254 } 3255 } 3256 } 3257 } 3258 return out; 3259 } 3260 }; 3261