╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
936 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 937 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 938 const slot_height = self.options.cell_height + border_rows; 939 const col = index % self.last_cols; 940 const row = index / self.last_cols; 941 return .{ 942 .x = @intCast(col * self.options.cell_width), 943 .y = @intCast(row * slot_height), 944 .size = .{ .width = self.options.cell_width, .height = slot_height }, 945 }; 946 } 947 948 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 949 for (self.text_boxes.items, 0..) |tb, i| { 950 if (tb.box.focus.id == focus_id) return i; 951 } 952 return null; 953 } 954 955 pub const Scroll = struct { 956 scroll: wgt.Scroll(Widget), 957 958 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options) !Scroll { 959 var layout_inner = try FlowBox.init(allocator, options); 960 errdefer layout_inner.deinit(allocator); 961 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert }); 962 errdefer scroll.deinit(allocator); 963 return .{ .scroll = scroll }; 964 } 965 966 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 967 self.scroll.deinit(allocator); 968 } 969 970 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const []const u8, links: ?[]const []const u8) !void { 971 self.scroll.x = 0; 972 self.scroll.y = 0; 973 try self.inner().setItems(allocator, items, links); 974 } 975 976 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 977 try self.scroll.build(allocator, constraint, root_focus); 978 } 979 980 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 981 _ = allocator; 982 const in = self.inner(); 983 const child_id = in.focus.child_id orelse return; 984 const current_index = in.indexOfFocusId(child_id) orelse return; 985 const count = in.text_boxes.items.len; 986 if (count == 0) return; 987 const cols = in.last_cols; 988 const slot_height = in.options.cell_height + FlowBox.border_rows; 989 990 var index = current_index; 991 switch (key) { 992 .arrow_up => index -|= cols, 993 .arrow_down => if (index + cols < count) { 994 index += cols; 995 }, 996 .arrow_left => index -|= 1, 997 .arrow_right => if (index + 1 < count) { 998 index += 1; 999 }, 1000 .home => index = 0, 1001 .end => index = count - 1, 1002 .page_up => { 1003 if (self.scroll.grid) |grid| if (slot_height > 0) { 1004 const rows_per_page = grid.size.height / slot_height; 1005 index -|= rows_per_page * cols; 1006 }; 1007 }, 1008 .page_down => { 1009 if (self.scroll.grid) |grid| if (slot_height > 0) { 1010 const rows_per_page = grid.size.height / slot_height; 1011 index = @min(index + rows_per_page * cols, count - 1); 1012 }; 1013 }, 1014 // scroll wheel moves the focused cell by a full row so the 1015 // viewport (via scrollToRect) follows in row-sized steps, 1016 // matching how a scroll wheel feels in a grid view. 1017 .mouse => |mouse| switch (mouse.action) { 1018 .scroll => |dir| switch (dir) { 1019 .up => index -|= cols, 1020 .down => if (index + cols < count) { 1021 index += cols; 1022 }, 1023 }, 1024 else => {}, 1025 }, 1026 else => {}, 1027 } 1028 1029 if (index != current_index) { 1030 try root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 1031 if (in.cellRect(index)) |rect| { 1032 self.scroll.scrollToRect(rect); 1033 } 1034 } 1035 } 1036 1037 pub fn clearGrid(self: *Scroll) void { 1038 self.scroll.clearGrid(); 1039 } 1040 1041 pub fn getGrid(self: Scroll) ?Grid { 1042 return self.scroll.getGrid(); 1043 } 1044 1045 pub fn getFocus(self: *Scroll) *Focus { 1046 return self.scroll.getFocus(); 1047 } 1048 1049 pub fn getSelectedIndex(self: Scroll) ?usize { 1050 const in = self.scroll.child.flow_box; 1051 const child_id = in.focus.child_id orelse return null; 1052 return in.indexOfFocusId(child_id); 1053 } 1054 1055 fn inner(self: *Scroll) *FlowBox { 1056 return &self.scroll.child.flow_box; 1057 } 1058 }; 1059 }; 1060 1061 // an invisible widget that fills the horizontal space granted by its parent. 1062 // used inside Box(horiz) with a min_size so the box reserves space for the 1063 // children that follow, pushing them to the right. 1064 pub const Spacer = struct { 1065 focus: *Focus, 1066 grid: ?Grid, 1067 1068 pub fn init(allocator: std.mem.Allocator) !Spacer { 1069 return .{ 1070 .focus = try Focus.create(allocator, .container), 1071 .grid = null, 1072 }; 1073 } 1074 1075 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1076 self.focus.destroy(allocator); 1077 if (self.grid) |*grid| { 1078 grid.deinit(); 1079 self.grid = null; 1080 } 1081 } 1082 1083 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1084 _ = root_focus; 1085 self.clearGrid(); 1086 const width = constraint.max_size.width orelse return; 1087 if (width == 0) return; 1088 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1089 } 1090 1091 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1092 _ = self; 1093 _ = allocator; 1094 _ = key; 1095 _ = root_focus; 1096 } 1097 1098 pub fn clearGrid(self: *Spacer) void { 1099 if (self.grid) |*grid| { 1100 grid.deinit(); 1101 self.grid = null; 1102 } 1103 } 1104 1105 pub fn getGrid(self: Spacer) ?Grid { 1106 return self.grid; 1107 } 1108 1109 pub fn getFocus(self: *Spacer) *Focus { 1110 return self.focus; 1111 } 1112 }; 1113 1114 // a single-child wrapper that builds the child at its natural size and 1115 // positions its grid in the middle of the area granted by the parent 1116 pub const Center = struct { 1117 focus: *Focus, 1118 grid: ?Grid, 1119 child: *Widget, 1120 direction: Direction, 1121 1122 pub const Direction = enum { both, horiz, vert }; 1123 1124 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, direction: Direction) !Center { 1125 const child = try allocator.create(Widget); 1126 errdefer allocator.destroy(child); 1127 child.* = child_widget; 1128 return .{ 1129 .focus = try Focus.create(allocator, .container), 1130 .grid = null, 1131 .child = child, 1132 .direction = direction, 1133 }; 1134 } 1135 1136 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1137 self.focus.destroy(allocator); 1138 if (self.grid) |*grid| { 1139 grid.deinit(); 1140 self.grid = null; 1141 } 1142 self.child.deinit(allocator); 1143 allocator.destroy(self.child); 1144 } 1145 1146 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1147 self.clearGrid(); 1148 self.getFocus().clear(); 1149 1150 // build the child without forcing it to fill min_size; it sizes 1151 // itself to its content within the available max. 1152 try self.child.build(allocator, .{ 1153 .min_size = .{ .width = null, .height = null }, 1154 .max_size = constraint.max_size, 1155 }, root_focus); 1156 1157 const child_grid = self.child.getGrid() orelse return; 1158 1159 // prefer max when given; otherwise grow to max(min, child) so that 1160 // when only a min is set (e.g. the wasm path passes viewport rows 1161 // as min_height with no max), we can still vertically center while 1162 // letting taller content extend past the min. 1163 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; 1164 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; 1165 if (width == 0 or height == 0) return; 1166 1167 const offset_x: usize = if (self.direction == .horiz or self.direction == .both) 1168 (width -| child_grid.size.width) / 2 1169 else 1170 0; 1171 const offset_y: usize = if (self.direction == .vert or self.direction == .both) 1172 (height -| child_grid.size.height) / 2 1173 else 1174 0; 1175 1176 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1177 errdefer grid.deinit(); 1178 try grid.drawGrid(child_grid, offset_x, offset_y); 1179 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1180 self.getFocus().child_id = self.child.getFocus().id; 1181 1182 self.grid = grid; 1183 } 1184 1185 pub fn input(self: *Center, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1186 try self.child.input(allocator, key, root_focus); 1187 } 1188 1189 pub fn clearGrid(self: *Center) void { 1190 if (self.grid) |*grid| { 1191 grid.deinit(); 1192 self.grid = null; 1193 } 1194 self.child.clearGrid(); 1195 } 1196 1197 pub fn getGrid(self: Center) ?Grid { 1198 return self.grid; 1199 } 1200 1201 pub fn getFocus(self: *Center) *Focus { 1202 return self.focus; 1203 } 1204 }; 1205 1206 // renders truecolor ANSI art 1207 pub const AnsiArt = struct { 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