╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1302 1303 pub fn getGrid(self: AnsiArt) ?Grid { 1304 return self.grid; 1305 } 1306 1307 pub fn getFocus(self: *AnsiArt) *Focus { 1308 return &self.focus; 1309 } 1310 1311 fn applySgr(style: *Grid.Style, params: []const u8) void { 1312 var nums: [16]u32 = undefined; 1313 var n: usize = 0; 1314 var it = std.mem.splitScalar(u8, params, ';'); 1315 while (it.next()) |tok| { 1316 if (n >= nums.len) break; 1317 // an empty parameter (e.g. bare "\x1b[m") means 0 1318 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1319 n += 1; 1320 } 1321 if (n == 0) { 1322 style.* = .{}; 1323 return; 1324 } 1325 var i: usize = 0; 1326 while (i < n) : (i += 1) { 1327 switch (nums[i]) { 1328 0 => style.* = .{}, 1329 7 => style.inverted = true, 1330 27 => style.inverted = false, 1331 39 => style.fg = null, 1332 49 => style.bg = null, 1333 38, 48 => { 1334 // truecolor form: 38;2;r;g;b — anything else is ignored 1335 if (i + 4 < n and nums[i + 1] == 2) { 1336 const c = Grid.Color{ 1337 .r = @truncate(nums[i + 2]), 1338 .g = @truncate(nums[i + 3]), 1339 .b = @truncate(nums[i + 4]), 1340 }; 1341 if (nums[i] == 38) style.fg = c else style.bg = c; 1342 i += 4; 1343 } 1344 }, 1345 else => {}, 1346 } 1347 } 1348 } 1349 }; 1350 1351 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1352 pub const AnsiBackground = struct { 1353 grid: ?Grid, 1354 child: *Widget, 1355 art: AnsiArt, 1356 session: *Session, 1357 1358 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1359 var cw = child_widget; 1360 const child = allocator.create(Widget) catch |e| { 1361 cw.deinit(allocator); 1362 return e; 1363 }; 1364 child.* = cw; 1365 return .{ .grid = null, .child = child, .art = AnsiArt.init(art_content), .session = session }; 1366 } 1367 1368 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1369 if (self.grid) |*grid| grid.deinit(); 1370 self.art.deinit(allocator); 1371 self.child.deinit(allocator); 1372 allocator.destroy(self.child); 1373 } 1374 1375 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1376 self.clearGrid(); 1377 1378 // make the wrapped page fill the whole available area 1379 try self.child.build(allocator, .{ 1380 .min_size = .{ 1381 .width = constraint.max_size.width orelse constraint.min_size.width, 1382 .height = constraint.max_size.height orelse constraint.min_size.height, 1383 }, 1384 .max_size = constraint.max_size, 1385 }, root_focus); 1386 1387 if (self.session.data.enable_ansi) { 1388 if (self.child.getGrid()) |fg| { 1389 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1390 } 1391 } 1392 } 1393 1394 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1395 try self.child.input(allocator, key, root_focus); 1396 } 1397 1398 pub fn clearGrid(self: *AnsiBackground) void { 1399 if (self.grid) |*grid| { 1400 grid.deinit(); 1401 self.grid = null; 1402 } 1403 self.child.clearGrid(); 1404 self.art.clearGrid(); 1405 } 1406 1407 pub fn getGrid(self: AnsiBackground) ?Grid { 1408 return self.grid orelse self.child.getGrid(); 1409 } 1410 1411 pub fn getFocus(self: *AnsiBackground) *Focus { 1412 return self.child.getFocus(); 1413 } 1414 1415 // which top corner the art hugs. 1416 const ArtAnchor = enum { top_left, top_right }; 1417 1418 // a foreground cell counts as blank if it's empty or a bare space without styling 1419 fn cellIsBlank(cell: Grid.Cell) bool { 1420 if (cell.rune) |rune| { 1421 return std.mem.eql(u8, rune, " ") and 1422 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1423 } 1424 return true; 1425 } 1426 1427 // a single representative color for an art cell 1428 fn artCellColor(style: Grid.Style) ?Grid.Color { 1429 const f = style.fg orelse return style.bg; 1430 const b = style.bg orelse return f; 1431 return .{ 1432 .r = @intCast((@as(u16, f.r) + b.r) / 2), 1433 .g = @intCast((@as(u16, f.g) + b.g) / 2), 1434 .b = @intCast((@as(u16, f.b) + b.b) / 2), 1435 }; 1436 } 1437 1438 // behind a visible glyph the art color is dimmed to this fraction of its 1439 // brightness so light terminal text stays legible on top of it 1440 const glyph_bg_brightness = 45; // percent 1441 1442 // a glyph sitting over the art also gets its foreground pinned to this color 1443 // (unless it set its own), so the text stays legible 1444 const glyph_fg_over_art = Grid.Color{ .r = 235, .g = 235, .b = 235 }; 1445 1446 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1447 const c = color orelse return null; 1448 return .{ 1449 .r = @intCast(@as(u16, c.r) * glyph_bg_brightness / 100), 1450 .g = @intCast(@as(u16, c.g) * glyph_bg_brightness / 100), 1451 .b = @intCast(@as(u16, c.b) * glyph_bg_brightness / 100), 1452 }; 1453 } 1454 1455 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1456 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1457 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1458 // snap such channels clear of that range so the backdrop doesn't blink or 1459 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1460 // actually render truecolor. 1461 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1462 const c = color orelse return null; 1463 const snap = struct { 1464 fn f(v: u8) u8 { 1465 return if (v >= 1 and v <= 9) 10 else v; 1466 } 1467 }.f; 1468 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1469 } 1470 1471 // composites ANSI art behind a foreground grid 1472 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1473 art.clearGrid(); 1474 try art.build(allocator, .{ 1475 .min_size = .{ .width = null, .height = null }, 1476 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1477 }, root_focus); 1478 1479 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1480 errdefer out.deinit(); 1481 1482 if (art.getGrid()) |art_grid| { 1483 const anchor_x = switch (anchor) { 1484 .top_left => 0, 1485 .top_right => foreground.size.width -| art_grid.size.width, 1486 }; 1487 for (0..art_grid.size.height) |y| { 1488 for (0..art_grid.size.width) |x| { 1489 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1490 if (src.rune == null) continue; 1491 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1492 const dst = &out.cells.items[idx]; 1493 if (cellIsBlank(dst.*)) { 1494 dst.* = src; 1495 dst.style.fg = sgrSafe(dst.style.fg); 1496 dst.style.bg = sgrSafe(dst.style.bg); 1497 } else if (dst.style.bg == null) { 1498 dst.style.bg = sgrSafe(dimColor(artCellColor(src.style))); 1499 if (dst.style.fg == null) dst.style.fg = glyph_fg_over_art; 1500 } 1501 } 1502 } 1503 } 1504 return out; 1505 } 1506 }; 1507