╭─────────────╮ │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 295 // start the server 296 297 var cli = false; 298 299 var arg_it = try init.minimal.args.iterateAllocator(allocator); 300 defer arg_it.deinit(); 301 _ = arg_it.skip(); 302 while (arg_it.next()) |arg| { 303 if (std.mem.eql(u8, "--cli", arg)) { 304 cli = true; 305 } 306 } 307 308 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "server" }); 309 defer allocator.free(server_path); 310 311 // let the native TUI's page builders open the on-disk repos for the file tree 312 session.io = io; 313 session.repos_dir = try std.fs.path.join(session_arena.allocator(), &.{ server_path, "repos" }); 314 315 if (cli) { 316 var stdout_writer = std.Io.File.stdout().writer(io, &.{}); 317 var stderr_writer = std.Io.File.stderr().writer(io, &.{}); 318 const run_opts = hx.main.RunOpts{ .out = &stdout_writer.interface, .err = &stderr_writer.interface }; 319 320 const key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 321 defer allocator.free(key_path); 322 323 const Runnable = struct { 324 io: std.Io, 325 key_path: []const u8, 326 327 pub fn run(self: @This()) !void { 328 std.debug.print( 329 \\ 330 \\connect to the TUI with: 331 \\ ssh -p 8022 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null localhost 332 \\ 333 \\create a git repo and push over SSH: 334 \\ mkdir -p temp-try/client/test 335 \\ cd temp-try/client/test 336 \\ git init 337 \\ echo "hello" > hello.txt 338 \\ git add hello.txt 339 \\ git commit -m "let there be light" 340 \\ 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 341 \\ 342 \\to quit, press enter. 343 \\ 344 , .{self.key_path}); 345 // portable stdin read via std.Io — equivalent to the 346 // std.posix.read(STDIN_FILENO, ...) that doesn't compile 347 // on windows. 348 var buf: [1]u8 = undefined; 349 var stdin_reader = std.Io.File.stdin().reader(self.io, &buf); 350 _ = stdin_reader.interface.takeByte() catch {}; 351 } 352 }; 353 354 try srv.run(.xit, .{}, io, allocator, cwd_path, .{ 355 .data_dir = server_path, 356 }, run_opts.err, Runnable{ .io = io, .key_path = key_path }); 357 } else { 358 var null_writer = std.Io.Writer.Discarding.init(&.{}); 359 const run_opts = hx.main.RunOpts{ .out = &null_writer.writer, .err = &null_writer.writer }; 360 361 const Runnable = struct { 362 io: std.Io, 363 allocator: std.mem.Allocator, 364 session: *ui.Session, 365 366 pub fn run(self: @This()) !void { 367 // launch the TUI 368 try hx.ui.run(self.io, self.allocator, self.session); 369 } 370 }; 371 372 try srv.run(.xit, .{}, io, allocator, cwd_path, .{ 373 .data_dir = server_path, 374 }, run_opts.err, Runnable{ .io = io, .allocator = allocator, .session = &session }); 375 } 376 } 377