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