╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ try.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
235 236 // a batch of commits so the commits tab has more than one page to 237 // paginate through. each rewrites a few files with scattered line 238 // edits, so every commit is a multi-file diff with several separate 239 // hunks to look at. stepped timestamps vary the date column. 240 const base_ts: u64 = 1_700_000_000; // 2023-11-14 241 const edit_files = [_][]const u8{ "src/alpha.txt", "src/beta.txt", "src/gamma.txt" }; 242 // cycled through to pad each line into prose-like content. 243 const words = [_][]const u8{ 244 "lorem", "ipsum", "dolor", "sit", "amet", "consectetur", 245 "quantum", "vector", "matrix", "buffer", "kernel", "socket", 246 "falcon", "otter", "badger", "walrus", "ferret", "marmot", 247 "scatter", "gather", "encode", "decode", "render", "commit", 248 }; 249 var c: usize = 0; 250 while (c < 30) : (c += 1) { 251 { 252 var repo_dir = try cwd.openDir(io, repo_path, .{}); 253 defer repo_dir.close(io); 254 try repo_dir.createDirPath(io, "src"); 255 for (edit_files, 0..) |path, fi| { 256 const file = try repo_dir.createFile(io, path, .{}); 257 defer file.close(io); 258 var writer = std.Io.Writer.Allocating.init(allocator); 259 defer writer.deinit(); 260 // every 8th line (offset per file) encodes the commit 261 // number; the rest stay constant. the changed lines sit 262 // far enough apart that diff renders each as its own hunk. 263 // each line is padded with cycled words to a per-line 264 // length, capped at 120 characters. 265 for (0..40) |line| { 266 const start = writer.written().len; 267 if ((line + fi) % 8 == 0) { 268 try writer.writer.print("line {d}: rev {d}", .{ line, c }); 269 } else { 270 try writer.writer.print("line {d}", .{line}); 271 } 272 const target = 40 + (line * 17 + fi * 23) % 70; 273 var w = line + fi; 274 while (true) : (w += 1) { 275 const word = words[w % words.len]; 276 const len = writer.written().len - start; 277 if (len >= target or len + 1 + word.len > 120) break; 278 try writer.writer.print(" {s}", .{word}); 279 } 280 try writer.writer.writeByte('\n'); 281 } 282 try file.writeStreamingAll(io, writer.written()); 283 } 284 } 285 try repo_i.add(io, allocator, &edit_files); 286 const message = try std.fmt.allocPrint(arena.allocator(), "scattered edits {d}", .{c + 1}); 287 _ = try repo_i.commit(io, allocator, .{ .message = message, .timestamp = base_ts + c * std.time.s_per_day }); 288 } 289 } 290 291 break :blk try ui.Session.init(&session_arena, &repo, .{}); 292 }; 293 session.is_terminal = true; 294 // try uses the default serve options below, so the footer's url points at 295 // the default web UI port. 296 session.web_port = try (srv.Options{}).wuiPort(); 297 298 // start the server 299 300 var cli = false; 301 302 var arg_it = try init.minimal.args.iterateAllocator(allocator); 303 defer arg_it.deinit(); 304 _ = arg_it.skip(); 305 while (arg_it.next()) |arg| { 306 if (std.mem.eql(u8, "--cli", arg)) { 307 cli = true; 308 } 309 } 310 311 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "server" }); 312 defer allocator.free(server_path); 313 314 // let the native TUI's page builders open the on-disk repos for the file tree 315 session.io = io; 316 session.repos_dir = try std.fs.path.join(session_arena.allocator(), &.{ server_path, "repos" }); 317 318 if (cli) { 319 var stdout_writer = std.Io.File.stdout().writer(io, &.{}); 320 var stderr_writer = std.Io.File.stderr().writer(io, &.{}); 321 const run_opts = hx.main.RunOpts{ .out = &stdout_writer.interface, .err = &stderr_writer.interface }; 322 323 const key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 324 defer allocator.free(key_path); 325 326 const Runnable = struct { 327 io: std.Io, 328 key_path: []const u8, 329 330 pub fn run(self: @This()) !void { 331 std.debug.print( 332 \\ 333 \\connect to the TUI with: 334 \\ ssh -p 8022 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null localhost 335 \\ 336 \\create a git repo and push over SSH: 337 \\ mkdir -p temp-try/client/test 338 \\ cd temp-try/client/test 339 \\ git init 340 \\ echo "hello" > hello.txt 341 \\ git add hello.txt 342 \\ git commit -m "let there be light" 343 \\ GIT_SSH_COMMAND='ssh -p 8022 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -i {s}' git push localhost:admin/test HEAD:master 344 \\ 345 \\to quit, press enter. 346 \\ 347 , .{self.key_path}); 348 // portable stdin read via std.Io — equivalent to the 349 // std.posix.read(STDIN_FILENO, ...) that doesn't compile 350 // on windows. 351 var buf: [1]u8 = undefined; 352 var stdin_reader = std.Io.File.stdin().reader(self.io, &buf); 353 _ = stdin_reader.interface.takeByte() catch {}; 354 } 355 }; 356 357 try srv.run(.xit, .{}, io, allocator, cwd_path, .{ 358 .data_dir = server_path, 359 }, run_opts.err, Runnable{ .io = io, .key_path = key_path }); 360 } else { 361 var null_writer = std.Io.Writer.Discarding.init(&.{}); 362 const run_opts = hx.main.RunOpts{ .out = &null_writer.writer, .err = &null_writer.writer }; 363 364 const Runnable = struct { 365 io: std.Io, 366 allocator: std.mem.Allocator, 367 session: *ui.Session, 368 369 pub fn run(self: @This()) !void { 370 // launch the TUI 371 try hx.ui.run(self.io, self.allocator, self.session); 372 } 373 }; 374 375 try srv.run(.xit, .{}, io, allocator, cwd_path, .{ 376 .data_dir = server_path, 377 }, run_opts.err, Runnable{ .io = io, .allocator = allocator, .session = &session }); 378 } 379 } 380