╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1321 switch (key) { 1322 .mouse => |mouse| { 1323 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 1324 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 1325 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 1326 }, 1327 else => {}, 1328 } 1329 } 1330 1331 pub fn clearGrid(self: *Footer) void { 1332 if (self.grid) |*grid| { 1333 grid.deinit(); 1334 self.grid = null; 1335 } 1336 } 1337 1338 pub fn getGrid(self: Footer) ?Grid { 1339 return self.grid; 1340 } 1341 1342 pub fn getFocus(self: *Footer) *Focus { 1343 return self.focus; 1344 } 1345 }; 1346 1347 // a single-child wrapper that builds the child at its natural size and 1348 // positions its grid in the middle of the area granted by the parent 1349 // a bordered label, indented past the content it precedes 1350 pub const SectionLabel = struct { 1351 box: wgt.Box(Widget), 1352 1353 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 1354 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1355 errdefer box.deinit(allocator); 1356 1357 { 1358 var gap = try wgt.Text.init(allocator, " "); 1359 errdefer gap.deinit(allocator); 1360 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1361 } 1362 1363 { 1364 var tb = try wgt.TextBox.init(allocator, content, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 1365 errdefer tb.deinit(allocator); 1366 tb.getFocus().mode = .all; 1367 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1368 } 1369 1370 box.getFocus().child_id = box.children.keys()[1]; 1371 return .{ .box = box }; 1372 } 1373 1374 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 1375 self.box.deinit(allocator); 1376 } 1377 1378 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1379 try self.box.build(allocator, constraint, root_focus); 1380 } 1381 1382 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1383 _ = self; 1384 _ = allocator; 1385 _ = key; 1386 _ = root_focus; 1387 } 1388 1389 pub fn clearGrid(self: *SectionLabel) void { 1390 self.box.clearGrid(); 1391 } 1392 1393 pub fn getGrid(self: SectionLabel) ?Grid { 1394 return self.box.getGrid(); 1395 } 1396 1397 pub fn getFocus(self: *SectionLabel) *Focus { 1398 return self.box.getFocus(); 1399 } 1400 }; 1401 1402 // a submit button, indented past its form's content. the web form POSTs 1403 // the button's form; terminal hosts detect presses through `buttonId`. 1404 pub const SubmitButton = struct { 1405 box: wgt.Box(Widget), 1406 1407 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 1408 return initLabeled(allocator, "submit"); 1409 } 1410 1411 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 1412 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1413 errdefer box.deinit(allocator); 1414 1415 { 1416 var gap = try wgt.Text.init(allocator, " "); 1417 errdefer gap.deinit(allocator); 1418 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1419 } 1420 1421 { 1422 var button = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 1423 errdefer button.deinit(allocator); 1424 button.getFocus().mode = .all; 1425 // the renderer distinguishes plain clickables from buttons that 1426 // should POST to a server route by this kind. 1427 button.getFocus().kind = .{ .custom = "submit" }; 1428 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1429 } 1430 1431 box.getFocus().child_id = box.children.keys()[1]; 1432 return .{ .box = box }; 1433 } 1434 1435 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 1436 self.box.deinit(allocator); 1437 } 1438 1439 pub fn setLabel(self: *SubmitButton, allocator: std.mem.Allocator, label: []const u8) !void { 1440 const button = &self.box.children.values()[1]; 1441 try button.widget.text_box.setContent(allocator, label); 1442 button.min_size = .{ .width = label.len + 2, .height = null }; 1443 } 1444 1445 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1446 try self.box.build(allocator, constraint, root_focus); 1447 } 1448 1449 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1450 _ = self; 1451 _ = allocator; 1452 _ = key; 1453 _ = root_focus; 1454 } 1455 1456 pub fn clearGrid(self: *SubmitButton) void { 1457 self.box.clearGrid(); 1458 } 1459 1460 pub fn getGrid(self: SubmitButton) ?Grid { 1461 return self.box.getGrid(); 1462 } 1463 1464 pub fn getFocus(self: *SubmitButton) *Focus { 1465 return self.box.getFocus(); 1466 } 1467 1468 // the button's focus id, for click hit-testing 1469 pub fn buttonId(self: *const SubmitButton) usize { 1470 return self.box.children.keys()[1]; 1471 } 1472 }; 1473 1474 // keep `rect`, in a centered widget's space, in view within its scroll 1475 pub fn scrollToCenteredRect(scroll: *wgt.Scroll(Widget), rect: layout.IRect) void { 1476 const center = &scroll.child.center; 1477 const center_grid = center.getGrid() orelse return; 1478 const child_grid = center.child.getGrid() orelse return; 1479 const offset_y: isize = @intCast((center_grid.size.height -| child_grid.size.height) / 2); 1480 scroll.scrollToRect(.{ .x = rect.x, .y = rect.y + offset_y, .size = rect.size }); 1481 } 1482 1483 pub const Center = struct { 1484 focus: *Focus, 1485 grid: ?Grid, 1486 child: *Widget, 1487 1488 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1489 const child = try allocator.create(Widget); 1490 errdefer allocator.destroy(child); 1491 child.* = child_widget; 1492 return .{ 1493 .focus = try Focus.create(allocator, .container), 1494 .grid = null, 1495 .child = child, 1496 }; 1497 } 1498 1499 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1500 self.focus.destroy(allocator); 1501 if (self.grid) |*grid| { 1502 grid.deinit(); 1503 self.grid = null; 1504 } 1505 self.child.deinit(allocator); 1506 allocator.destroy(self.child); 1507 } 1508 1509 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1510 self.clearGrid(); 1511 self.getFocus().clear(); 1512 1513 // build the child without forcing it to fill min_size; it sizes 1514 // itself to its content within the available max. 1515 try self.child.build(allocator, .{ 1516 .min_size = .{ .width = null, .height = null }, 1517 .max_size = constraint.max_size, 1518 }, root_focus); 1519 1520 const child_grid = self.child.getGrid() orelse return; 1521 1522 // prefer max when given; otherwise grow to max(min, child) so that 1523 // when only a min is set (e.g. the wasm path passes viewport rows 1524 // as min_height with no max), we can still vertically center while 1525 // letting taller content extend past the min. 1526 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; 1527 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; 1528 if (width == 0 or height == 0) return; 1529 1530 const offset_x: usize = (width -| child_grid.size.width) / 2; 1531 const offset_y: usize = (height -| child_grid.size.height) / 2; 1532 1533 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1534 errdefer grid.deinit(); 1535 try grid.drawGrid(child_grid, offset_x, offset_y); 1536 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1537 self.getFocus().child_id = self.child.getFocus().id; 1538 1539 self.grid = grid; 1540 } 1541 1542 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1543 try self.child.input(allocator, key, root_focus); 1544 } 1545 1546 pub fn clearGrid(self: *Center) void { 1547 if (self.grid) |*grid| { 1548 grid.deinit(); 1549 self.grid = null; 1550 } 1551 self.child.clearGrid(); 1552 } 1553 1554 pub fn getGrid(self: Center) ?Grid { 1555 return self.grid; 1556 } 1557 1558 pub fn getFocus(self: *Center) *Focus { 1559 return self.focus; 1560 } 1561 }; 1562 1563 // renders truecolor ANSI art 1564 pub const AnsiArt = struct { 1565 focus: *Focus, 1566 grid: ?Grid, 1567 content: []const u8, 1568 1569 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1570 return .{ 1571 .focus = try Focus.create(allocator, .container), 1572 .grid = null, 1573 .content = content, 1574 }; 1575 } 1576 1577 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1578 self.focus.destroy(allocator); 1579 if (self.grid) |*grid| { 1580 grid.deinit(); 1581 self.grid = null; 1582 } 1583 } 1584 1585 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1586 _ = root_focus; 1587 self.clearGrid(); 1588 1589 // parse into rows of cells, then copy into a rectangular grid 1590 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1591 defer { 1592 for (rows.items) |*row| row.deinit(allocator); 1593 rows.deinit(allocator); 1594 } 1595 var row: std.ArrayList(Grid.Cell) = .empty; 1596 errdefer row.deinit(allocator); 1597 1598 var style: Grid.Style = .{}; 1599 var width: usize = 0; 1600 const content = self.content; 1601 var i: usize = 0; 1602 while (i < content.len) { 1603 const byte = content[i]; 1604 if (byte == '\n') { 1605 width = @max(width, row.items.len); 1606 try rows.append(allocator, row); 1607 row = .empty; 1608 i += 1; 1609 } else if (byte == '\r') { 1610 // git may checkout the ansi art with crlf line endings on windows 1611 i += 1; 1612 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1613 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1614 var j = i + 2; 1615 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1616 if (j >= content.len) { 1617 i = content.len; // malformed trailing escape; stop 1618 } else { 1619 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1620 i = j + 1; 1621 } 1622 } else { 1623 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1624 const end = @min(content.len, i + len); 1625 const rune = content[i..end]; 1626 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 1627 // a foreground color does not make a space visible. only an 1628 // effective background makes it opaque; foreground state often 1629 // remains active through the padding at the end of a row. 1630 const effective_background = if (style.inverted) style.fg else style.bg; 1631 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 1632 try row.append(allocator, .{ 1633 .rune = if (transparent) null else codepoint, 1634 .style = style, 1635 }); 1636 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 1637 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 1638 } 1639 i = end; 1640 } 1641 } 1642 // a trailing row with content but no closing newline 1643 if (row.items.len > 0) { 1644 width = @max(width, row.items.len); 1645 try rows.append(allocator, row); 1646 } else { 1647 row.deinit(allocator); 1648 } 1649 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1650 1651 const height = rows.items.len; 1652 if (width == 0 or height == 0) return; 1653 1654 const clamped_w = @min(width, constraint.max_size.width orelse width); 1655 const clamped_h = @min(height, constraint.max_size.height orelse height); 1656 1657 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1658 errdefer grid.deinit(); 1659 for (rows.items[0..clamped_h], 0..) |r, y| { 1660 const n = @min(r.items.len, clamped_w); 1661 for (r.items[0..n], 0..) |cell, x| { 1662 if (cell.continuation) continue; 1663 (try grid.cell(x, y)).style = cell.style; 1664 try grid.setRune(x, y, cell.rune); 1665 } 1666 } 1667 self.grid = grid; 1668 } 1669 1670 pub fn clearGrid(self: *AnsiArt) void { 1671 if (self.grid) |*grid| { 1672 grid.deinit(); 1673 self.grid = null; 1674 } 1675 } 1676 1677 pub fn getGrid(self: AnsiArt) ?Grid { 1678 return self.grid; 1679 } 1680 1681 pub fn getFocus(self: *AnsiArt) *Focus { 1682 return self.focus; 1683 } 1684 1685 fn applySgr(style: *Grid.Style, params: []const u8) void { 1686 var nums: [16]u32 = undefined; 1687 var n: usize = 0; 1688 var it = std.mem.splitScalar(u8, params, ';'); 1689 while (it.next()) |tok| { 1690 if (n >= nums.len) break; 1691 // an empty parameter (e.g. bare "\x1b[m") means 0 1692 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1693 n += 1; 1694 } 1695 if (n == 0) { 1696 style.* = .{}; 1697 return; 1698 } 1699 var i: usize = 0; 1700 while (i < n) : (i += 1) { 1701 switch (nums[i]) { 1702 0 => style.* = .{}, 1703 7 => style.inverted = true, 1704 27 => style.inverted = false, 1705 39 => style.fg = null, 1706 49 => style.bg = null, 1707 38, 48 => { 1708 // truecolor form: 38;2;r;g;b — anything else is ignored 1709 if (i + 4 < n and nums[i + 1] == 2) { 1710 const c = Grid.Color{ .rgb = .{ 1711 .r = @truncate(nums[i + 2]), 1712 .g = @truncate(nums[i + 3]), 1713 .b = @truncate(nums[i + 4]), 1714 } }; 1715 if (nums[i] == 38) style.fg = c else style.bg = c; 1716 i += 4; 1717 } 1718 }, 1719 else => {}, 1720 } 1721 } 1722 } 1723 }; 1724 1725 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1726 pub const AnsiBackground = struct { 1727 grid: ?Grid, 1728 child: *Widget, 1729 art: AnsiArt, 1730 session: *ui.Session, 1731 1732 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1733 var cw = child_widget; 1734 const child = allocator.create(Widget) catch |e| { 1735 cw.deinit(allocator); 1736 return e; 1737 }; 1738 child.* = cw; 1739 errdefer { 1740 child.deinit(allocator); 1741 allocator.destroy(child); 1742 } 1743 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1744 } 1745 1746 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1747 if (self.grid) |*grid| grid.deinit(); 1748 self.art.deinit(allocator); 1749 self.child.deinit(allocator); 1750 allocator.destroy(self.child); 1751 } 1752 1753 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1754 self.clearGrid(); 1755 1756 // make the wrapped page fill the whole available area 1757 try self.child.build(allocator, .{ 1758 .min_size = .{ 1759 .width = constraint.max_size.width orelse constraint.min_size.width, 1760 .height = constraint.max_size.height orelse constraint.min_size.height, 1761 }, 1762 .max_size = constraint.max_size, 1763 }, root_focus); 1764 1765 const foreground = self.child.getGrid() orelse return; 1766 if (!self.session.color) return; 1767 // terminals show the art only once they report a background to fade it toward 1768 const backdrop: ?Grid.Color.Rgb = if (self.session.is_terminal) self.session.terminal_background orelse return else null; 1769 self.art.content = self.session.data.ansi_art; 1770 try self.art.build(allocator, .{ 1771 .min_size = .{ .width = null, .height = null }, 1772 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1773 }, root_focus); 1774 // the web fades the art with css opacity instead 1775 const art_backdrop = backdrop orelse return; 1776 if (self.art.grid) |*art_grid| { 1777 for (art_grid.cells) |*cell| { 1778 cell.style.fg = fadeColor(cell.style.fg, art_backdrop); 1779 cell.style.bg = fadeColor(cell.style.bg, art_backdrop); 1780 } 1781 self.grid = try artBehind(allocator, foreground, art_grid.*); 1782 } 1783 } 1784 1785 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1786 try self.child.input(allocator, key, root_focus); 1787 } 1788 1789 pub fn clearGrid(self: *AnsiBackground) void { 1790 if (self.grid) |*grid| { 1791 grid.deinit(); 1792 self.grid = null; 1793 } 1794 self.child.clearGrid(); 1795 self.art.clearGrid(); 1796 } 1797 1798 pub fn getGrid(self: AnsiBackground) ?Grid { 1799 return self.grid orelse self.child.getGrid(); 1800 } 1801 1802 pub fn getFocus(self: *AnsiBackground) *Focus { 1803 return self.child.getFocus(); 1804 } 1805 1806 // a foreground cell counts as blank if it's empty or a bare space without styling 1807 fn cellIsBlank(cell: Grid.Cell) bool { 1808 if (cell.continuation) return false; 1809 if (cell.rune) |rune| { 1810 return rune == ' ' and 1811 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1812 } 1813 return true; 1814 } 1815 1816 // the art is faded toward the background at this opacity so foreground 1817 // text stays legible over it 1818 pub const art_opacity = 25; // percent 1819 1820 // the art parser only emits truecolor, so a palette color never occurs 1821 fn artRgb(color: ?Grid.Color) ?Grid.Color.Rgb { 1822 const c = color orelse return null; 1823 return switch (c) { 1824 .rgb => |v| v, 1825 else => null, 1826 }; 1827 } 1828 1829 fn fadeColor(color: ?Grid.Color, backdrop: Grid.Color.Rgb) ?Grid.Color { 1830 const v = artRgb(color) orelse return color; 1831 const blend = struct { 1832 fn f(c: u8, b: u8) u8 { 1833 return @intCast((@as(u16, c) * art_opacity + @as(u16, b) * (100 - art_opacity)) / 100); 1834 } 1835 }.f; 1836 return .{ .rgb = .{ .r = blend(v.r, backdrop.r), .g = blend(v.g, backdrop.g), .b = blend(v.b, backdrop.b) } }; 1837 } 1838 1839 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1840 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1841 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1842 // snap such channels clear of that range so the backdrop doesn't blink or 1843 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1844 // actually render truecolor. 1845 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1846 const v = artRgb(color) orelse return color; 1847 const snap = struct { 1848 fn f(c: u8) u8 { 1849 return if (c >= 1 and c <= 9) 10 else c; 1850 } 1851 }.f; 1852 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 1853 } 1854 1855 // composites ANSI art behind a foreground grid 1856 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1857 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1858 errdefer out.deinit(); 1859 1860 const anchor_x = foreground.size.width -| art_grid.size.width; 1861 for (0..art_grid.size.height) |y| { 1862 for (0..art_grid.size.width) |x| { 1863 const src = (try art_grid.cell(x, y)).*; 1864 if (src.rune == null) continue; 1865 const dst = try out.cell(anchor_x + x, y); 1866 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1867 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1868 // blank cells take the art; occupied cells take its background 1869 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1870 dst.style = src.style; 1871 dst.style.fg = sgrSafe(dst.style.fg); 1872 dst.style.bg = sgrSafe(dst.style.bg); 1873 try out.setRune(anchor_x + x, y, src.rune); 1874 } else { 1875 const art_bg = sgrSafe(src.style.bg orelse src.style.fg); 1876 dst.style.bg = art_bg; 1877 if (next_dst) |next| next.style.bg = art_bg; 1878 } 1879 } 1880 } 1881 return out; 1882 } 1883 }; 1884 1885 pub const CopyableText = struct { 1886 pub const Choice = struct { 1887 selector: []const u8 = "", 1888 text: []const u8, 1889 copyable_text: ?[]const u8 = null, 1890 label: []const u8 = "", 1891 bottom_label: []const u8 = "", 1892 }; 1893 1894 box: wgt.Box(Widget), 1895 session: *ui.Session, 1896 choices: []Choice, 1897 selected: usize = 0, 1898 1899 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice, initial: usize) !CopyableText { 1900 if (choices.len == 0) return error.MissingCopyableText; 1901 1902 const owned_choices = try allocator.dupe(Choice, choices); 1903 errdefer allocator.free(owned_choices); 1904 1905 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1906 errdefer box.deinit(allocator); 1907 1908 if (choices.len > 1) for (choices) |choice| { 1909 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 1910 errdefer selector_box.deinit(allocator); 1911 selector_box.getFocus().mode = .all; 1912 try box.children.put(allocator, selector_box.getFocus().id, .{ .widget = .{ .text_box = selector_box }, .rect = null, .min_size = .{ .width = choice.selector.len + 2, .height = 3 } }); 1913 }; 1914 1915 var text_input = try wgt.TextInput.init(allocator, .{ 1916 .border = .single, 1917 .round_corners = true, 1918 .top_label = .{ .text = choices[initial].label }, 1919 .bottom_label = .{ .text = choices[initial].bottom_label }, 1920 .read_only = true, 1921 .render_content = session.is_terminal, 1922 .visible_width = null, 1923 }); 1924 errdefer text_input.deinit(allocator); 1925 text_input.getFocus().mode = .all; 1926 try text_input.setContent(allocator, choices[initial].text); 1927 box.getFocus().child_id = text_input.getFocus().id; 1928 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1929 1930 return .{ .box = box, .session = session, .choices = owned_choices, .selected = initial }; 1931 } 1932 1933 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, handle: ui.RepoHandle) !?CopyableText { 1934 const aa = session.page_arena.allocator(); 1935 var choices: [2]Choice = undefined; 1936 var count: usize = 0; 1937 var ssh_index: ?usize = null; 1938 switch (handle.location) { 1939 .repo => |identity| { 1940 if (session.data.git_http_port) |port| { 1941 const url = try aa.print("http://localhost:{d}/{s}", .{ port, identity }); 1942 choices[count] = .{ 1943 .selector = "http", 1944 .text = url, 1945 .copyable_text = try aa.print("git clone {s}", .{url}), 1946 .label = " clone ", 1947 }; 1948 count += 1; 1949 } 1950 if (session.data.git_ssh_port) |port| { 1951 ssh_index = count; 1952 const url = try aa.print("ssh://localhost:{d}/{s}", .{ port, identity }); 1953 choices[count] = .{ 1954 .selector = "ssh", 1955 .text = url, 1956 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1957 .label = " clone ", 1958 }; 1959 count += 1; 1960 } 1961 }, 1962 .fork => |f| if (session.data.git_ssh_port) |port| { 1963 const url = try aa.print("ssh://localhost:{d}/{s}+{s}", .{ port, f.identity, f.id }); 1964 choices[count] = .{ 1965 .text = url, 1966 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1967 .label = " clone ", 1968 }; 1969 count += 1; 1970 }, 1971 } 1972 if (count == 0) return null; 1973 // a viewer who can push starts on the ssh url 1974 const initial = if (handle.canWrite()) ssh_index orelse 0 else 0; 1975 return @as(?CopyableText, try init(allocator, session, choices[0..count], initial)); 1976 } 1977 1978 pub fn minWidth(self: *const CopyableText) usize { 1979 var width = maxWidth(self.choices) + 2; 1980 if (self.choices.len > 1) { 1981 for (self.choices) |choice| width += choice.selector.len + 2; 1982 } 1983 return width; 1984 } 1985 1986 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1987 self.box.deinit(allocator); 1988 allocator.free(self.choices); 1989 } 1990 1991 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1992 self.clearGrid(); 1993 1994 if (self.choices.len > 1) { 1995 if (root_focus.grandchild_id) |id| { 1996 for (0..self.choices.len) |i| { 1997 if (id != self.selector(i).getFocus().id) continue; 1998 if (i != self.selected) try self.select(allocator, i); 1999 root_focus.setFocus(self.textInput().getFocus().id); 2000 break; 2001 } 2002 } 2003 2004 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 2005 for (0..self.choices.len) |i| { 2006 self.selector(i).options.border = if (i == self.selected) 2007 (if (focused) .double else .single) 2008 else 2009 .hidden; 2010 } 2011 } 2012 self.textInput().options.border = .single; 2013 self.textInput().cursor = 0; 2014 self.textInput().scroll_offset = 0; 2015 try self.box.build(allocator, constraint, root_focus); 2016 2017 if (!self.session.is_terminal) { 2018 const aa = self.session.arena.allocator(); 2019 const id = self.textInput().getFocus().id; 2020 try self.session.text_inputs.put(aa, id, self.textInput()); 2021 } 2022 } 2023 2024 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2025 switch (key) { 2026 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 2027 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 2028 .enter => if (self.session.is_terminal) self.show(), 2029 .mouse => |mouse| { 2030 if (self.choices.len > 1) for (0..self.choices.len) |i| { 2031 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 2032 try self.select(allocator, i); 2033 root_focus.setFocus(self.textInput().getFocus().id); 2034 return; 2035 }; 2036 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 2037 }, 2038 else => {}, 2039 } 2040 } 2041 2042 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 2043 self.selected = selected; 2044 const choice = self.choices[selected]; 2045 const text_input = self.textInput(); 2046 text_input.options.top_label.text = choice.label; 2047 text_input.options.bottom_label.text = choice.bottom_label; 2048 try text_input.setContent(allocator, choice.text); 2049 } 2050 2051 fn show(self: *CopyableText) void { 2052 const choice = self.choices[self.selected]; 2053 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 2054 } 2055 2056 fn textInput(self: *CopyableText) *wgt.TextInput { 2057 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 2058 } 2059 2060 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 2061 return &self.box.children.values()[index].widget.text_box; 2062 } 2063 2064 fn maxWidth(choices: []const Choice) usize { 2065 var width: usize = 0; 2066 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 2067 return width; 2068 } 2069 2070 pub fn clearGrid(self: *CopyableText) void { 2071 self.box.clearGrid(); 2072 } 2073 2074 pub fn getGrid(self: CopyableText) ?Grid { 2075 return self.box.getGrid(); 2076 } 2077 2078 pub fn getFocus(self: *CopyableText) *Focus { 2079 return self.box.getFocus(); 2080 } 2081 }; 2082