╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1049 var n: usize = 0; 1050 var it = std.mem.splitScalar(u8, params, ';'); 1051 while (it.next()) |tok| { 1052 if (n >= nums.len) break; 1053 // an empty parameter (e.g. bare "\x1b[m") means 0 1054 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1055 n += 1; 1056 } 1057 if (n == 0) { 1058 style.* = .{}; 1059 return; 1060 } 1061 var i: usize = 0; 1062 while (i < n) : (i += 1) { 1063 switch (nums[i]) { 1064 0 => style.* = .{}, 1065 7 => style.inverted = true, 1066 27 => style.inverted = false, 1067 39 => style.fg = null, 1068 49 => style.bg = null, 1069 38, 48 => { 1070 // truecolor form: 38;2;r;g;b — anything else is ignored 1071 if (i + 4 < n and nums[i + 1] == 2) { 1072 const c = Grid.Color{ 1073 .r = @truncate(nums[i + 2]), 1074 .g = @truncate(nums[i + 3]), 1075 .b = @truncate(nums[i + 4]), 1076 }; 1077 if (nums[i] == 38) style.fg = c else style.bg = c; 1078 i += 4; 1079 } 1080 }, 1081 else => {}, 1082 } 1083 } 1084 } 1085 }; 1086 1087 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1088 pub const AnsiBackground = struct { 1089 grid: ?Grid, 1090 child: *Widget, 1091 art: AnsiArt, 1092 session: *ui.Session, 1093 1094 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1095 var cw = child_widget; 1096 const child = allocator.create(Widget) catch |e| { 1097 cw.deinit(allocator); 1098 return e; 1099 }; 1100 child.* = cw; 1101 errdefer { 1102 child.deinit(allocator); 1103 allocator.destroy(child); 1104 } 1105 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1106 } 1107 1108 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1109 if (self.grid) |*grid| grid.deinit(); 1110 self.art.deinit(allocator); 1111 self.child.deinit(allocator); 1112 allocator.destroy(self.child); 1113 } 1114 1115 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1116 self.clearGrid(); 1117 1118 // make the wrapped page fill the whole available area 1119 try self.child.build(allocator, .{ 1120 .min_size = .{ 1121 .width = constraint.max_size.width orelse constraint.min_size.width, 1122 .height = constraint.max_size.height orelse constraint.min_size.height, 1123 }, 1124 .max_size = constraint.max_size, 1125 }, root_focus); 1126 1127 if (!self.session.data.enable_ansi) return; 1128 const foreground = self.child.getGrid() orelse return; 1129 self.art.content = self.session.data.ansi_art; 1130 try self.buildArt(allocator, foreground.size, root_focus); 1131 if (!self.session.is_terminal) return; 1132 const art_grid = self.art.getGrid() orelse return; 1133 self.grid = try artBehind(allocator, foreground, art_grid); 1134 } 1135 1136 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1137 try self.child.input(allocator, key, root_focus); 1138 } 1139 1140 pub fn clearGrid(self: *AnsiBackground) void { 1141 if (self.grid) |*grid| { 1142 grid.deinit(); 1143 self.grid = null; 1144 } 1145 self.child.clearGrid(); 1146 self.art.clearGrid(); 1147 } 1148 1149 pub fn getGrid(self: AnsiBackground) ?Grid { 1150 return self.grid orelse self.child.getGrid(); 1151 } 1152 1153 pub fn getFocus(self: *AnsiBackground) *Focus { 1154 return self.child.getFocus(); 1155 } 1156 1157 // a foreground cell counts as blank if it's empty or a bare space without styling 1158 fn cellIsBlank(cell: Grid.Cell) bool { 1159 if (cell.continuation) return false; 1160 if (cell.rune) |rune| { 1161 return rune == ' ' and 1162 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1163 } 1164 return true; 1165 } 1166 1167 // the art is dimmed to this fraction of its brightness so foreground text 1168 // stays legible over it 1169 const art_brightness = 35; // percent 1170 1171 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1172 const c = color orelse return null; 1173 return .{ 1174 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1175 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1176 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1177 }; 1178 } 1179 1180 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 1181 try self.art.build(allocator, .{ 1182 .min_size = .{ .width = null, .height = null }, 1183 .max_size = .{ .width = size.width, .height = size.height }, 1184 }, root_focus); 1185 if (self.art.grid) |*grid| { 1186 for (grid.cells) |*cell| { 1187 cell.style.fg = dimColor(cell.style.fg); 1188 cell.style.bg = dimColor(cell.style.bg); 1189 } 1190 } 1191 } 1192 1193 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1194 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1195 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1196 // snap such channels clear of that range so the backdrop doesn't blink or 1197 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1198 // actually render truecolor. 1199 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1200 const c = color orelse return null; 1201 const snap = struct { 1202 fn f(v: u8) u8 { 1203 return if (v >= 1 and v <= 9) 10 else v; 1204 } 1205 }.f; 1206 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1207 } 1208 1209 fn applyArtBackground(dst: *Grid.Cell, src: Grid.Cell) void { 1210 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 1211 dst.style.bg = background_maybe; 1212 // change unstyled text to contrast with the art behind it 1213 if (dst.style.fg == null) { 1214 if (background_maybe) |background| { 1215 // use near-black and near-white, which is easier on the eyes 1216 // while retaining high contrast 1217 const luminance = (@as(u32, background.r) * 299 + 1218 @as(u32, background.g) * 587 + 1219 @as(u32, background.b) * 114) / 1000; 1220 dst.style.fg = if (luminance >= 128) 1221 .{ .r = 16, .g = 16, .b = 16 } 1222 else 1223 .{ .r = 240, .g = 240, .b = 240 }; 1224 } 1225 } 1226 } 1227 1228 // composites ANSI art behind a foreground grid 1229 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1230 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1231 errdefer out.deinit(); 1232 1233 const anchor_x = foreground.size.width -| art_grid.size.width; 1234 for (0..art_grid.size.height) |y| { 1235 for (0..art_grid.size.width) |x| { 1236 const src = (try art_grid.cell(x, y)).*; 1237 if (src.rune == null) continue; 1238 const dst = try out.cell(anchor_x + x, y); 1239 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1240 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1241 // blank cells take the art; occupied cells take its background 1242 if (cellIsBlank(dst.*) and (next_dst == null or cellIsBlank(next_dst.?.*))) { 1243 dst.style = src.style; 1244 dst.style.fg = sgrSafe(dst.style.fg); 1245 dst.style.bg = sgrSafe(dst.style.bg); 1246 try out.setRune(anchor_x + x, y, src.rune); 1247 } else { 1248 applyArtBackground(dst, src); 1249 if (next_dst) |next| applyArtBackground(next, src); 1250 } 1251 } 1252 } 1253 return out; 1254 } 1255 }; 1256 1257 pub const CopyableText = struct { 1258 pub const Choice = struct { 1259 selector: []const u8 = "", 1260 text: []const u8, 1261 copyable_text: ?[]const u8 = null, 1262 label: []const u8 = "", 1263 bottom_label: []const u8 = "", 1264 }; 1265 1266 box: wgt.Box(Widget), 1267 session: *ui.Session, 1268 choices: []Choice, 1269 selected: usize = 0, 1270 1271 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1272 if (choices.len == 0) return error.MissingCopyableText; 1273 1274 const owned_choices = try allocator.dupe(Choice, choices); 1275 errdefer allocator.free(owned_choices); 1276 1277 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1278 errdefer box.deinit(allocator); 1279 1280 if (choices.len > 1) for (choices) |choice| { 1281 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .wrap_kind = .none }); 1282 errdefer selector_box.deinit(allocator); 1283 selector_box.getFocus().focusable = true; 1284 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 } }); 1285 }; 1286 1287 var text_input = try wgt.TextInput.init(allocator, .{ 1288 .border_style = .single, 1289 .label = choices[0].label, 1290 .bottom_label = choices[0].bottom_label, 1291 .read_only = true, 1292 .render_content = session.is_terminal, 1293 .visible_width = null, 1294 }); 1295 errdefer text_input.deinit(allocator); 1296 text_input.getFocus().focusable = true; 1297 try text_input.setContent(allocator, choices[0].text); 1298 box.getFocus().child_id = text_input.getFocus().id; 1299 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1300 1301 return .{ .box = box, .session = session, .choices = owned_choices }; 1302 } 1303 1304 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1305 const aa = session.page_arena.allocator(); 1306 var choices: [2]Choice = undefined; 1307 var count: usize = 0; 1308 switch (location) { 1309 .repo => |identity| { 1310 if (session.data.git_http_port) |port| { 1311 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1312 choices[count] = .{ 1313 .selector = "http", 1314 .text = url, 1315 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1316 .label = " clone ", 1317 }; 1318 count += 1; 1319 } 1320 if (session.data.git_ssh_port) |port| { 1321 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1322 choices[count] = .{ 1323 .selector = "ssh", 1324 .text = url, 1325 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1326 .label = " clone ", 1327 }; 1328 count += 1; 1329 } 1330 }, 1331 .fork => |f| if (session.data.git_ssh_port) |port| { 1332 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1333 choices[count] = .{ 1334 .text = url, 1335 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1336 .label = " clone ", 1337 }; 1338 count += 1; 1339 }, 1340 } 1341 if (count == 0) return null; 1342 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1343 } 1344 1345 pub fn minWidth(self: *const CopyableText) usize { 1346 var width = maxWidth(self.choices) + 2; 1347 if (self.choices.len > 1) { 1348 for (self.choices) |choice| width += choice.selector.len + 2; 1349 } 1350 return width; 1351 } 1352 1353 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1354 self.box.deinit(allocator); 1355 allocator.free(self.choices); 1356 } 1357 1358 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1359 self.clearGrid(); 1360 1361 if (self.choices.len > 1) { 1362 if (root_focus.grandchild_id) |id| { 1363 for (0..self.choices.len) |i| { 1364 if (id != self.selector(i).getFocus().id) continue; 1365 if (i != self.selected) try self.select(allocator, i); 1366 root_focus.setFocus(self.textInput().getFocus().id); 1367 break; 1368 } 1369 } 1370 1371 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1372 for (0..self.choices.len) |i| { 1373 self.selector(i).options.border_style = if (i == self.selected) 1374 (if (focused) .double else .single) 1375 else 1376 .hidden; 1377 } 1378 } 1379 self.textInput().options.border_style = .single; 1380 self.textInput().cursor = 0; 1381 self.textInput().scroll_offset = 0; 1382 try self.box.build(allocator, constraint, root_focus); 1383 1384 if (!self.session.is_terminal) { 1385 const aa = self.session.arena.allocator(); 1386 const id = self.textInput().getFocus().id; 1387 try self.session.text_inputs.put(aa, id, self.textInput()); 1388 } 1389 } 1390 1391 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1392 switch (key) { 1393 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1394 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1395 .enter => if (self.session.is_terminal) self.show(), 1396 .mouse => |mouse| { 1397 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1398 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1399 try self.select(allocator, i); 1400 root_focus.setFocus(self.textInput().getFocus().id); 1401 return; 1402 }; 1403 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1404 }, 1405 else => {}, 1406 } 1407 } 1408 1409 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1410 self.selected = selected; 1411 const choice = self.choices[selected]; 1412 const text_input = self.textInput(); 1413 text_input.options.label = choice.label; 1414 text_input.options.bottom_label = choice.bottom_label; 1415 try text_input.setContent(allocator, choice.text); 1416 } 1417 1418 fn show(self: *CopyableText) void { 1419 const choice = self.choices[self.selected]; 1420 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1421 } 1422 1423 fn textInput(self: *CopyableText) *wgt.TextInput { 1424 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1425 } 1426 1427 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1428 return &self.box.children.values()[index].widget.text_box; 1429 } 1430 1431 fn maxWidth(choices: []const Choice) usize { 1432 var width: usize = 0; 1433 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1434 return width; 1435 } 1436 1437 pub fn clearGrid(self: *CopyableText) void { 1438 self.box.clearGrid(); 1439 } 1440 1441 pub fn getGrid(self: CopyableText) ?Grid { 1442 return self.box.getGrid(); 1443 } 1444 1445 pub fn getFocus(self: *CopyableText) *Focus { 1446 return self.box.getFocus(); 1447 } 1448 }; 1449