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