╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ main.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
32 var arg_it = try init.minimal.args.iterateAllocator(allocator); 33 defer arg_it.deinit(); 34 _ = arg_it.skip(); 35 while (arg_it.next()) |arg| { 36 try args.append(allocator, arg); 37 } 38 39 var stdout_writer = std.Io.File.stdout().writer(io, &.{}); 40 var stderr_writer = std.Io.File.stderr().writer(io, &.{}); 41 42 var environ_map = try std.process.Environ.createMap(init.minimal.environ, allocator); 43 defer environ_map.deinit(); 44 const global_config_path = try xit.config.globalConfigPath(io, allocator, &environ_map); 45 defer if (global_config_path) |path| allocator.free(path); 46 47 const run_opts = RunOpts{ 48 .out = &stdout_writer.interface, 49 .err = &stderr_writer.interface, 50 .global_config_path = global_config_path, 51 }; 52 53 const cwd_path = try std.process.currentPathAlloc(io, allocator); 54 defer allocator.free(cwd_path); 55 56 run(.xit, .{}, io, allocator, args.items, cwd_path, run_opts) catch |err| switch (err) { 57 error.HandledError => return 1, 58 else => |e| return e, 59 }; 60 61 return 0; 62 } 63 64 pub fn run( 65 comptime repo_kind: rp.RepoKind, 66 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 67 io: std.Io, 68 allocator: std.mem.Allocator, 69 args: []const []const u8, 70 cwd_path: []const u8, 71 run_opts: RunOpts, 72 ) !void { 73 var cmd_args = try cmd.CommandArgs.init(allocator, args); 74 defer cmd_args.deinit(); 75 76 switch (try cmd.CommandDispatch.init(&cmd_args)) { 77 .invalid => |invalid| switch (invalid) { 78 .command => |command| { 79 try run_opts.err.print("\"{s}\" is not a valid command\n\n", .{command}); 80 try cmd.printHelp(null, run_opts.err); 81 return error.HandledError; 82 }, 83 .argument => |argument| { 84 try run_opts.err.print("\"{s}\" is not a valid argument\n\n", .{argument.value}); 85 try cmd.printHelp(argument.command, run_opts.err); 86 return error.HandledError; 87 }, 88 }, 89 .help => |cmd_kind_maybe| try cmd.printHelp(cmd_kind_maybe, run_opts.out), 90 .local => { 91 // session-lifetime allocations (the repo path, session state) 92 var session_arena = std.heap.ArenaAllocator.init(allocator); 93 defer session_arena.deinit(); 94 95 // probe both backends for a repo; the nearest one wins, so a repo 96 // isn't shadowed by one it is nested inside. only the work path 97 // and backend kind are kept; pages re-open the repo on each build. 98 const local: ui.RepoSource = blk: { 99 var found: ?ui.RepoSource = null; 100 inline for ([_]rp.RepoKind{ .git, .xit }) |probe_kind| { 101 if (rp.AnyRepo(probe_kind, .{}).open(io, allocator, .{ .path = cwd_path })) |repo| { 102 var opened = repo; 103 defer opened.deinit(io, allocator); 104 const work_path = switch (opened) { 105 inline else => |*r| r.core.work_path, 106 }; 107 const found_len = if (found) |f| f.path.len else 0; 108 if (work_path.len > found_len) { 109 found = .{ 110 .path = try session_arena.allocator().dupe(u8, work_path), 111 .repo_kind = probe_kind, 112 .global_config_path = run_opts.global_config_path, 113 }; 114 } 115 } else |err| switch (err) { 116 error.RepoNotFound => {}, 117 else => |e| return e, 118 } 119 } 120 break :blk found orelse { 121 try run_opts.err.print("no git or xit repo found in the current directory\n", .{}); 122 return error.HandledError; 123 }; 124 }; 125 126 var session = ui.Session{ 127 .arena = &session_arena, 128 .page_arena = &session_arena, 129 .io = io, 130 .local = local, 131 .is_terminal = true, 132 .data = .{ 133 // the files root; local routes carry no identity 134 .current_page = .{ .repo_files = .{ .name = .{} } }, 135 .host_kind = .local, 136 }, 137 }; 138 139 // serve the web UI in the background while the TUI runs in the 140 // foreground; the footer points at whatever port it bound. 141 const Runnable = struct { 142 io: std.Io, 143 allocator: std.mem.Allocator, 144 session: *ui.Session, 145 146 pub fn run(self: @This(), web_port: u16) !void { 147 self.session.web_port = web_port; 148 try ui.run(self.io, self.allocator, self.session, null); 149 } 150 }; 151 152 try srv.runLocal(io, allocator, local, run_opts.err, Runnable{ .io = io, .allocator = allocator, .session = &session }); 153 }, 154 .cli => |cli_cmd| switch (cli_cmd) { 155 .serve => |options| try srv.run(repo_kind, any_repo_opts, io, allocator, cwd_path, options, run_opts.err, {}), 156 }, 157 } 158 } 159