╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1208 focus: *Focus, 1209 grid: ?Grid, 1210 content: []const u8, 1211 1212 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1213 return .{ 1214 .focus = try Focus.create(allocator, .container), 1215 .grid = null, 1216 .content = content, 1217 }; 1218 } 1219 1220 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1221 self.focus.destroy(allocator); 1222 if (self.grid) |*grid| { 1223 grid.deinit(); 1224 self.grid = null; 1225 } 1226 } 1227 1228 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1229 _ = root_focus; 1230 self.clearGrid(); 1231 1232 // parse into rows of cells, then copy into a rectangular grid 1233 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1234 defer { 1235 for (rows.items) |*row| row.deinit(allocator); 1236 rows.deinit(allocator); 1237 } 1238 var row: std.ArrayList(Grid.Cell) = .empty; 1239 errdefer row.deinit(allocator); 1240 1241 var style: Grid.Style = .{}; 1242 var width: usize = 0; 1243 const content = self.content; 1244 var i: usize = 0; 1245 while (i < content.len) { 1246 const byte = content[i]; 1247 if (byte == '\n') { 1248 width = @max(width, row.items.len); 1249 try rows.append(allocator, row); 1250 row = .empty; 1251 i += 1; 1252 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1253 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1254 var j = i + 2; 1255 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1256 if (j >= content.len) { 1257 i = content.len; // malformed trailing escape; stop 1258 } else { 1259 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1260 i = j + 1; 1261 } 1262 } else { 1263 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1264 const end = @min(content.len, i + len); 1265 const rune = content[i..end]; 1266 const transparent = end - i == 1 and rune[0] == ' ' and 1267 style.fg == null and style.bg == null and !style.inverted; 1268 try row.append(allocator, .{ 1269 .rune = if (transparent) null else rune, 1270 .style = style, 1271 }); 1272 i = end; 1273 } 1274 } 1275 // a trailing row with content but no closing newline 1276 if (row.items.len > 0) { 1277 width = @max(width, row.items.len); 1278 try rows.append(allocator, row); 1279 } else { 1280 row.deinit(allocator); 1281 } 1282 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1283 1284 const height = rows.items.len; 1285 if (width == 0 or height == 0) return; 1286 1287 const clamped_w = @min(width, constraint.max_size.width orelse width); 1288 const clamped_h = @min(height, constraint.max_size.height orelse height); 1289 1290 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1291 errdefer grid.deinit(); 1292 for (rows.items[0..clamped_h], 0..) |r, y| { 1293 const n = @min(r.items.len, clamped_w); 1294 for (r.items[0..n], 0..) |cell, x| { 1295 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1296 } 1297 } 1298 self.grid = grid; 1299 } 1300 1301 pub fn input(self: *AnsiArt, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1302 _ = self; 1303 _ = allocator; 1304 _ = key; 1305 _ = root_focus; 1306 } 1307 1308 pub fn clearGrid(self: *AnsiArt) void { 1309 if (self.grid) |*grid| { 1310 grid.deinit(); 1311 self.grid = null; 1312 } 1313 } 1314 1315 pub fn getGrid(self: AnsiArt) ?Grid { 1316 return self.grid; 1317 } 1318 1319 pub fn getFocus(self: *AnsiArt) *Focus { 1320 return self.focus; 1321 } 1322 1323 fn applySgr(style: *Grid.Style, params: []const u8) void { 1324 var nums: [16]u32 = undefined; 1325 var n: usize = 0; 1326 var it = std.mem.splitScalar(u8, params, ';'); 1327 while (it.next()) |tok| { 1328 if (n >= nums.len) break; 1329 // an empty parameter (e.g. bare "\x1b[m") means 0 1330 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1331 n += 1; 1332 } 1333 if (n == 0) { 1334 style.* = .{}; 1335 return; 1336 } 1337 var i: usize = 0; 1338 while (i < n) : (i += 1) { 1339 switch (nums[i]) { 1340 0 => style.* = .{}, 1341 7 => style.inverted = true, 1342 27 => style.inverted = false, 1343 39 => style.fg = null, 1344 49 => style.bg = null, 1345 38, 48 => { 1346 // truecolor form: 38;2;r;g;b — anything else is ignored 1347 if (i + 4 < n and nums[i + 1] == 2) { 1348 const c = Grid.Color{ 1349 .r = @truncate(nums[i + 2]), 1350 .g = @truncate(nums[i + 3]), 1351 .b = @truncate(nums[i + 4]), 1352 }; 1353 if (nums[i] == 38) style.fg = c else style.bg = c; 1354 i += 4; 1355 } 1356 }, 1357 else => {}, 1358 } 1359 } 1360 } 1361 }; 1362 1363 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1364 pub const AnsiBackground = struct { 1365 grid: ?Grid, 1366 child: *Widget, 1367 art: AnsiArt, 1368 session: *Session, 1369 1370 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1371 var cw = child_widget; 1372 const child = allocator.create(Widget) catch |e| { 1373 cw.deinit(allocator); 1374 return e; 1375 }; 1376 child.* = cw; 1377 errdefer { 1378 child.deinit(allocator); 1379 allocator.destroy(child); 1380 } 1381 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 1382 } 1383 1384 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1385 if (self.grid) |*grid| grid.deinit(); 1386 self.art.deinit(allocator); 1387 self.child.deinit(allocator); 1388 allocator.destroy(self.child); 1389 } 1390 1391 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1392 self.clearGrid(); 1393 1394 // make the wrapped page fill the whole available area 1395 try self.child.build(allocator, .{ 1396 .min_size = .{ 1397 .width = constraint.max_size.width orelse constraint.min_size.width, 1398 .height = constraint.max_size.height orelse constraint.min_size.height, 1399 }, 1400 .max_size = constraint.max_size, 1401 }, root_focus); 1402 1403 if (self.session.data.enable_ansi) { 1404 if (self.child.getGrid()) |fg| { 1405 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1406 } 1407 } 1408 } 1409 1410 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1411 try self.child.input(allocator, key, root_focus); 1412 } 1413 1414 pub fn clearGrid(self: *AnsiBackground) void { 1415 if (self.grid) |*grid| { 1416 grid.deinit(); 1417 self.grid = null; 1418 } 1419 self.child.clearGrid(); 1420 self.art.clearGrid(); 1421 } 1422 1423 pub fn getGrid(self: AnsiBackground) ?Grid { 1424 return self.grid orelse self.child.getGrid(); 1425 } 1426 1427 pub fn getFocus(self: *AnsiBackground) *Focus { 1428 return self.child.getFocus(); 1429 } 1430 1431 // which top corner the art hugs. 1432 const ArtAnchor = enum { top_left, top_right }; 1433 1434 // a foreground cell counts as blank if it's empty or a bare space without styling 1435 fn cellIsBlank(cell: Grid.Cell) bool { 1436 if (cell.rune) |rune| { 1437 return std.mem.eql(u8, rune, " ") and 1438 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1439 } 1440 return true; 1441 } 1442 1443 // a single representative color for an art cell 1444 fn artCellColor(style: Grid.Style) ?Grid.Color { 1445 const f = style.fg orelse return style.bg; 1446 const b = style.bg orelse return f; 1447 return .{ 1448 .r = @intCast((@as(u16, f.r) + b.r) / 2), 1449 .g = @intCast((@as(u16, f.g) + b.g) / 2), 1450 .b = @intCast((@as(u16, f.b) + b.b) / 2), 1451 }; 1452 } 1453 1454 // behind a visible glyph the art color is dimmed to this fraction of its 1455 // brightness so light terminal text stays legible on top of it 1456 const glyph_bg_brightness = 45; // percent 1457 1458 // a glyph sitting over the art also gets its foreground pinned to this color 1459 // (unless it set its own), so the text stays legible 1460 const glyph_fg_over_art = Grid.Color{ .r = 235, .g = 235, .b = 235 }; 1461 1462 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1463 const c = color orelse return null; 1464 return .{ 1465 .r = @intCast(@as(u16, c.r) * glyph_bg_brightness / 100), 1466 .g = @intCast(@as(u16, c.g) * glyph_bg_brightness / 100), 1467 .b = @intCast(@as(u16, c.b) * glyph_bg_brightness / 100), 1468 }; 1469 } 1470 1471 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1472 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1473 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1474 // snap such channels clear of that range so the backdrop doesn't blink or 1475 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1476 // actually render truecolor. 1477 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1478 const c = color orelse return null; 1479 const snap = struct { 1480 fn f(v: u8) u8 { 1481 return if (v >= 1 and v <= 9) 10 else v; 1482 } 1483 }.f; 1484 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1485 } 1486 1487 // composites ANSI art behind a foreground grid 1488 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1489 art.clearGrid(); 1490 try art.build(allocator, .{ 1491 .min_size = .{ .width = null, .height = null }, 1492 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1493 }, root_focus); 1494 1495 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1496 errdefer out.deinit(); 1497 1498 if (art.getGrid()) |art_grid| { 1499 const anchor_x = switch (anchor) { 1500 .top_left => 0, 1501 .top_right => foreground.size.width -| art_grid.size.width, 1502 }; 1503 for (0..art_grid.size.height) |y| { 1504 for (0..art_grid.size.width) |x| { 1505 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1506 if (src.rune == null) continue; 1507 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1508 const dst = &out.cells.items[idx]; 1509 if (cellIsBlank(dst.*)) { 1510 dst.* = src; 1511 dst.style.fg = sgrSafe(dst.style.fg); 1512 dst.style.bg = sgrSafe(dst.style.bg); 1513 } else if (dst.style.bg == null) { 1514 dst.style.bg = sgrSafe(dimColor(artCellColor(src.style))); 1515 if (dst.style.fg == null) dst.style.fg = glyph_fg_over_art; 1516 } 1517 } 1518 } 1519 } 1520 return out; 1521 } 1522 }; 1523