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