╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1385 if (self.grid) |*grid| grid.deinit(); 1386 self.art.deinit(allocator); 1387 self.child.deinit(allocator); 1388 allocator.destroy(self.child); 1389 } 1390 1391 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1392 self.clearGrid(); 1393 1394 // make the wrapped page fill the whole available area 1395 try self.child.build(allocator, .{ 1396 .min_size = .{ 1397 .width = constraint.max_size.width orelse constraint.min_size.width, 1398 .height = constraint.max_size.height orelse constraint.min_size.height, 1399 }, 1400 .max_size = constraint.max_size, 1401 }, root_focus); 1402 1403 if (self.session.data.enable_ansi) { 1404 if (self.child.getGrid()) |fg| { 1405 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1406 } 1407 } 1408 } 1409 1410 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1411 try self.child.input(allocator, key, root_focus); 1412 } 1413 1414 pub fn clearGrid(self: *AnsiBackground) void { 1415 if (self.grid) |*grid| { 1416 grid.deinit(); 1417 self.grid = null; 1418 } 1419 self.child.clearGrid(); 1420 self.art.clearGrid(); 1421 } 1422 1423 pub fn getGrid(self: AnsiBackground) ?Grid { 1424 return self.grid orelse self.child.getGrid(); 1425 } 1426 1427 pub fn getFocus(self: *AnsiBackground) *Focus { 1428 return self.child.getFocus(); 1429 } 1430 1431 // which top corner the art hugs. 1432 const ArtAnchor = enum { top_left, top_right }; 1433 1434 // a foreground cell counts as blank if it's empty or a bare space without styling 1435 fn cellIsBlank(cell: Grid.Cell) bool { 1436 if (cell.rune) |rune| { 1437 return std.mem.eql(u8, rune, " ") and 1438 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1439 } 1440 return true; 1441 } 1442 1443 // a single representative color for an art cell 1444 fn artCellColor(style: Grid.Style) ?Grid.Color { 1445 const f = style.fg orelse return style.bg; 1446 const b = style.bg orelse return f; 1447 return .{ 1448 .r = @intCast((@as(u16, f.r) + b.r) / 2), 1449 .g = @intCast((@as(u16, f.g) + b.g) / 2), 1450 .b = @intCast((@as(u16, f.b) + b.b) / 2), 1451 }; 1452 } 1453 1454 // behind a visible glyph the art color is dimmed to this fraction of its 1455 // brightness so light terminal text stays legible on top of it 1456 const glyph_bg_brightness = 45; // percent 1457 1458 // a glyph sitting over the art also gets its foreground pinned to this color 1459 // (unless it set its own), so the text stays legible 1460 const glyph_fg_over_art = Grid.Color{ .r = 235, .g = 235, .b = 235 }; 1461 1462 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1463 const c = color orelse return null; 1464 return .{ 1465 .r = @intCast(@as(u16, c.r) * glyph_bg_brightness / 100), 1466 .g = @intCast(@as(u16, c.g) * glyph_bg_brightness / 100), 1467 .b = @intCast(@as(u16, c.b) * glyph_bg_brightness / 100), 1468 }; 1469 } 1470 1471 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1472 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1473 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1474 // snap such channels clear of that range so the backdrop doesn't blink or 1475 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1476 // actually render truecolor. 1477 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1478 const c = color orelse return null; 1479 const snap = struct { 1480 fn f(v: u8) u8 { 1481 return if (v >= 1 and v <= 9) 10 else v; 1482 } 1483 }.f; 1484 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1485 } 1486 1487 // composites ANSI art behind a foreground grid 1488 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1489 art.clearGrid(); 1490 try art.build(allocator, .{ 1491 .min_size = .{ .width = null, .height = null }, 1492 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1493 }, root_focus); 1494 1495 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1496 errdefer out.deinit(); 1497 1498 if (art.getGrid()) |art_grid| { 1499 const anchor_x = switch (anchor) { 1500 .top_left => 0, 1501 .top_right => foreground.size.width -| art_grid.size.width, 1502 }; 1503 for (0..art_grid.size.height) |y| { 1504 for (0..art_grid.size.width) |x| { 1505 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1506 if (src.rune == null) continue; 1507 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1508 const dst = &out.cells.items[idx]; 1509 if (cellIsBlank(dst.*)) { 1510 dst.* = src; 1511 dst.style.fg = sgrSafe(dst.style.fg); 1512 dst.style.bg = sgrSafe(dst.style.bg); 1513 } else if (dst.style.bg == null) { 1514 dst.style.bg = sgrSafe(dimColor(artCellColor(src.style))); 1515 if (dst.style.fg == null) dst.style.fg = glyph_fg_over_art; 1516 } 1517 } 1518 } 1519 } 1520 return out; 1521 } 1522 }; 1523