╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1188 const total_width = cols * cell_width; 1189 if (total_width == 0 or content_height == 0) return; 1190 1191 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 1192 errdefer grid.deinit(); 1193 1194 for (self.text_boxes.items, 0..) |*tb, i| { 1195 const tb_grid = tb.getGrid() orelse continue; 1196 const col = i % cols; 1197 const row = i / cols; 1198 const cell_x = col * cell_width; 1199 const cell_y = row * slot_height; 1200 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 1201 try grid.drawGrid(tb_grid, cell_x, cell_y); 1202 } 1203 1204 self.grid = grid; 1205 } 1206 1207 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1208 _ = self; 1209 _ = allocator; 1210 _ = key; 1211 _ = root_focus; 1212 } 1213 1214 pub fn clearGrid(self: *FlowBox) void { 1215 if (self.grid) |*grid| { 1216 grid.deinit(); 1217 self.grid = null; 1218 } 1219 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1220 } 1221 1222 pub fn getGrid(self: FlowBox) ?Grid { 1223 return self.grid; 1224 } 1225 1226 pub fn getFocus(self: *FlowBox) *Focus { 1227 return self.focus; 1228 } 1229 1230 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 1231 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 1232 const slot_height = self.options.cell_height + border_rows; 1233 const col = index % self.last_cols; 1234 const row = index / self.last_cols; 1235 return .{ 1236 .x = @intCast(col * self.options.cell_width), 1237 .y = @intCast(row * slot_height), 1238 .size = .{ .width = self.options.cell_width, .height = slot_height }, 1239 }; 1240 } 1241 1242 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 1243 for (self.text_boxes.items, 0..) |tb, i| { 1244 if (tb.box.focus.id == focus_id) return i; 1245 } 1246 return null; 1247 } 1248 1249 pub const Scroll = struct { 1250 scroll: wgt.Scroll(Widget), 1251 1252 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 1253 var layout_inner = try FlowBox.init(allocator, options); 1254 errdefer layout_inner.deinit(allocator); 1255 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 1256 errdefer scroll.deinit(allocator); 1257 return .{ .scroll = scroll }; 1258 } 1259 1260 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 1261 self.scroll.deinit(allocator); 1262 } 1263 1264 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 1265 self.scroll.x = 0; 1266 self.scroll.y = 0; 1267 try self.inner().setItems(allocator, items); 1268 } 1269 1270 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1271 try self.scroll.build(allocator, constraint, root_focus); 1272 } 1273 1274 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1275 _ = allocator; 1276 const in = self.inner(); 1277 const child_id = in.focus.child_id orelse return; 1278 const current_index = in.indexOfFocusId(child_id) orelse return; 1279 const count = in.text_boxes.items.len; 1280 if (count == 0) return; 1281 const cols = in.last_cols; 1282 const slot_height = in.options.cell_height + FlowBox.border_rows; 1283 1284 var index = current_index; 1285 switch (key) { 1286 .arrow_up => index -|= cols, 1287 .arrow_down => if (index + cols < count) { 1288 index += cols; 1289 }, 1290 .arrow_left => index -|= 1, 1291 .arrow_right => if (index + 1 < count) { 1292 index += 1; 1293 }, 1294 .home => index = 0, 1295 .end => index = count - 1, 1296 .page_up => { 1297 if (self.scroll.grid) |grid| if (slot_height > 0) { 1298 const rows_per_page = grid.size.height / slot_height; 1299 index -|= rows_per_page * cols; 1300 }; 1301 }, 1302 .page_down => { 1303 if (self.scroll.grid) |grid| if (slot_height > 0) { 1304 const rows_per_page = grid.size.height / slot_height; 1305 index = @min(index + rows_per_page * cols, count - 1); 1306 }; 1307 }, 1308 // scroll wheel moves the focused cell by a full row so the 1309 // viewport (via scrollToRect) follows in row-sized steps, 1310 // matching how a scroll wheel feels in a grid view. 1311 .mouse => |mouse| switch (mouse.action) { 1312 .scroll => |dir| switch (dir) { 1313 .up => index -|= cols, 1314 .down => if (index + cols < count) { 1315 index += cols; 1316 }, 1317 }, 1318 else => {}, 1319 }, 1320 else => {}, 1321 } 1322 1323 if (index != current_index) { 1324 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 1325 if (in.cellRect(index)) |rect| { 1326 self.scroll.scrollToRect(rect); 1327 } 1328 } 1329 } 1330 1331 pub fn clearGrid(self: *Scroll) void { 1332 self.scroll.clearGrid(); 1333 } 1334 1335 pub fn getGrid(self: Scroll) ?Grid { 1336 return self.scroll.getGrid(); 1337 } 1338 1339 pub fn getFocus(self: *Scroll) *Focus { 1340 return self.scroll.getFocus(); 1341 } 1342 1343 pub fn getSelectedIndex(self: Scroll) ?usize { 1344 const in = self.scroll.child.flow_box; 1345 const child_id = in.focus.child_id orelse return null; 1346 return in.indexOfFocusId(child_id); 1347 } 1348 1349 fn inner(self: *Scroll) *FlowBox { 1350 return &self.scroll.child.flow_box; 1351 } 1352 }; 1353 }; 1354 1355 // an invisible widget that fills the horizontal space granted by its parent. 1356 // used inside Box(horiz) with a min_size so the box reserves space for the 1357 // children that follow, pushing them to the right. 1358 pub const Spacer = struct { 1359 focus: *Focus, 1360 grid: ?Grid, 1361 1362 pub fn init(allocator: std.mem.Allocator) !Spacer { 1363 return .{ 1364 .focus = try Focus.create(allocator, .container), 1365 .grid = null, 1366 }; 1367 } 1368 1369 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1370 self.focus.destroy(allocator); 1371 if (self.grid) |*grid| { 1372 grid.deinit(); 1373 self.grid = null; 1374 } 1375 } 1376 1377 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1378 _ = root_focus; 1379 self.clearGrid(); 1380 const width = constraint.max_size.width orelse return; 1381 if (width == 0) return; 1382 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1383 } 1384 1385 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1386 _ = self; 1387 _ = allocator; 1388 _ = key; 1389 _ = root_focus; 1390 } 1391 1392 pub fn clearGrid(self: *Spacer) void { 1393 if (self.grid) |*grid| { 1394 grid.deinit(); 1395 self.grid = null; 1396 } 1397 } 1398 1399 pub fn getGrid(self: Spacer) ?Grid { 1400 return self.grid; 1401 } 1402 1403 pub fn getFocus(self: *Spacer) *Focus { 1404 return self.focus; 1405 } 1406 }; 1407 1408 // a one-row, non-focusable status bar showing the current page's url 1409 pub const Footer = struct { 1410 focus: *Focus, 1411 grid: ?Grid, 1412 session: *Session, 1413 arena: std.heap.ArenaAllocator, 1414 1415 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 1416 return .{ 1417 .focus = try Focus.create(allocator, .container), 1418 .grid = null, 1419 .session = session, 1420 .arena = std.heap.ArenaAllocator.init(allocator), 1421 }; 1422 } 1423 1424 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1425 self.focus.destroy(allocator); 1426 if (self.grid) |*grid| grid.deinit(); 1427 self.arena.deinit(); 1428 } 1429 1430 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1431 _ = root_focus; 1432 self.clearGrid(); 1433 const width = constraint.max_size.width orelse return; 1434 if (width == 0) return; 1435 1436 _ = self.arena.reset(.retain_capacity); 1437 const aa = self.arena.allocator(); 1438 const path = self.session.data.current_page.urlAlloc(&self.arena) catch return; 1439 const text = if (self.session.web_port) |port| 1440 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1441 else 1442 path; 1443 1444 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1445 errdefer grid.deinit(); 1446 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1447 var i: usize = 0; 1448 while (utf8.nextCodepointSlice()) |ch| { 1449 if (i == width) break; 1450 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1451 i += 1; 1452 } 1453 self.grid = grid; 1454 } 1455 1456 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1457 _ = self; 1458 _ = allocator; 1459 _ = key; 1460 _ = root_focus; 1461 } 1462 1463 pub fn clearGrid(self: *Footer) void { 1464 if (self.grid) |*grid| { 1465 grid.deinit(); 1466 self.grid = null; 1467 } 1468 } 1469 1470 pub fn getGrid(self: Footer) ?Grid { 1471 return self.grid; 1472 } 1473 1474 pub fn getFocus(self: *Footer) *Focus { 1475 return self.focus; 1476 } 1477 }; 1478 1479 // a single-child wrapper that builds the child at its natural size and 1480 // positions its grid in the middle of the area granted by the parent 1481 pub const Center = struct { 1482 focus: *Focus, 1483 grid: ?Grid, 1484 child: *Widget, 1485 direction: Direction, 1486 1487 pub const Direction = enum { both, horiz, vert }; 1488 1489 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, direction: Direction) !Center { 1490 const child = try allocator.create(Widget); 1491 errdefer allocator.destroy(child); 1492 child.* = child_widget; 1493 return .{ 1494 .focus = try Focus.create(allocator, .container), 1495 .grid = null, 1496 .child = child, 1497 .direction = direction, 1498 }; 1499 } 1500 1501 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1502 self.focus.destroy(allocator); 1503 if (self.grid) |*grid| { 1504 grid.deinit(); 1505 self.grid = null; 1506 } 1507 self.child.deinit(allocator); 1508 allocator.destroy(self.child); 1509 } 1510 1511 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1512 self.clearGrid(); 1513 self.getFocus().clear(); 1514 1515 // build the child without forcing it to fill min_size; it sizes 1516 // itself to its content within the available max. 1517 try self.child.build(allocator, .{ 1518 .min_size = .{ .width = null, .height = null }, 1519 .max_size = constraint.max_size, 1520 }, root_focus); 1521 1522 const child_grid = self.child.getGrid() orelse return; 1523 1524 // prefer max when given; otherwise grow to max(min, child) so that 1525 // when only a min is set (e.g. the wasm path passes viewport rows 1526 // as min_height with no max), we can still vertically center while 1527 // letting taller content extend past the min. 1528 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; 1529 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; 1530 if (width == 0 or height == 0) return; 1531 1532 const offset_x: usize = if (self.direction == .horiz or self.direction == .both) 1533 (width -| child_grid.size.width) / 2 1534 else 1535 0; 1536 const offset_y: usize = if (self.direction == .vert or self.direction == .both) 1537 (height -| child_grid.size.height) / 2 1538 else 1539 0; 1540 1541 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1542 errdefer grid.deinit(); 1543 try grid.drawGrid(child_grid, offset_x, offset_y); 1544 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1545 self.getFocus().child_id = self.child.getFocus().id; 1546 1547 self.grid = grid; 1548 } 1549 1550 pub fn input(self: *Center, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1551 try self.child.input(allocator, key, root_focus); 1552 } 1553 1554 pub fn clearGrid(self: *Center) void { 1555 if (self.grid) |*grid| { 1556 grid.deinit(); 1557 self.grid = null; 1558 } 1559 self.child.clearGrid(); 1560 } 1561 1562 pub fn getGrid(self: Center) ?Grid { 1563 return self.grid; 1564 } 1565 1566 pub fn getFocus(self: *Center) *Focus { 1567 return self.focus; 1568 } 1569 }; 1570 1571 // renders truecolor ANSI art 1572 pub const AnsiArt = struct { 1573 focus: *Focus, 1574 grid: ?Grid, 1575 content: []const u8, 1576 1577 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1578 return .{ 1579 .focus = try Focus.create(allocator, .container), 1580 .grid = null, 1581 .content = content, 1582 }; 1583 } 1584 1585 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1586 self.focus.destroy(allocator); 1587 if (self.grid) |*grid| { 1588 grid.deinit(); 1589 self.grid = null; 1590 } 1591 } 1592 1593 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1594 _ = root_focus; 1595 self.clearGrid(); 1596 1597 // parse into rows of cells, then copy into a rectangular grid 1598 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1599 defer { 1600 for (rows.items) |*row| row.deinit(allocator); 1601 rows.deinit(allocator); 1602 } 1603 var row: std.ArrayList(Grid.Cell) = .empty; 1604 errdefer row.deinit(allocator); 1605 1606 var style: Grid.Style = .{}; 1607 var width: usize = 0; 1608 const content = self.content; 1609 var i: usize = 0; 1610 while (i < content.len) { 1611 const byte = content[i]; 1612 if (byte == '\n') { 1613 width = @max(width, row.items.len); 1614 try rows.append(allocator, row); 1615 row = .empty; 1616 i += 1; 1617 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1618 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1619 var j = i + 2; 1620 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1621 if (j >= content.len) { 1622 i = content.len; // malformed trailing escape; stop 1623 } else { 1624 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1625 i = j + 1; 1626 } 1627 } else { 1628 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1629 const end = @min(content.len, i + len); 1630 const rune = content[i..end]; 1631 const transparent = end - i == 1 and rune[0] == ' ' and 1632 style.fg == null and style.bg == null and !style.inverted; 1633 try row.append(allocator, .{ 1634 .rune = if (transparent) null else rune, 1635 .style = style, 1636 }); 1637 i = end; 1638 } 1639 } 1640 // a trailing row with content but no closing newline 1641 if (row.items.len > 0) { 1642 width = @max(width, row.items.len); 1643 try rows.append(allocator, row); 1644 } else { 1645 row.deinit(allocator); 1646 } 1647 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1648 1649 const height = rows.items.len; 1650 if (width == 0 or height == 0) return; 1651 1652 const clamped_w = @min(width, constraint.max_size.width orelse width); 1653 const clamped_h = @min(height, constraint.max_size.height orelse height); 1654 1655 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1656 errdefer grid.deinit(); 1657 for (rows.items[0..clamped_h], 0..) |r, y| { 1658 const n = @min(r.items.len, clamped_w); 1659 for (r.items[0..n], 0..) |cell, x| { 1660 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1661 } 1662 } 1663 self.grid = grid; 1664 } 1665 1666 pub fn input(self: *AnsiArt, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1667 _ = self; 1668 _ = allocator; 1669 _ = key; 1670 _ = root_focus; 1671 } 1672 1673 pub fn clearGrid(self: *AnsiArt) void { 1674 if (self.grid) |*grid| { 1675 grid.deinit(); 1676 self.grid = null; 1677 } 1678 } 1679 1680 pub fn getGrid(self: AnsiArt) ?Grid { 1681 return self.grid; 1682 } 1683 1684 pub fn getFocus(self: *AnsiArt) *Focus { 1685 return self.focus; 1686 } 1687 1688 fn applySgr(style: *Grid.Style, params: []const u8) void { 1689 var nums: [16]u32 = undefined; 1690 var n: usize = 0; 1691 var it = std.mem.splitScalar(u8, params, ';'); 1692 while (it.next()) |tok| { 1693 if (n >= nums.len) break; 1694 // an empty parameter (e.g. bare "\x1b[m") means 0 1695 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1696 n += 1; 1697 } 1698 if (n == 0) { 1699 style.* = .{}; 1700 return; 1701 } 1702 var i: usize = 0; 1703 while (i < n) : (i += 1) { 1704 switch (nums[i]) { 1705 0 => style.* = .{}, 1706 7 => style.inverted = true, 1707 27 => style.inverted = false, 1708 39 => style.fg = null, 1709 49 => style.bg = null, 1710 38, 48 => { 1711 // truecolor form: 38;2;r;g;b — anything else is ignored 1712 if (i + 4 < n and nums[i + 1] == 2) { 1713 const c = Grid.Color{ 1714 .r = @truncate(nums[i + 2]), 1715 .g = @truncate(nums[i + 3]), 1716 .b = @truncate(nums[i + 4]), 1717 }; 1718 if (nums[i] == 38) style.fg = c else style.bg = c; 1719 i += 4; 1720 } 1721 }, 1722 else => {}, 1723 } 1724 } 1725 } 1726 }; 1727 1728 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1729 pub const AnsiBackground = struct { 1730 grid: ?Grid, 1731 child: *Widget, 1732 art: AnsiArt, 1733 session: *Session, 1734 1735 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1736 var cw = child_widget; 1737 const child = allocator.create(Widget) catch |e| { 1738 cw.deinit(allocator); 1739 return e; 1740 }; 1741 child.* = cw; 1742 errdefer { 1743 child.deinit(allocator); 1744 allocator.destroy(child); 1745 } 1746 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 1747 } 1748 1749 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1750 if (self.grid) |*grid| grid.deinit(); 1751 self.art.deinit(allocator); 1752 self.child.deinit(allocator); 1753 allocator.destroy(self.child); 1754 } 1755 1756 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1757 self.clearGrid(); 1758 1759 // make the wrapped page fill the whole available area 1760 try self.child.build(allocator, .{ 1761 .min_size = .{ 1762 .width = constraint.max_size.width orelse constraint.min_size.width, 1763 .height = constraint.max_size.height orelse constraint.min_size.height, 1764 }, 1765 .max_size = constraint.max_size, 1766 }, root_focus); 1767 1768 if (self.session.data.enable_ansi) { 1769 if (self.child.getGrid()) |fg| { 1770 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1771 } 1772 } 1773 } 1774 1775 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1776 try self.child.input(allocator, key, root_focus); 1777 } 1778 1779 pub fn clearGrid(self: *AnsiBackground) void { 1780 if (self.grid) |*grid| { 1781 grid.deinit(); 1782 self.grid = null; 1783 } 1784 self.child.clearGrid(); 1785 self.art.clearGrid(); 1786 } 1787 1788 pub fn getGrid(self: AnsiBackground) ?Grid { 1789 return self.grid orelse self.child.getGrid(); 1790 } 1791 1792 pub fn getFocus(self: *AnsiBackground) *Focus { 1793 return self.child.getFocus(); 1794 } 1795 1796 // which top corner the art hugs. 1797 const ArtAnchor = enum { top_left, top_right }; 1798 1799 // a foreground cell counts as blank if it's empty or a bare space without styling 1800 fn cellIsBlank(cell: Grid.Cell) bool { 1801 if (cell.rune) |rune| { 1802 return std.mem.eql(u8, rune, " ") and 1803 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1804 } 1805 return true; 1806 } 1807 1808 // the art is dimmed to this fraction of its brightness so foreground text 1809 // stays legible over it 1810 const art_brightness = 45; // percent 1811 1812 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1813 const c = color orelse return null; 1814 return .{ 1815 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1816 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1817 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1818 }; 1819 } 1820 1821 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1822 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1823 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1824 // snap such channels clear of that range so the backdrop doesn't blink or 1825 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1826 // actually render truecolor. 1827 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1828 const c = color orelse return null; 1829 const snap = struct { 1830 fn f(v: u8) u8 { 1831 return if (v >= 1 and v <= 9) 10 else v; 1832 } 1833 }.f; 1834 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1835 } 1836 1837 // composites ANSI art behind a foreground grid 1838 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1839 art.clearGrid(); 1840 try art.build(allocator, .{ 1841 .min_size = .{ .width = null, .height = null }, 1842 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1843 }, root_focus); 1844 1845 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1846 errdefer out.deinit(); 1847 1848 if (art.getGrid()) |art_grid| { 1849 const anchor_x = switch (anchor) { 1850 .top_left => 0, 1851 .top_right => foreground.size.width -| art_grid.size.width, 1852 }; 1853 for (0..art_grid.size.height) |y| { 1854 for (0..art_grid.size.width) |x| { 1855 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1856 if (src.rune == null) continue; 1857 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1858 const dst = &out.cells.items[idx]; 1859 // composite the dimmed art behind every cell so it shows 1860 // through the whole UI: a blank cell becomes the art, while a 1861 // cell with a glyph keeps the glyph but takes the dimmed art as 1862 // its background (replacing whatever background it had, which 1863 // would otherwise obscure the art). 1864 if (cellIsBlank(dst.*)) { 1865 dst.* = src; 1866 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 1867 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 1868 } else { 1869 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 1870 } 1871 } 1872 } 1873 } 1874 return out; 1875 } 1876 }; 1877