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