╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1174 for (r.items[0..n], 0..) |cell, x| { 1175 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1176 } 1177 } 1178 self.grid = grid; 1179 } 1180 1181 pub fn input(self: *AnsiArt, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1182 _ = self; 1183 _ = allocator; 1184 _ = key; 1185 _ = root_focus; 1186 } 1187 1188 pub fn clearGrid(self: *AnsiArt) void { 1189 if (self.grid) |*grid| { 1190 grid.deinit(); 1191 self.grid = null; 1192 } 1193 } 1194 1195 pub fn getGrid(self: AnsiArt) ?Grid { 1196 return self.grid; 1197 } 1198 1199 pub fn getFocus(self: *AnsiArt) *Focus { 1200 return &self.focus; 1201 } 1202 1203 fn applySgr(style: *Grid.Style, params: []const u8) void { 1204 var nums: [16]u32 = undefined; 1205 var n: usize = 0; 1206 var it = std.mem.splitScalar(u8, params, ';'); 1207 while (it.next()) |tok| { 1208 if (n >= nums.len) break; 1209 // an empty parameter (e.g. bare "\x1b[m") means 0 1210 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1211 n += 1; 1212 } 1213 if (n == 0) { 1214 style.* = .{}; 1215 return; 1216 } 1217 var i: usize = 0; 1218 while (i < n) : (i += 1) { 1219 switch (nums[i]) { 1220 0 => style.* = .{}, 1221 7 => style.inverted = true, 1222 27 => style.inverted = false, 1223 39 => style.fg = null, 1224 49 => style.bg = null, 1225 38, 48 => { 1226 // truecolor form: 38;2;r;g;b — anything else is ignored 1227 if (i + 4 < n and nums[i + 1] == 2) { 1228 const c = Grid.Color{ 1229 .r = @truncate(nums[i + 2]), 1230 .g = @truncate(nums[i + 3]), 1231 .b = @truncate(nums[i + 4]), 1232 }; 1233 if (nums[i] == 38) style.fg = c else style.bg = c; 1234 i += 4; 1235 } 1236 }, 1237 else => {}, 1238 } 1239 } 1240 } 1241 }; 1242 1243 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1244 pub const AnsiBackground = struct { 1245 grid: ?Grid, 1246 child: *Widget, 1247 art: AnsiArt, 1248 session: *Session, 1249 1250 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1251 var cw = child_widget; 1252 const child = allocator.create(Widget) catch |e| { 1253 cw.deinit(allocator); 1254 return e; 1255 }; 1256 child.* = cw; 1257 return .{ .grid = null, .child = child, .art = AnsiArt.init(art_content), .session = session }; 1258 } 1259 1260 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1261 if (self.grid) |*grid| grid.deinit(); 1262 self.art.deinit(allocator); 1263 self.child.deinit(allocator); 1264 allocator.destroy(self.child); 1265 } 1266 1267 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1268 self.clearGrid(); 1269 1270 // make the wrapped page fill the whole available area 1271 try self.child.build(allocator, .{ 1272 .min_size = .{ 1273 .width = constraint.max_size.width orelse constraint.min_size.width, 1274 .height = constraint.max_size.height orelse constraint.min_size.height, 1275 }, 1276 .max_size = constraint.max_size, 1277 }, root_focus); 1278 1279 if (self.session.data.enable_ansi) { 1280 if (self.child.getGrid()) |fg| { 1281 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1282 } 1283 } 1284 } 1285 1286 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1287 try self.child.input(allocator, key, root_focus); 1288 } 1289 1290 pub fn clearGrid(self: *AnsiBackground) void { 1291 if (self.grid) |*grid| { 1292 grid.deinit(); 1293 self.grid = null; 1294 } 1295 self.child.clearGrid(); 1296 self.art.clearGrid(); 1297 } 1298 1299 pub fn getGrid(self: AnsiBackground) ?Grid { 1300 return self.grid orelse self.child.getGrid(); 1301 } 1302 1303 pub fn getFocus(self: *AnsiBackground) *Focus { 1304 return self.child.getFocus(); 1305 } 1306 1307 // which top corner the art hugs. 1308 const ArtAnchor = enum { top_left, top_right }; 1309 1310 // a foreground cell counts as blank if it's empty or a bare space without styling 1311 fn cellIsBlank(cell: Grid.Cell) bool { 1312 if (cell.rune) |rune| { 1313 return std.mem.eql(u8, rune, " ") and 1314 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1315 } 1316 return true; 1317 } 1318 1319 // a single representative color for an art cell 1320 fn artCellColor(style: Grid.Style) ?Grid.Color { 1321 const f = style.fg orelse return style.bg; 1322 const b = style.bg orelse return f; 1323 return .{ 1324 .r = @intCast((@as(u16, f.r) + b.r) / 2), 1325 .g = @intCast((@as(u16, f.g) + b.g) / 2), 1326 .b = @intCast((@as(u16, f.b) + b.b) / 2), 1327 }; 1328 } 1329 1330 // behind a visible glyph the art color is dimmed to this fraction of its 1331 // brightness so light terminal text stays legible on top of it 1332 const glyph_bg_brightness = 45; // percent 1333 1334 // a glyph sitting over the art also gets its foreground pinned to this color 1335 // (unless it set its own), so the text stays legible 1336 const glyph_fg_over_art = Grid.Color{ .r = 235, .g = 235, .b = 235 }; 1337 1338 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1339 const c = color orelse return null; 1340 return .{ 1341 .r = @intCast(@as(u16, c.r) * glyph_bg_brightness / 100), 1342 .g = @intCast(@as(u16, c.g) * glyph_bg_brightness / 100), 1343 .b = @intCast(@as(u16, c.b) * glyph_bg_brightness / 100), 1344 }; 1345 } 1346 1347 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1348 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1349 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1350 // snap such channels clear of that range so the backdrop doesn't blink or 1351 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1352 // actually render truecolor. 1353 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1354 const c = color orelse return null; 1355 const snap = struct { 1356 fn f(v: u8) u8 { 1357 return if (v >= 1 and v <= 9) 10 else v; 1358 } 1359 }.f; 1360 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1361 } 1362 1363 // composites ANSI art behind a foreground grid 1364 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1365 art.clearGrid(); 1366 try art.build(allocator, .{ 1367 .min_size = .{ .width = null, .height = null }, 1368 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1369 }, root_focus); 1370 1371 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1372 errdefer out.deinit(); 1373 1374 if (art.getGrid()) |art_grid| { 1375 const anchor_x = switch (anchor) { 1376 .top_left => 0, 1377 .top_right => foreground.size.width -| art_grid.size.width, 1378 }; 1379 for (0..art_grid.size.height) |y| { 1380 for (0..art_grid.size.width) |x| { 1381 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1382 if (src.rune == null) continue; 1383 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1384 const dst = &out.cells.items[idx]; 1385 if (cellIsBlank(dst.*)) { 1386 dst.* = src; 1387 dst.style.fg = sgrSafe(dst.style.fg); 1388 dst.style.bg = sgrSafe(dst.style.bg); 1389 } else if (dst.style.bg == null) { 1390 dst.style.bg = sgrSafe(dimColor(artCellColor(src.style))); 1391 if (dst.style.fg == null) dst.style.fg = glyph_fg_over_art; 1392 } 1393 } 1394 } 1395 } 1396 return out; 1397 } 1398 }; 1399