╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1076 self.focus.destroy(allocator); 1077 if (self.grid) |*grid| { 1078 grid.deinit(); 1079 self.grid = null; 1080 } 1081 self.child.deinit(allocator); 1082 allocator.destroy(self.child); 1083 } 1084 1085 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1086 self.clearGrid(); 1087 self.getFocus().clear(); 1088 1089 // build the child without forcing it to fill min_size; it sizes 1090 // itself to its content within the available max. 1091 try self.child.build(allocator, .{ 1092 .min_size = .{ .width = null, .height = null }, 1093 .max_size = constraint.max_size, 1094 }, root_focus); 1095 1096 const child_grid = self.child.getGrid() orelse return; 1097 1098 // prefer max when given; otherwise grow to max(min, child) so that 1099 // when only a min is set (e.g. the wasm path passes viewport rows 1100 // as min_height with no max), we can still vertically center while 1101 // letting taller content extend past the min. 1102 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; 1103 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; 1104 if (width == 0 or height == 0) return; 1105 1106 const offset_x: usize = (width -| child_grid.size.width) / 2; 1107 const offset_y: usize = (height -| child_grid.size.height) / 2; 1108 1109 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1110 errdefer grid.deinit(); 1111 try grid.drawGrid(child_grid, offset_x, offset_y); 1112 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1113 self.getFocus().child_id = self.child.getFocus().id; 1114 1115 self.grid = grid; 1116 } 1117 1118 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1119 try self.child.input(allocator, key, root_focus); 1120 } 1121 1122 pub fn clearGrid(self: *Center) void { 1123 if (self.grid) |*grid| { 1124 grid.deinit(); 1125 self.grid = null; 1126 } 1127 self.child.clearGrid(); 1128 } 1129 1130 pub fn getGrid(self: Center) ?Grid { 1131 return self.grid; 1132 } 1133 1134 pub fn getFocus(self: *Center) *Focus { 1135 return self.focus; 1136 } 1137 }; 1138 1139 // renders truecolor ANSI art 1140 pub const AnsiArt = struct { 1141 focus: *Focus, 1142 grid: ?Grid, 1143 content: []const u8, 1144 1145 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1146 return .{ 1147 .focus = try Focus.create(allocator, .container), 1148 .grid = null, 1149 .content = content, 1150 }; 1151 } 1152 1153 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1154 self.focus.destroy(allocator); 1155 if (self.grid) |*grid| { 1156 grid.deinit(); 1157 self.grid = null; 1158 } 1159 } 1160 1161 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1162 _ = root_focus; 1163 self.clearGrid(); 1164 1165 // parse into rows of cells, then copy into a rectangular grid 1166 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1167 defer { 1168 for (rows.items) |*row| row.deinit(allocator); 1169 rows.deinit(allocator); 1170 } 1171 var row: std.ArrayList(Grid.Cell) = .empty; 1172 errdefer row.deinit(allocator); 1173 1174 var style: Grid.Style = .{}; 1175 var width: usize = 0; 1176 const content = self.content; 1177 var i: usize = 0; 1178 while (i < content.len) { 1179 const byte = content[i]; 1180 if (byte == '\n') { 1181 width = @max(width, row.items.len); 1182 try rows.append(allocator, row); 1183 row = .empty; 1184 i += 1; 1185 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1186 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1187 var j = i + 2; 1188 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1189 if (j >= content.len) { 1190 i = content.len; // malformed trailing escape; stop 1191 } else { 1192 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1193 i = j + 1; 1194 } 1195 } else { 1196 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1197 const end = @min(content.len, i + len); 1198 const rune = content[i..end]; 1199 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 1200 // a foreground color does not make a space visible. only an 1201 // effective background makes it opaque; foreground state often 1202 // remains active through the padding at the end of a row. 1203 const effective_background = if (style.inverted) style.fg else style.bg; 1204 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 1205 try row.append(allocator, .{ 1206 .rune = if (transparent) null else codepoint, 1207 .style = style, 1208 }); 1209 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 1210 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 1211 } 1212 i = end; 1213 } 1214 } 1215 // a trailing row with content but no closing newline 1216 if (row.items.len > 0) { 1217 width = @max(width, row.items.len); 1218 try rows.append(allocator, row); 1219 } else { 1220 row.deinit(allocator); 1221 } 1222 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1223 1224 const height = rows.items.len; 1225 if (width == 0 or height == 0) return; 1226 1227 const clamped_w = @min(width, constraint.max_size.width orelse width); 1228 const clamped_h = @min(height, constraint.max_size.height orelse height); 1229 1230 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1231 errdefer grid.deinit(); 1232 for (rows.items[0..clamped_h], 0..) |r, y| { 1233 const n = @min(r.items.len, clamped_w); 1234 for (r.items[0..n], 0..) |cell, x| { 1235 if (cell.continuation) continue; 1236 (try grid.cell(x, y)).style = cell.style; 1237 try grid.setRune(x, y, cell.rune); 1238 } 1239 } 1240 self.grid = grid; 1241 } 1242 1243 pub fn clearGrid(self: *AnsiArt) void { 1244 if (self.grid) |*grid| { 1245 grid.deinit(); 1246 self.grid = null; 1247 } 1248 } 1249 1250 pub fn getGrid(self: AnsiArt) ?Grid { 1251 return self.grid; 1252 } 1253 1254 pub fn getFocus(self: *AnsiArt) *Focus { 1255 return self.focus; 1256 } 1257 1258 fn applySgr(style: *Grid.Style, params: []const u8) void { 1259 var nums: [16]u32 = undefined; 1260 var n: usize = 0; 1261 var it = std.mem.splitScalar(u8, params, ';'); 1262 while (it.next()) |tok| { 1263 if (n >= nums.len) break; 1264 // an empty parameter (e.g. bare "\x1b[m") means 0 1265 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1266 n += 1; 1267 } 1268 if (n == 0) { 1269 style.* = .{}; 1270 return; 1271 } 1272 var i: usize = 0; 1273 while (i < n) : (i += 1) { 1274 switch (nums[i]) { 1275 0 => style.* = .{}, 1276 7 => style.inverted = true, 1277 27 => style.inverted = false, 1278 39 => style.fg = null, 1279 49 => style.bg = null, 1280 38, 48 => { 1281 // truecolor form: 38;2;r;g;b — anything else is ignored 1282 if (i + 4 < n and nums[i + 1] == 2) { 1283 const c = Grid.Color{ 1284 .r = @truncate(nums[i + 2]), 1285 .g = @truncate(nums[i + 3]), 1286 .b = @truncate(nums[i + 4]), 1287 }; 1288 if (nums[i] == 38) style.fg = c else style.bg = c; 1289 i += 4; 1290 } 1291 }, 1292 else => {}, 1293 } 1294 } 1295 } 1296 }; 1297 1298 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1299 pub const AnsiBackground = struct { 1300 grid: ?Grid, 1301 child: *Widget, 1302 art: AnsiArt, 1303 session: *ui.Session, 1304 1305 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1306 var cw = child_widget; 1307 const child = allocator.create(Widget) catch |e| { 1308 cw.deinit(allocator); 1309 return e; 1310 }; 1311 child.* = cw; 1312 errdefer { 1313 child.deinit(allocator); 1314 allocator.destroy(child); 1315 } 1316 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1317 } 1318 1319 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1320 if (self.grid) |*grid| grid.deinit(); 1321 self.art.deinit(allocator); 1322 self.child.deinit(allocator); 1323 allocator.destroy(self.child); 1324 } 1325 1326 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1327 self.clearGrid(); 1328 1329 // make the wrapped page fill the whole available area 1330 try self.child.build(allocator, .{ 1331 .min_size = .{ 1332 .width = constraint.max_size.width orelse constraint.min_size.width, 1333 .height = constraint.max_size.height orelse constraint.min_size.height, 1334 }, 1335 .max_size = constraint.max_size, 1336 }, root_focus); 1337 1338 if (!self.session.data.enable_ansi) return; 1339 const foreground = self.child.getGrid() orelse return; 1340 self.art.content = self.session.data.ansi_art; 1341 try self.buildArt(allocator, foreground.size, root_focus); 1342 if (!self.session.is_terminal) return; 1343 const art_grid = self.art.getGrid() orelse return; 1344 self.grid = try artBehind(allocator, foreground, art_grid); 1345 } 1346 1347 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1348 try self.child.input(allocator, key, root_focus); 1349 } 1350 1351 pub fn clearGrid(self: *AnsiBackground) void { 1352 if (self.grid) |*grid| { 1353 grid.deinit(); 1354 self.grid = null; 1355 } 1356 self.child.clearGrid(); 1357 self.art.clearGrid(); 1358 } 1359 1360 pub fn getGrid(self: AnsiBackground) ?Grid { 1361 return self.grid orelse self.child.getGrid(); 1362 } 1363 1364 pub fn getFocus(self: *AnsiBackground) *Focus { 1365 return self.child.getFocus(); 1366 } 1367 1368 // a foreground cell counts as blank if it's empty or a bare space without styling 1369 fn cellIsBlank(cell: Grid.Cell) bool { 1370 if (cell.continuation) return false; 1371 if (cell.rune) |rune| { 1372 return rune == ' ' and 1373 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1374 } 1375 return true; 1376 } 1377 1378 // the art is dimmed to this fraction of its brightness so foreground text 1379 // stays legible over it 1380 const art_brightness = 35; // percent 1381 1382 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1383 const c = color orelse return null; 1384 return .{ 1385 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1386 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1387 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1388 }; 1389 } 1390 1391 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 1392 try self.art.build(allocator, .{ 1393 .min_size = .{ .width = null, .height = null }, 1394 .max_size = .{ .width = size.width, .height = size.height }, 1395 }, root_focus); 1396 if (self.art.grid) |*grid| { 1397 for (grid.cells) |*cell| { 1398 cell.style.fg = dimColor(cell.style.fg); 1399 cell.style.bg = dimColor(cell.style.bg); 1400 } 1401 } 1402 } 1403 1404 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1405 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1406 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1407 // snap such channels clear of that range so the backdrop doesn't blink or 1408 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1409 // actually render truecolor. 1410 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1411 const c = color orelse return null; 1412 const snap = struct { 1413 fn f(v: u8) u8 { 1414 return if (v >= 1 and v <= 9) 10 else v; 1415 } 1416 }.f; 1417 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1418 } 1419 1420 fn applyArtBackground(dst: *Grid.Cell, src: Grid.Cell) void { 1421 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 1422 dst.style.bg = background_maybe; 1423 // change unstyled text to contrast with the art behind it 1424 if (dst.style.fg == null) { 1425 if (background_maybe) |background| { 1426 // use near-black and near-white, which is easier on the eyes 1427 // while retaining high contrast 1428 const luminance = (@as(u32, background.r) * 299 + 1429 @as(u32, background.g) * 587 + 1430 @as(u32, background.b) * 114) / 1000; 1431 dst.style.fg = if (luminance >= 128) 1432 .{ .r = 16, .g = 16, .b = 16 } 1433 else 1434 .{ .r = 240, .g = 240, .b = 240 }; 1435 } 1436 } 1437 } 1438 1439 // composites ANSI art behind a foreground grid 1440 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1441 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1442 errdefer out.deinit(); 1443 1444 const anchor_x = foreground.size.width -| art_grid.size.width; 1445 for (0..art_grid.size.height) |y| { 1446 for (0..art_grid.size.width) |x| { 1447 const src = (try art_grid.cell(x, y)).*; 1448 if (src.rune == null) continue; 1449 const dst = try out.cell(anchor_x + x, y); 1450 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1451 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1452 // blank cells take the art; occupied cells take its background 1453 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1454 dst.style = src.style; 1455 dst.style.fg = sgrSafe(dst.style.fg); 1456 dst.style.bg = sgrSafe(dst.style.bg); 1457 try out.setRune(anchor_x + x, y, src.rune); 1458 } else { 1459 applyArtBackground(dst, src); 1460 if (next_dst) |next| applyArtBackground(next, src); 1461 } 1462 } 1463 } 1464 return out; 1465 } 1466 }; 1467 1468 pub const CopyableText = struct { 1469 pub const Choice = struct { 1470 selector: []const u8 = "", 1471 text: []const u8, 1472 copyable_text: ?[]const u8 = null, 1473 label: []const u8 = "", 1474 bottom_label: []const u8 = "", 1475 }; 1476 1477 box: wgt.Box(Widget), 1478 session: *ui.Session, 1479 choices: []Choice, 1480 selected: usize = 0, 1481 1482 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1483 if (choices.len == 0) return error.MissingCopyableText; 1484 1485 const owned_choices = try allocator.dupe(Choice, choices); 1486 errdefer allocator.free(owned_choices); 1487 1488 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1489 errdefer box.deinit(allocator); 1490 1491 if (choices.len > 1) for (choices) |choice| { 1492 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 1493 errdefer selector_box.deinit(allocator); 1494 selector_box.getFocus().mode = .all; 1495 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 } }); 1496 }; 1497 1498 var text_input = try wgt.TextInput.init(allocator, .{ 1499 .border_style = .single, 1500 .rounded_corners = true, 1501 .label = choices[0].label, 1502 .bottom_label = choices[0].bottom_label, 1503 .read_only = true, 1504 .render_content = session.is_terminal, 1505 .visible_width = null, 1506 }); 1507 errdefer text_input.deinit(allocator); 1508 text_input.getFocus().mode = .all; 1509 try text_input.setContent(allocator, choices[0].text); 1510 box.getFocus().child_id = text_input.getFocus().id; 1511 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1512 1513 return .{ .box = box, .session = session, .choices = owned_choices }; 1514 } 1515 1516 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1517 const aa = session.page_arena.allocator(); 1518 var choices: [2]Choice = undefined; 1519 var count: usize = 0; 1520 switch (location) { 1521 .repo => |identity| { 1522 if (session.data.git_http_port) |port| { 1523 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1524 choices[count] = .{ 1525 .selector = "http", 1526 .text = url, 1527 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1528 .label = " clone ", 1529 }; 1530 count += 1; 1531 } 1532 if (session.data.git_ssh_port) |port| { 1533 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1534 choices[count] = .{ 1535 .selector = "ssh", 1536 .text = url, 1537 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1538 .label = " clone ", 1539 }; 1540 count += 1; 1541 } 1542 }, 1543 .fork => |f| if (session.data.git_ssh_port) |port| { 1544 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1545 choices[count] = .{ 1546 .text = url, 1547 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1548 .label = " clone ", 1549 }; 1550 count += 1; 1551 }, 1552 } 1553 if (count == 0) return null; 1554 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1555 } 1556 1557 pub fn minWidth(self: *const CopyableText) usize { 1558 var width = maxWidth(self.choices) + 2; 1559 if (self.choices.len > 1) { 1560 for (self.choices) |choice| width += choice.selector.len + 2; 1561 } 1562 return width; 1563 } 1564 1565 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1566 self.box.deinit(allocator); 1567 allocator.free(self.choices); 1568 } 1569 1570 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1571 self.clearGrid(); 1572 1573 if (self.choices.len > 1) { 1574 if (root_focus.grandchild_id) |id| { 1575 for (0..self.choices.len) |i| { 1576 if (id != self.selector(i).getFocus().id) continue; 1577 if (i != self.selected) try self.select(allocator, i); 1578 root_focus.setFocus(self.textInput().getFocus().id); 1579 break; 1580 } 1581 } 1582 1583 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1584 for (0..self.choices.len) |i| { 1585 self.selector(i).options.border_style = if (i == self.selected) 1586 (if (focused) .double else .single) 1587 else 1588 .hidden; 1589 } 1590 } 1591 self.textInput().options.border_style = .single; 1592 self.textInput().cursor = 0; 1593 self.textInput().scroll_offset = 0; 1594 try self.box.build(allocator, constraint, root_focus); 1595 1596 if (!self.session.is_terminal) { 1597 const aa = self.session.arena.allocator(); 1598 const id = self.textInput().getFocus().id; 1599 try self.session.text_inputs.put(aa, id, self.textInput()); 1600 } 1601 } 1602 1603 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1604 switch (key) { 1605 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1606 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1607 .enter => if (self.session.is_terminal) self.show(), 1608 .mouse => |mouse| { 1609 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1610 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1611 try self.select(allocator, i); 1612 root_focus.setFocus(self.textInput().getFocus().id); 1613 return; 1614 }; 1615 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1616 }, 1617 else => {}, 1618 } 1619 } 1620 1621 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1622 self.selected = selected; 1623 const choice = self.choices[selected]; 1624 const text_input = self.textInput(); 1625 text_input.options.label = choice.label; 1626 text_input.options.bottom_label = choice.bottom_label; 1627 try text_input.setContent(allocator, choice.text); 1628 } 1629 1630 fn show(self: *CopyableText) void { 1631 const choice = self.choices[self.selected]; 1632 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1633 } 1634 1635 fn textInput(self: *CopyableText) *wgt.TextInput { 1636 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1637 } 1638 1639 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1640 return &self.box.children.values()[index].widget.text_box; 1641 } 1642 1643 fn maxWidth(choices: []const Choice) usize { 1644 var width: usize = 0; 1645 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1646 return width; 1647 } 1648 1649 pub fn clearGrid(self: *CopyableText) void { 1650 self.box.clearGrid(); 1651 } 1652 1653 pub fn getGrid(self: CopyableText) ?Grid { 1654 return self.box.getGrid(); 1655 } 1656 1657 pub fn getFocus(self: *CopyableText) *Focus { 1658 return self.box.getFocus(); 1659 } 1660 }; 1661