╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1250 self.grid = grid; 1251 } 1252 1253 pub fn clearGrid(self: *AnsiArt) void { 1254 if (self.grid) |*grid| { 1255 grid.deinit(); 1256 self.grid = null; 1257 } 1258 } 1259 1260 pub fn getGrid(self: AnsiArt) ?Grid { 1261 return self.grid; 1262 } 1263 1264 pub fn getFocus(self: *AnsiArt) *Focus { 1265 return self.focus; 1266 } 1267 1268 fn applySgr(style: *Grid.Style, params: []const u8) void { 1269 var nums: [16]u32 = undefined; 1270 var n: usize = 0; 1271 var it = std.mem.splitScalar(u8, params, ';'); 1272 while (it.next()) |tok| { 1273 if (n >= nums.len) break; 1274 // an empty parameter (e.g. bare "\x1b[m") means 0 1275 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1276 n += 1; 1277 } 1278 if (n == 0) { 1279 style.* = .{}; 1280 return; 1281 } 1282 var i: usize = 0; 1283 while (i < n) : (i += 1) { 1284 switch (nums[i]) { 1285 0 => style.* = .{}, 1286 7 => style.inverted = true, 1287 27 => style.inverted = false, 1288 39 => style.fg = null, 1289 49 => style.bg = null, 1290 38, 48 => { 1291 // truecolor form: 38;2;r;g;b — anything else is ignored 1292 if (i + 4 < n and nums[i + 1] == 2) { 1293 const c = Grid.Color{ 1294 .r = @truncate(nums[i + 2]), 1295 .g = @truncate(nums[i + 3]), 1296 .b = @truncate(nums[i + 4]), 1297 }; 1298 if (nums[i] == 38) style.fg = c else style.bg = c; 1299 i += 4; 1300 } 1301 }, 1302 else => {}, 1303 } 1304 } 1305 } 1306 }; 1307 1308 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1309 pub const AnsiBackground = struct { 1310 grid: ?Grid, 1311 child: *Widget, 1312 art: AnsiArt, 1313 session: *ui.Session, 1314 1315 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1316 var cw = child_widget; 1317 const child = allocator.create(Widget) catch |e| { 1318 cw.deinit(allocator); 1319 return e; 1320 }; 1321 child.* = cw; 1322 errdefer { 1323 child.deinit(allocator); 1324 allocator.destroy(child); 1325 } 1326 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1327 } 1328 1329 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1330 if (self.grid) |*grid| grid.deinit(); 1331 self.art.deinit(allocator); 1332 self.child.deinit(allocator); 1333 allocator.destroy(self.child); 1334 } 1335 1336 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1337 self.clearGrid(); 1338 1339 // make the wrapped page fill the whole available area 1340 try self.child.build(allocator, .{ 1341 .min_size = .{ 1342 .width = constraint.max_size.width orelse constraint.min_size.width, 1343 .height = constraint.max_size.height orelse constraint.min_size.height, 1344 }, 1345 .max_size = constraint.max_size, 1346 }, root_focus); 1347 1348 if (!self.session.data.enable_ansi) return; 1349 const foreground = self.child.getGrid() orelse return; 1350 self.art.content = self.session.data.ansi_art; 1351 try self.buildArt(allocator, foreground.size, root_focus); 1352 if (!self.session.is_terminal) return; 1353 const art_grid = self.art.getGrid() orelse return; 1354 self.grid = try artBehind(allocator, foreground, art_grid); 1355 } 1356 1357 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1358 try self.child.input(allocator, key, root_focus); 1359 } 1360 1361 pub fn clearGrid(self: *AnsiBackground) void { 1362 if (self.grid) |*grid| { 1363 grid.deinit(); 1364 self.grid = null; 1365 } 1366 self.child.clearGrid(); 1367 self.art.clearGrid(); 1368 } 1369 1370 pub fn getGrid(self: AnsiBackground) ?Grid { 1371 return self.grid orelse self.child.getGrid(); 1372 } 1373 1374 pub fn getFocus(self: *AnsiBackground) *Focus { 1375 return self.child.getFocus(); 1376 } 1377 1378 // a foreground cell counts as blank if it's empty or a bare space without styling 1379 fn cellIsBlank(cell: Grid.Cell) bool { 1380 if (cell.continuation) return false; 1381 if (cell.rune) |rune| { 1382 return rune == ' ' and 1383 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1384 } 1385 return true; 1386 } 1387 1388 // the art is dimmed to this fraction of its brightness so foreground text 1389 // stays legible over it 1390 const art_brightness = 35; // percent 1391 1392 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1393 const c = color orelse return null; 1394 return .{ 1395 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1396 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1397 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1398 }; 1399 } 1400 1401 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 1402 try self.art.build(allocator, .{ 1403 .min_size = .{ .width = null, .height = null }, 1404 .max_size = .{ .width = size.width, .height = size.height }, 1405 }, root_focus); 1406 if (self.art.grid) |*grid| { 1407 for (grid.cells) |*cell| { 1408 cell.style.fg = dimColor(cell.style.fg); 1409 cell.style.bg = dimColor(cell.style.bg); 1410 } 1411 } 1412 } 1413 1414 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1415 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1416 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1417 // snap such channels clear of that range so the backdrop doesn't blink or 1418 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1419 // actually render truecolor. 1420 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1421 const c = color orelse return null; 1422 const snap = struct { 1423 fn f(v: u8) u8 { 1424 return if (v >= 1 and v <= 9) 10 else v; 1425 } 1426 }.f; 1427 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1428 } 1429 1430 fn applyArtBackground(dst: *Grid.Cell, src: Grid.Cell) void { 1431 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 1432 dst.style.bg = background_maybe; 1433 // change unstyled text to contrast with the art behind it 1434 if (dst.style.fg == null) { 1435 if (background_maybe) |background| { 1436 // use near-black and near-white, which is easier on the eyes 1437 // while retaining high contrast 1438 const luminance = (@as(u32, background.r) * 299 + 1439 @as(u32, background.g) * 587 + 1440 @as(u32, background.b) * 114) / 1000; 1441 dst.style.fg = if (luminance >= 128) 1442 .{ .r = 16, .g = 16, .b = 16 } 1443 else 1444 .{ .r = 240, .g = 240, .b = 240 }; 1445 } 1446 } 1447 } 1448 1449 // composites ANSI art behind a foreground grid 1450 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1451 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1452 errdefer out.deinit(); 1453 1454 const anchor_x = foreground.size.width -| art_grid.size.width; 1455 for (0..art_grid.size.height) |y| { 1456 for (0..art_grid.size.width) |x| { 1457 const src = (try art_grid.cell(x, y)).*; 1458 if (src.rune == null) continue; 1459 const dst = try out.cell(anchor_x + x, y); 1460 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1461 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1462 // blank cells take the art; occupied cells take its background 1463 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1464 dst.style = src.style; 1465 dst.style.fg = sgrSafe(dst.style.fg); 1466 dst.style.bg = sgrSafe(dst.style.bg); 1467 try out.setRune(anchor_x + x, y, src.rune); 1468 } else { 1469 applyArtBackground(dst, src); 1470 if (next_dst) |next| applyArtBackground(next, src); 1471 } 1472 } 1473 } 1474 return out; 1475 } 1476 }; 1477 1478 pub const CopyableText = struct { 1479 pub const Choice = struct { 1480 selector: []const u8 = "", 1481 text: []const u8, 1482 copyable_text: ?[]const u8 = null, 1483 label: []const u8 = "", 1484 bottom_label: []const u8 = "", 1485 }; 1486 1487 box: wgt.Box(Widget), 1488 session: *ui.Session, 1489 choices: []Choice, 1490 selected: usize = 0, 1491 1492 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1493 if (choices.len == 0) return error.MissingCopyableText; 1494 1495 const owned_choices = try allocator.dupe(Choice, choices); 1496 errdefer allocator.free(owned_choices); 1497 1498 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1499 errdefer box.deinit(allocator); 1500 1501 if (choices.len > 1) for (choices) |choice| { 1502 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 1503 errdefer selector_box.deinit(allocator); 1504 selector_box.getFocus().mode = .all; 1505 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 } }); 1506 }; 1507 1508 var text_input = try wgt.TextInput.init(allocator, .{ 1509 .border_style = .single, 1510 .rounded_corners = true, 1511 .label = choices[0].label, 1512 .bottom_label = choices[0].bottom_label, 1513 .read_only = true, 1514 .render_content = session.is_terminal, 1515 .visible_width = null, 1516 }); 1517 errdefer text_input.deinit(allocator); 1518 text_input.getFocus().mode = .all; 1519 try text_input.setContent(allocator, choices[0].text); 1520 box.getFocus().child_id = text_input.getFocus().id; 1521 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1522 1523 return .{ .box = box, .session = session, .choices = owned_choices }; 1524 } 1525 1526 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1527 const aa = session.page_arena.allocator(); 1528 var choices: [2]Choice = undefined; 1529 var count: usize = 0; 1530 switch (location) { 1531 .repo => |identity| { 1532 if (session.data.git_http_port) |port| { 1533 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1534 choices[count] = .{ 1535 .selector = "http", 1536 .text = url, 1537 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1538 .label = " clone ", 1539 }; 1540 count += 1; 1541 } 1542 if (session.data.git_ssh_port) |port| { 1543 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1544 choices[count] = .{ 1545 .selector = "ssh", 1546 .text = url, 1547 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1548 .label = " clone ", 1549 }; 1550 count += 1; 1551 } 1552 }, 1553 .fork => |f| if (session.data.git_ssh_port) |port| { 1554 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1555 choices[count] = .{ 1556 .text = url, 1557 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1558 .label = " clone ", 1559 }; 1560 count += 1; 1561 }, 1562 } 1563 if (count == 0) return null; 1564 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1565 } 1566 1567 pub fn minWidth(self: *const CopyableText) usize { 1568 var width = maxWidth(self.choices) + 2; 1569 if (self.choices.len > 1) { 1570 for (self.choices) |choice| width += choice.selector.len + 2; 1571 } 1572 return width; 1573 } 1574 1575 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1576 self.box.deinit(allocator); 1577 allocator.free(self.choices); 1578 } 1579 1580 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1581 self.clearGrid(); 1582 1583 if (self.choices.len > 1) { 1584 if (root_focus.grandchild_id) |id| { 1585 for (0..self.choices.len) |i| { 1586 if (id != self.selector(i).getFocus().id) continue; 1587 if (i != self.selected) try self.select(allocator, i); 1588 root_focus.setFocus(self.textInput().getFocus().id); 1589 break; 1590 } 1591 } 1592 1593 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1594 for (0..self.choices.len) |i| { 1595 self.selector(i).options.border_style = if (i == self.selected) 1596 (if (focused) .double else .single) 1597 else 1598 .hidden; 1599 } 1600 } 1601 self.textInput().options.border_style = .single; 1602 self.textInput().cursor = 0; 1603 self.textInput().scroll_offset = 0; 1604 try self.box.build(allocator, constraint, root_focus); 1605 1606 if (!self.session.is_terminal) { 1607 const aa = self.session.arena.allocator(); 1608 const id = self.textInput().getFocus().id; 1609 try self.session.text_inputs.put(aa, id, self.textInput()); 1610 } 1611 } 1612 1613 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1614 switch (key) { 1615 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1616 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1617 .enter => if (self.session.is_terminal) self.show(), 1618 .mouse => |mouse| { 1619 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1620 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1621 try self.select(allocator, i); 1622 root_focus.setFocus(self.textInput().getFocus().id); 1623 return; 1624 }; 1625 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1626 }, 1627 else => {}, 1628 } 1629 } 1630 1631 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1632 self.selected = selected; 1633 const choice = self.choices[selected]; 1634 const text_input = self.textInput(); 1635 text_input.options.label = choice.label; 1636 text_input.options.bottom_label = choice.bottom_label; 1637 try text_input.setContent(allocator, choice.text); 1638 } 1639 1640 fn show(self: *CopyableText) void { 1641 const choice = self.choices[self.selected]; 1642 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1643 } 1644 1645 fn textInput(self: *CopyableText) *wgt.TextInput { 1646 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1647 } 1648 1649 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1650 return &self.box.children.values()[index].widget.text_box; 1651 } 1652 1653 fn maxWidth(choices: []const Choice) usize { 1654 var width: usize = 0; 1655 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1656 return width; 1657 } 1658 1659 pub fn clearGrid(self: *CopyableText) void { 1660 self.box.clearGrid(); 1661 } 1662 1663 pub fn getGrid(self: CopyableText) ?Grid { 1664 return self.box.getGrid(); 1665 } 1666 1667 pub fn getFocus(self: *CopyableText) *Focus { 1668 return self.box.getFocus(); 1669 } 1670 }; 1671