╭ CloneUrl.zig ─────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const ui = @import("../ui.zig"); 4 const xit = @import("xit"); 5 const xitui = xit.xitui; 6 const wgt = xitui.widget; 7 const layout = xitui.layout; 8 const Key = xitui.input.Key; 9 const Grid = xitui.grid.Grid; 10 const Focus = xitui.focus.Focus; 11 const inp = @import("input.zig"); 12 13 const debug_ssh_prefix = "GIT_SSH_COMMAND='ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR' \\\r\n"; 14 15 pub const View = struct { 16 box: wgt.Box(ui.Widget), 17 session: *ui.Session, 18 protocols: [2]Protocol, 19 protocol_count: usize, 20 selected: usize = 0, 21 22 const Protocol = struct { 23 name: []const u8, 24 text: []const u8, 25 copyable_text: []const u8, 26 }; 27 28 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, identity: []const u8) !View { 29 const aa = session.page_arena.allocator(); 30 var protocols: [2]Protocol = undefined; 31 var protocol_count: usize = 0; 32 if (session.data.clone_http_port) |port| { 33 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 34 protocols[protocol_count] = .{ 35 .name = "http", 36 .text = url, 37 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 38 }; 39 protocol_count += 1; 40 } 41 if (session.data.clone_ssh_port) |port| { 42 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 43 protocols[protocol_count] = .{ 44 .name = "ssh", 45 .text = url, 46 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ if (builtin.mode == .Debug) debug_ssh_prefix else "", url }), 47 }; 48 protocol_count += 1; 49 } 50 return initProtocols(allocator, session, protocols, protocol_count, " clone "); 51 } 52 53 pub fn initPush(allocator: std.mem.Allocator, session: *ui.Session, identity: []const u8, id: []const u8, branch: []const u8) !View { 54 const aa = session.page_arena.allocator(); 55 var protocols: [2]Protocol = undefined; 56 var protocol_count: usize = 0; 57 if (session.data.clone_http_port) |port| { 58 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}/patch:{s}/branch:{s}", .{ port, identity, id, branch }); 59 const command = try std.fmt.allocPrint(aa, "git push {s} HEAD:patch", .{url}); 60 protocols[protocol_count] = .{ .name = "http", .text = command, .copyable_text = command }; 61 protocol_count += 1; 62 } 63 if (session.data.clone_ssh_port) |port| { 64 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}/patch:{s}/branch:{s}", .{ port, identity, id, branch }); 65 const command = try std.fmt.allocPrint(aa, "git push {s} HEAD:patch", .{url}); 66 protocols[protocol_count] = .{ 67 .name = "ssh", 68 .text = command, 69 .copyable_text = if (builtin.mode == .Debug) try std.fmt.allocPrint(aa, "{s}{s}", .{ debug_ssh_prefix, command }) else command, 70 }; 71 protocol_count += 1; 72 } 73 return initProtocols(allocator, session, protocols, protocol_count, " push "); 74 } 75 76 fn initProtocols(allocator: std.mem.Allocator, session: *ui.Session, protocols: [2]Protocol, protocol_count: usize, input_label: []const u8) !View { 77 if (protocol_count == 0) return error.MissingClonePort; 78 79 var max_text_len: usize = 0; 80 for (protocols[0..protocol_count]) |protocol| max_text_len = @max(max_text_len, protocol.text.len); 81 82 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 83 errdefer box.deinit(allocator); 84 box.getFocus().kind = .{ .custom = "form:" }; 85 86 for (protocols[0..protocol_count]) |protocol| { 87 var label = try wgt.TextBox(ui.Widget).init(allocator, protocol.name, .{ .border_style = .hidden, .wrap_kind = .none }); 88 errdefer label.deinit(allocator); 89 label.getFocus().focusable = true; 90 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .text_box = label }, .rect = null, .min_size = .{ .width = protocol.name.len + 2, .height = 3 } }); 91 } 92 93 var text_input = try wgt.TextInput(ui.Widget).init(allocator, .{ 94 .border_style = .single, 95 .label = input_label, 96 .render_content = session.is_terminal, 97 .visible_width = max_text_len, 98 }); 99 errdefer text_input.deinit(allocator); 100 text_input.getFocus().focusable = true; 101 try text_input.setContent(allocator, protocols[0].text); 102 box.getFocus().child_id = text_input.getFocus().id; 103 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = .{ .width = max_text_len + 2, .height = 3 } }); 104 105 return .{ .box = box, .session = session, .protocols = protocols, .protocol_count = protocol_count }; 106 } 107 108 pub fn minWidth(self: *const View) usize { 109 var width: usize = 0; 110 var max_text_len: usize = 0; 111 for (self.protocols[0..self.protocol_count]) |protocol| { 112 width += protocol.name.len + 2; 113 max_text_len = @max(max_text_len, protocol.text.len); 114 } 115 return width + max_text_len + 2; 116 } 117 118 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 119 self.box.deinit(allocator); 120 } 121 122 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 123 self.clearGrid(); 124 125 if (root_focus.grandchild_id) |id| { 126 var clicked: ?usize = null; 127 for (0..self.protocol_count) |i| { 128 if (id == self.protocolLabel(i).getFocus().id) clicked = i; 129 } 130 if (clicked) |selected| { 131 if (selected != self.selected) { 132 self.selected = selected; 133 try self.textInput().setContent(allocator, self.protocols[selected].text); 134 } 135 root_focus.setFocus(self.textInput().getFocus().id); 136 } 137 } 138 139 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 140 for (0..self.protocol_count) |i| { 141 self.protocolLabel(i).options.border_style = if (i == self.selected) 142 (if (focused) .double else .single) 143 else 144 .hidden; 145 } 146 self.textInput().options.border_style = .single; 147 self.textInput().cursor = 0; 148 self.textInput().scroll_offset = 0; 149 try self.box.build(allocator, constraint, root_focus); 150 151 if (!self.session.is_terminal) { 152 const aa = self.session.arena.allocator(); 153 const id = self.textInput().getFocus().id; 154 try self.session.text_inputs.put(aa, id, self.textInput()); 155 try self.session.input_values.put(aa, id, self.protocols[self.selected].text); 156 } 157 } 158 159 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 160 const next = switch (key) { 161 .arrow_left => if (self.selected > 0) self.selected - 1 else return, 162 .arrow_right => if (self.selected + 1 < self.protocol_count) self.selected + 1 else return, 163 .enter => { 164 if (self.session.is_terminal) self.session.host_request = .{ .show_copyable_text = self.protocols[self.selected].copyable_text }; 165 return; 166 }, 167 .mouse => |mouse| blk: { 168 for (0..self.protocol_count) |i| { 169 if (inp.leftClickOn(root_focus, self.protocolLabel(i).getFocus().id, mouse)) break :blk i; 170 } 171 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) 172 self.session.host_request = .{ .show_copyable_text = self.protocols[self.selected].copyable_text }; 173 return; 174 }, 175 else => return, 176 }; 177 self.selected = next; 178 try self.textInput().setContent(allocator, self.protocols[next].text); 179 } 180 181 fn textInput(self: *View) *wgt.TextInput(ui.Widget) { 182 return &self.box.children.values()[self.protocol_count].widget.text_input; 183 } 184 185 fn protocolLabel(self: *View, index: usize) *wgt.TextBox(ui.Widget) { 186 return &self.box.children.values()[index].widget.text_box; 187 } 188 189 pub fn clearGrid(self: *View) void { 190 self.box.clearGrid(); 191 } 192 193 pub fn getGrid(self: View) ?Grid { 194 return self.box.getGrid(); 195 } 196 197 pub fn getFocus(self: *View) *Focus { 198 return self.box.getFocus(); 199 } 200 }; 201