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