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