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