╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1270 1271 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 1272 return .{ 1273 .focus = try Focus.create(allocator, .container), 1274 .grid = null, 1275 .session = session, 1276 .arena = std.heap.ArenaAllocator.init(allocator), 1277 }; 1278 } 1279 1280 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1281 self.focus.destroy(allocator); 1282 if (self.grid) |*grid| grid.deinit(); 1283 self.arena.deinit(); 1284 } 1285 1286 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1287 _ = root_focus; 1288 self.clearGrid(); 1289 const width = constraint.max_size.width orelse return; 1290 if (width == 0) return; 1291 1292 _ = self.arena.reset(.retain_capacity); 1293 const aa = self.arena.allocator(); 1294 const path = self.session.data.current_page.urlAlloc(&self.arena) catch return; 1295 const text = if (self.session.web_port) |port| 1296 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1297 else 1298 path; 1299 1300 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1301 errdefer grid.deinit(); 1302 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1303 var i: usize = 0; 1304 while (utf8.nextCodepointSlice()) |ch| { 1305 if (i == width) break; 1306 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1307 i += 1; 1308 } 1309 self.grid = grid; 1310 } 1311 1312 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1313 _ = self; 1314 _ = allocator; 1315 _ = key; 1316 _ = root_focus; 1317 } 1318 1319 pub fn clearGrid(self: *Footer) void { 1320 if (self.grid) |*grid| { 1321 grid.deinit(); 1322 self.grid = null; 1323 } 1324 } 1325 1326 pub fn getGrid(self: Footer) ?Grid { 1327 return self.grid; 1328 } 1329 1330 pub fn getFocus(self: *Footer) *Focus { 1331 return self.focus; 1332 } 1333 }; 1334 1335 // a single-child wrapper that builds the child at its natural size and 1336 // positions its grid in the middle of the area granted by the parent 1337 pub const Center = struct { 1338 focus: *Focus, 1339 grid: ?Grid, 1340 child: *Widget, 1341 direction: Direction, 1342 1343 pub const Direction = enum { both, horiz, vert }; 1344 1345 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, direction: Direction) !Center { 1346 const child = try allocator.create(Widget); 1347 errdefer allocator.destroy(child); 1348 child.* = child_widget; 1349 return .{ 1350 .focus = try Focus.create(allocator, .container), 1351 .grid = null, 1352 .child = child, 1353 .direction = direction, 1354 }; 1355 } 1356 1357 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1358 self.focus.destroy(allocator); 1359 if (self.grid) |*grid| { 1360 grid.deinit(); 1361 self.grid = null; 1362 } 1363 self.child.deinit(allocator); 1364 allocator.destroy(self.child); 1365 } 1366 1367 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1368 self.clearGrid(); 1369 self.getFocus().clear(); 1370 1371 // build the child without forcing it to fill min_size; it sizes 1372 // itself to its content within the available max. 1373 try self.child.build(allocator, .{ 1374 .min_size = .{ .width = null, .height = null }, 1375 .max_size = constraint.max_size, 1376 }, root_focus); 1377 1378 const child_grid = self.child.getGrid() orelse return; 1379 1380 // prefer max when given; otherwise grow to max(min, child) so that 1381 // when only a min is set (e.g. the wasm path passes viewport rows 1382 // as min_height with no max), we can still vertically center while 1383 // letting taller content extend past the min. 1384 const width = if (constraint.max_size.width) |w| w else if (constraint.min_size.width) |min_w| @max(min_w, child_grid.size.width) else child_grid.size.width; 1385 const height = if (constraint.max_size.height) |h| h else if (constraint.min_size.height) |min_h| @max(min_h, child_grid.size.height) else child_grid.size.height; 1386 if (width == 0 or height == 0) return; 1387 1388 const offset_x: usize = if (self.direction == .horiz or self.direction == .both) 1389 (width -| child_grid.size.width) / 2 1390 else 1391 0; 1392 const offset_y: usize = if (self.direction == .vert or self.direction == .both) 1393 (height -| child_grid.size.height) / 2 1394 else 1395 0; 1396 1397 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1398 errdefer grid.deinit(); 1399 try grid.drawGrid(child_grid, offset_x, offset_y); 1400 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1401 self.getFocus().child_id = self.child.getFocus().id; 1402 1403 self.grid = grid; 1404 } 1405 1406 pub fn input(self: *Center, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1407 try self.child.input(allocator, key, root_focus); 1408 } 1409 1410 pub fn clearGrid(self: *Center) void { 1411 if (self.grid) |*grid| { 1412 grid.deinit(); 1413 self.grid = null; 1414 } 1415 self.child.clearGrid(); 1416 } 1417 1418 pub fn getGrid(self: Center) ?Grid { 1419 return self.grid; 1420 } 1421 1422 pub fn getFocus(self: *Center) *Focus { 1423 return self.focus; 1424 } 1425 }; 1426 1427 // renders truecolor ANSI art 1428 pub const AnsiArt = struct { 1429 focus: *Focus, 1430 grid: ?Grid, 1431 content: []const u8, 1432 1433 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1434 return .{ 1435 .focus = try Focus.create(allocator, .container), 1436 .grid = null, 1437 .content = content, 1438 }; 1439 } 1440 1441 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1442 self.focus.destroy(allocator); 1443 if (self.grid) |*grid| { 1444 grid.deinit(); 1445 self.grid = null; 1446 } 1447 } 1448 1449 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1450 _ = root_focus; 1451 self.clearGrid(); 1452 1453 // parse into rows of cells, then copy into a rectangular grid 1454 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1455 defer { 1456 for (rows.items) |*row| row.deinit(allocator); 1457 rows.deinit(allocator); 1458 } 1459 var row: std.ArrayList(Grid.Cell) = .empty; 1460 errdefer row.deinit(allocator); 1461 1462 var style: Grid.Style = .{}; 1463 var width: usize = 0; 1464 const content = self.content; 1465 var i: usize = 0; 1466 while (i < content.len) { 1467 const byte = content[i]; 1468 if (byte == '\n') { 1469 width = @max(width, row.items.len); 1470 try rows.append(allocator, row); 1471 row = .empty; 1472 i += 1; 1473 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1474 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1475 var j = i + 2; 1476 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1477 if (j >= content.len) { 1478 i = content.len; // malformed trailing escape; stop 1479 } else { 1480 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1481 i = j + 1; 1482 } 1483 } else { 1484 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1485 const end = @min(content.len, i + len); 1486 const rune = content[i..end]; 1487 const transparent = end - i == 1 and rune[0] == ' ' and 1488 style.fg == null and style.bg == null and !style.inverted; 1489 try row.append(allocator, .{ 1490 .rune = if (transparent) null else rune, 1491 .style = style, 1492 }); 1493 i = end; 1494 } 1495 } 1496 // a trailing row with content but no closing newline 1497 if (row.items.len > 0) { 1498 width = @max(width, row.items.len); 1499 try rows.append(allocator, row); 1500 } else { 1501 row.deinit(allocator); 1502 } 1503 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1504 1505 const height = rows.items.len; 1506 if (width == 0 or height == 0) return; 1507 1508 const clamped_w = @min(width, constraint.max_size.width orelse width); 1509 const clamped_h = @min(height, constraint.max_size.height orelse height); 1510 1511 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1512 errdefer grid.deinit(); 1513 for (rows.items[0..clamped_h], 0..) |r, y| { 1514 const n = @min(r.items.len, clamped_w); 1515 for (r.items[0..n], 0..) |cell, x| { 1516 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1517 } 1518 } 1519 self.grid = grid; 1520 } 1521 1522 pub fn input(self: *AnsiArt, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1523 _ = self; 1524 _ = allocator; 1525 _ = key; 1526 _ = root_focus; 1527 } 1528 1529 pub fn clearGrid(self: *AnsiArt) void { 1530 if (self.grid) |*grid| { 1531 grid.deinit(); 1532 self.grid = null; 1533 } 1534 } 1535 1536 pub fn getGrid(self: AnsiArt) ?Grid { 1537 return self.grid; 1538 } 1539 1540 pub fn getFocus(self: *AnsiArt) *Focus { 1541 return self.focus; 1542 } 1543 1544 fn applySgr(style: *Grid.Style, params: []const u8) void { 1545 var nums: [16]u32 = undefined; 1546 var n: usize = 0; 1547 var it = std.mem.splitScalar(u8, params, ';'); 1548 while (it.next()) |tok| { 1549 if (n >= nums.len) break; 1550 // an empty parameter (e.g. bare "\x1b[m") means 0 1551 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1552 n += 1; 1553 } 1554 if (n == 0) { 1555 style.* = .{}; 1556 return; 1557 } 1558 var i: usize = 0; 1559 while (i < n) : (i += 1) { 1560 switch (nums[i]) { 1561 0 => style.* = .{}, 1562 7 => style.inverted = true, 1563 27 => style.inverted = false, 1564 39 => style.fg = null, 1565 49 => style.bg = null, 1566 38, 48 => { 1567 // truecolor form: 38;2;r;g;b — anything else is ignored 1568 if (i + 4 < n and nums[i + 1] == 2) { 1569 const c = Grid.Color{ 1570 .r = @truncate(nums[i + 2]), 1571 .g = @truncate(nums[i + 3]), 1572 .b = @truncate(nums[i + 4]), 1573 }; 1574 if (nums[i] == 38) style.fg = c else style.bg = c; 1575 i += 4; 1576 } 1577 }, 1578 else => {}, 1579 } 1580 } 1581 } 1582 }; 1583 1584 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1585 pub const AnsiBackground = struct { 1586 grid: ?Grid, 1587 child: *Widget, 1588 art: AnsiArt, 1589 session: *Session, 1590 1591 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1592 var cw = child_widget; 1593 const child = allocator.create(Widget) catch |e| { 1594 cw.deinit(allocator); 1595 return e; 1596 }; 1597 child.* = cw; 1598 errdefer { 1599 child.deinit(allocator); 1600 allocator.destroy(child); 1601 } 1602 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 1603 } 1604 1605 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1606 if (self.grid) |*grid| grid.deinit(); 1607 self.art.deinit(allocator); 1608 self.child.deinit(allocator); 1609 allocator.destroy(self.child); 1610 } 1611 1612 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1613 self.clearGrid(); 1614 1615 // make the wrapped page fill the whole available area 1616 try self.child.build(allocator, .{ 1617 .min_size = .{ 1618 .width = constraint.max_size.width orelse constraint.min_size.width, 1619 .height = constraint.max_size.height orelse constraint.min_size.height, 1620 }, 1621 .max_size = constraint.max_size, 1622 }, root_focus); 1623 1624 if (self.session.data.enable_ansi) { 1625 if (self.child.getGrid()) |fg| { 1626 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1627 } 1628 } 1629 } 1630 1631 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1632 try self.child.input(allocator, key, root_focus); 1633 } 1634 1635 pub fn clearGrid(self: *AnsiBackground) void { 1636 if (self.grid) |*grid| { 1637 grid.deinit(); 1638 self.grid = null; 1639 } 1640 self.child.clearGrid(); 1641 self.art.clearGrid(); 1642 } 1643 1644 pub fn getGrid(self: AnsiBackground) ?Grid { 1645 return self.grid orelse self.child.getGrid(); 1646 } 1647 1648 pub fn getFocus(self: *AnsiBackground) *Focus { 1649 return self.child.getFocus(); 1650 } 1651 1652 // which top corner the art hugs. 1653 const ArtAnchor = enum { top_left, top_right }; 1654 1655 // a foreground cell counts as blank if it's empty or a bare space without styling 1656 fn cellIsBlank(cell: Grid.Cell) bool { 1657 if (cell.rune) |rune| { 1658 return std.mem.eql(u8, rune, " ") and 1659 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1660 } 1661 return true; 1662 } 1663 1664 // the art is dimmed to this fraction of its brightness so foreground text 1665 // stays legible over it 1666 const art_brightness = 45; // percent 1667 1668 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1669 const c = color orelse return null; 1670 return .{ 1671 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1672 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1673 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1674 }; 1675 } 1676 1677 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1678 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1679 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1680 // snap such channels clear of that range so the backdrop doesn't blink or 1681 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1682 // actually render truecolor. 1683 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1684 const c = color orelse return null; 1685 const snap = struct { 1686 fn f(v: u8) u8 { 1687 return if (v >= 1 and v <= 9) 10 else v; 1688 } 1689 }.f; 1690 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1691 } 1692 1693 // composites ANSI art behind a foreground grid 1694 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1695 art.clearGrid(); 1696 try art.build(allocator, .{ 1697 .min_size = .{ .width = null, .height = null }, 1698 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1699 }, root_focus); 1700 1701 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1702 errdefer out.deinit(); 1703 1704 if (art.getGrid()) |art_grid| { 1705 const anchor_x = switch (anchor) { 1706 .top_left => 0, 1707 .top_right => foreground.size.width -| art_grid.size.width, 1708 }; 1709 for (0..art_grid.size.height) |y| { 1710 for (0..art_grid.size.width) |x| { 1711 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1712 if (src.rune == null) continue; 1713 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1714 const dst = &out.cells.items[idx]; 1715 // composite the dimmed art behind every cell so it shows 1716 // through the whole UI: a blank cell becomes the art, while a 1717 // cell with a glyph keeps the glyph but takes the dimmed art as 1718 // its background (replacing whatever background it had, which 1719 // would otherwise obscure the art). 1720 if (cellIsBlank(dst.*)) { 1721 dst.* = src; 1722 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 1723 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 1724 } else { 1725 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 1726 } 1727 } 1728 } 1729 } 1730 return out; 1731 } 1732 }; 1733