Commit 7cdb333

Eric Bower  ·  2026-03-23 11:51:56 -0400 EDT
parent 4dff5d4
chore: wrap lines where applicable to 100
5 files changed,  +152, -36
+10, -2
......@@ -74,7 +74,12 @@ pub fn send(fd: i32, tag: Tag, data: []const u8) !void {
7474 }
7575 }
7676
77-pub fn appendMessage(alloc: std.mem.Allocator, list: *std.ArrayList(u8), tag: Tag, data: []const u8) !void {
77+pub fn appendMessage(
78+ alloc: std.mem.Allocator,
79+ list: *std.ArrayList(u8),
80+ tag: Tag,
81+ data: []const u8,
82+) !void {
7883 const header = Header{
7984 .tag = tag,
8085 .len = @intCast(data.len),
......@@ -178,7 +183,10 @@ const SessionProbeResult = struct {
178183 info: Info,
179184 };
180185
181-pub fn probeSession(alloc: std.mem.Allocator, socket_path: []const u8) SessionProbeError!SessionProbeResult {
186+pub fn probeSession(
187+ alloc: std.mem.Allocator,
188+ socket_path: []const u8,
189+) SessionProbeError!SessionProbeResult {
182190 const timeout_ms = 1000;
183191 const fd = socket.sessionConnect(socket_path) catch |err| switch (err) {
184192 error.ConnectionRefused => return error.ConnectionRefused,
+15, -3
......@@ -13,7 +13,10 @@ pub const LogSystem = struct {
1313 self.path = try alloc.dupe(u8, path);
1414
1515 const file = std.fs.openFileAbsolute(path, .{ .mode = .read_write }) catch |err| switch (err) {
16- error.FileNotFound => try std.fs.createFileAbsolute(path, .{ .read = true, .mode = 0o640 }),
16+ error.FileNotFound => try std.fs.createFileAbsolute(
17+ path,
18+ .{ .read = true, .mode = 0o640 },
19+ ),
1720 else => return err,
1821 };
1922
......@@ -28,7 +31,13 @@ pub const LogSystem = struct {
2831 if (self.path.len > 0) self.alloc.free(self.path);
2932 }
3033
31- pub fn log(self: *LogSystem, comptime level: std.log.Level, comptime scope: @Type(.enum_literal), comptime format: []const u8, args: anytype) void {
34+ pub fn log(
35+ self: *LogSystem,
36+ comptime level: std.log.Level,
37+ comptime scope: @Type(.enum_literal),
38+ comptime format: []const u8,
39+ args: anytype,
40+ ) void {
3241 self.mutex.lock();
3342 defer self.mutex.unlock();
3443
......@@ -82,7 +91,10 @@ pub const LogSystem = struct {
8291 else => return err,
8392 };
8493
85- self.file = try std.fs.createFileAbsolute(self.path, .{ .truncate = true, .read = true, .mode = 0o640 });
94+ self.file = try std.fs.createFileAbsolute(
95+ self.path,
96+ .{ .truncate = true, .read = true, .mode = 0o640 },
97+ );
8698 self.current_size = 0;
8799 }
88100 };
+100, -25
......@@ -355,7 +355,12 @@ const Daemon = struct {
355355
356356 const shell = util.detectShell();
357357 // Use "-shellname" as argv[0] to signal login shell (traditional method)
358- const login_shell = try std.fmt.allocPrintSentinel(alloc, "-{s}", .{std.fs.path.basename(shell)}, 0);
358+ const login_shell = try std.fmt.allocPrintSentinel(
359+ alloc,
360+ "-{s}",
361+ .{std.fs.path.basename(shell)},
362+ 0,
363+ );
359364 const argv = [_:null]?[*:0]const u8{ login_shell, null };
360365 const err = std.posix.execveZ(shell, &argv, std.c.environ);
361366 std.log.err("execve failed: err={s}", .{@errorName(err)});
......@@ -412,7 +417,10 @@ const Daemon = struct {
412417 if (ipc.probeSession(self.alloc, self.socket_path)) |result| {
413418 posix.close(result.fd);
414419 if (self.command != null) {
415- std.log.warn("session already exists, ignoring command session={s}", .{self.session_name});
420+ std.log.warn(
421+ "session already exists, ignoring command session={s}",
422+ .{self.session_name},
423+ );
416424 }
417425 } else |err| switch (err) {
418426 // Daemon is definitively gone: safe to replace.
......@@ -424,7 +432,10 @@ const Daemon = struct {
424432 // The probe is only to decide create-vs-attach; the session
425433 // exists, so proceed to attach rather than fail or orphan.
426434 else => {
427- std.log.warn("probe slow ({s}), proceeding to attach session={s}", .{ @errorName(err), self.session_name });
435+ std.log.warn(
436+ "probe slow ({s}), proceeding to attach session={s}",
437+ .{ @errorName(err), self.session_name },
438+ );
428439 },
429440 }
430441 }
......@@ -440,9 +451,16 @@ const Daemon = struct {
440451 _ = try posix.setsid();
441452
442453 log_system.deinit();
443- const session_log_name = try std.fmt.allocPrint(self.alloc, "{s}.log", .{self.session_name});
454+ const session_log_name = try std.fmt.allocPrint(
455+ self.alloc,
456+ "{s}.log",
457+ .{self.session_name},
458+ );
444459 defer self.alloc.free(session_log_name);
445- const session_log_path = try std.fs.path.join(self.alloc, &.{ self.cfg.log_dir, session_log_name });
460+ const session_log_path = try std.fs.path.join(
461+ self.alloc,
462+ &.{ self.cfg.log_dir, session_log_name },
463+ );
446464 defer self.alloc.free(session_log_path);
447465 try log_system.init(self.alloc, session_log_path);
448466
......@@ -490,9 +508,15 @@ const Daemon = struct {
490508 while (remaining.len > 0) {
491509 const n = posix.write(pty_fd, remaining) catch |err| {
492510 if (err == error.WouldBlock) {
493- std.log.warn("pty write dropped {d}/{d} bytes (buffer full)", .{ remaining.len, data.len });
511+ std.log.warn(
512+ "pty write dropped {d}/{d} bytes (buffer full)",
513+ .{ remaining.len, data.len },
514+ );
494515 } else {
495- std.log.warn("pty write failed, {d} bytes lost: {s}", .{ remaining.len, @errorName(err) });
516+ std.log.warn(
517+ "pty write failed, {d} bytes lost: {s}",
518+ .{ remaining.len, @errorName(err) },
519+ );
496520 }
497521 return;
498522 };
......@@ -526,12 +550,18 @@ const Daemon = struct {
526550 // interfering with shell initialization (DA1 queries, etc.)
527551 if (self.has_pty_output and self.has_had_client) {
528552 const cursor = &term.screens.active.cursor;
529- std.log.debug("cursor before serialize: x={d} y={d} pending_wrap={}", .{ cursor.x, cursor.y, cursor.pending_wrap });
553+ std.log.debug(
554+ "cursor before serialize: x={d} y={d} pending_wrap={}",
555+ .{ cursor.x, cursor.y, cursor.pending_wrap },
556+ );
530557 if (util.serializeTerminalState(self.alloc, term)) |term_output| {
531558 std.log.debug("serialize terminal state", .{});
532559 defer self.alloc.free(term_output);
533560 ipc.appendMessage(self.alloc, &client.write_buf, .Output, term_output) catch |err| {
534- std.log.warn("failed to buffer terminal state for client err={s}", .{@errorName(err)});
561+ std.log.warn(
562+ "failed to buffer terminal state for client err={s}",
563+ .{@errorName(err)},
564+ );
535565 };
536566 client.has_pending_output = true;
537567 }
......@@ -552,7 +582,12 @@ const Daemon = struct {
552582 std.log.debug("init resize rows={d} cols={d}", .{ resize.rows, resize.cols });
553583 }
554584
555- pub fn handleResize(self: *Daemon, pty_fd: i32, term: *ghostty_vt.Terminal, payload: []const u8) !void {
585+ pub fn handleResize(
586+ self: *Daemon,
587+ pty_fd: i32,
588+ term: *ghostty_vt.Terminal,
589+ payload: []const u8,
590+ ) !void {
556591 if (payload.len != @sizeOf(ipc.Resize)) return;
557592
558593 const resize = std.mem.bytesToValue(ipc.Resize, payload);
......@@ -584,7 +619,8 @@ const Daemon = struct {
584619 pub fn handleKill(self: *Daemon) void {
585620 std.log.info("kill received session={s}", .{self.session_name});
586621 self.shutdown();
587- // gracefully shutdown shell processes, shells tend to ignore SIGTERM so we send SIGHUP instead
622+ // gracefully shutdown shell processes, shells tend to ignore SIGTERM so we send SIGHUP
623+ // instead
588624 // https://www.gnu.org/software/bash/manual/html_node/Signals.html
589625 // negative pid means kill process and children
590626 std.log.info("sending SIGHUP session={s} pid={d}", .{ self.session_name, self.pid });
......@@ -653,7 +689,12 @@ const Daemon = struct {
653689 client.has_pending_output = true;
654690 }
655691
656- pub fn handleHistory(self: *Daemon, client: *Client, term: *ghostty_vt.Terminal, payload: []const u8) !void {
692+ pub fn handleHistory(
693+ self: *Daemon,
694+ client: *Client,
695+ term: *ghostty_vt.Terminal,
696+ payload: []const u8,
697+ ) !void {
657698 const format: util.HistoryFormat = if (payload.len > 0)
658699 std.meta.intToEnum(util.HistoryFormat, payload[0]) catch .plain
659700 else
......@@ -784,7 +825,10 @@ fn wait(cfg: *Cfg, session_names: std.ArrayList([]const u8)) !void {
784825 // is no longer deleted, so this session would otherwise
785826 // persist as task_ended_at==0 forever → infinite "still
786827 // waiting". Count it as done+failed so wait terminates.
787- try stderr.print("task unreachable: {s} ({s})\n", .{ session.name, session.error_name orelse "unknown" });
828+ try stderr.print(
829+ "task unreachable: {s} ({s})\n",
830+ .{ session.name, session.error_name orelse "unknown" },
831+ );
788832 try stderr.flush();
789833 agg_exit_code = 1;
790834 done += 1;
......@@ -810,7 +854,10 @@ fn wait(cfg: *Cfg, session_names: std.ArrayList([]const u8)) !void {
810854 // crashed and the remaining N-1 happen to be done, total==done
811855 // would be a false success.
812856 if (total < max_seen) {
813- try stderr.print("error: {d} session(s) disappeared before completing\n", .{max_seen - total});
857+ try stderr.print(
858+ "error: {d} session(s) disappeared before completing\n",
859+ .{max_seen - total},
860+ );
814861 try stderr.flush();
815862 std.process.exit(1);
816863 return;
......@@ -943,7 +990,10 @@ fn kill(cfg: *Cfg, session_name: []const u8) !void {
943990 socket.cleanupStaleSocket(dir, session_name);
944991 w.interface.print("cleaned up stale session {s}\n", .{session_name}) catch {};
945992 } else {
946- w.interface.print("session {s} is unresponsive ({s}) -- daemon may be busy, try again or kill the process directly\n", .{ session_name, @errorName(err) }) catch {};
993+ w.interface.print(
994+ "session {s} is unresponsive ({s}) -- daemon may be busy, try again or kill the process directly\n",
995+ .{ session_name, @errorName(err) },
996+ ) catch {};
947997 }
948998 w.interface.flush() catch {};
949999 return;
......@@ -1193,7 +1243,9 @@ fn run(daemon: *Daemon, command_args: [][]const u8) !void {
11931243 else => return err,
11941244 };
11951245
1196- var poll_fds = [_]posix.pollfd{.{ .fd = probe_result.fd, .events = posix.POLL.IN, .revents = 0 }};
1246+ var poll_fds = [_]posix.pollfd{
1247+ .{ .fd = probe_result.fd, .events = posix.POLL.IN, .revents = 0 },
1248+ };
11971249 const poll_result = posix.poll(&poll_fds, 5000) catch return error.PollFailed;
11981250 if (poll_result == 0) {
11991251 std.log.err("timeout waiting for ack", .{});
......@@ -1297,7 +1349,8 @@ fn clientLoop(client_sock_fd: i32) !void {
12971349 };
12981350
12991351 // Handle stdin -> socket (Input)
1300- if (poll_fds.items[0].revents & (posix.POLL.IN | posix.POLL.HUP | posix.POLL.ERR | posix.POLL.NVAL) != 0) {
1352+ const inp_flags = (posix.POLL.IN | posix.POLL.HUP | posix.POLL.ERR | posix.POLL.NVAL);
1353+ if (poll_fds.items[0].revents & inp_flags != 0) {
13011354 var buf: [4096]u8 = undefined;
13021355 const n_opt: ?usize = posix.read(stdin_fd, &buf) catch |err| blk: {
13031356 if (err == error.WouldBlock) break :blk null;
......@@ -1397,7 +1450,10 @@ fn daemonLoop(daemon: *Daemon, server_sock_fd: i32, pty_fd: i32) !void {
13971450
13981451 daemon_loop: while (daemon.running) {
13991452 if (sigterm_received.swap(false, .acq_rel)) {
1400- std.log.info("SIGTERM received, shutting down gracefully session={s}", .{daemon.session_name});
1453+ std.log.info(
1454+ "SIGTERM received, shutting down gracefully session={s}",
1455+ .{daemon.session_name},
1456+ );
14011457 break :daemon_loop;
14021458 }
14031459
......@@ -1436,7 +1492,12 @@ fn daemonLoop(daemon: *Daemon, server_sock_fd: i32, pty_fd: i32) !void {
14361492 std.log.err("server socket error revents={d}", .{poll_fds.items[0].revents});
14371493 break :daemon_loop;
14381494 } else if (poll_fds.items[0].revents & posix.POLL.IN != 0) {
1439- const client_fd = try posix.accept(server_sock_fd, null, null, posix.SOCK.NONBLOCK | posix.SOCK.CLOEXEC);
1495+ const client_fd = try posix.accept(
1496+ server_sock_fd,
1497+ null,
1498+ null,
1499+ posix.SOCK.NONBLOCK | posix.SOCK.CLOEXEC,
1500+ );
14401501 const client = try daemon.alloc.create(Client);
14411502 client.* = Client{
14421503 .alloc = daemon.alloc,
......@@ -1446,10 +1507,14 @@ fn daemonLoop(daemon: *Daemon, server_sock_fd: i32, pty_fd: i32) !void {
14461507 };
14471508 client.write_buf = try std.ArrayList(u8).initCapacity(client.alloc, 4096);
14481509 try daemon.clients.append(daemon.alloc, client);
1449- std.log.info("client connected fd={d} total={d}", .{ client_fd, daemon.clients.items.len });
1510+ std.log.info(
1511+ "client connected fd={d} total={d}",
1512+ .{ client_fd, daemon.clients.items.len },
1513+ );
14501514 }
14511515
1452- if (poll_fds.items[1].revents & (posix.POLL.IN | posix.POLL.HUP | posix.POLL.ERR | posix.POLL.NVAL) != 0) {
1516+ const inp_flags = posix.POLL.IN | posix.POLL.HUP | posix.POLL.ERR | posix.POLL.NVAL;
1517+ if (poll_fds.items[1].revents & inp_flags != 0) {
14531518 // Read from PTY
14541519 var buf: [4096]u8 = undefined;
14551520 const n_opt: ?usize = posix.read(pty_fd, &buf) catch |err| blk: {
......@@ -1490,7 +1555,10 @@ fn daemonLoop(daemon: *Daemon, server_sock_fd: i32, pty_fd: i32) !void {
14901555 // Broadcast data to all clients
14911556 for (daemon.clients.items) |client| {
14921557 ipc.appendMessage(daemon.alloc, &client.write_buf, .Output, buf[0..n]) catch |err| {
1493- std.log.warn("failed to buffer output for client err={s}", .{@errorName(err)});
1558+ std.log.warn(
1559+ "failed to buffer output for client err={s}",
1560+ .{@errorName(err)},
1561+ );
14941562 continue;
14951563 };
14961564 client.has_pending_output = true;
......@@ -1505,7 +1573,8 @@ fn daemonLoop(daemon: *Daemon, server_sock_fd: i32, pty_fd: i32) !void {
15051573 // So number of clients in poll_fds is poll_fds.items.len - 2
15061574 const num_polled_clients = poll_fds.items.len - 2;
15071575 if (i > num_polled_clients) {
1508- // If we have more clients than polled (i.e. we just accepted one), start from the polled ones
1576+ // If we have more clients than polled (i.e. we just accepted one), start from the
1577+ // polled ones
15091578 i = num_polled_clients;
15101579 }
15111580
......@@ -1517,7 +1586,10 @@ fn daemonLoop(daemon: *Daemon, server_sock_fd: i32, pty_fd: i32) !void {
15171586 if (revents & posix.POLL.IN != 0) {
15181587 const n = client.read_buf.read(client.socket_fd) catch |err| {
15191588 if (err == error.WouldBlock) continue;
1520- std.log.debug("client read err={s} fd={d}", .{ @errorName(err), client.socket_fd });
1589+ std.log.debug(
1590+ "client read err={s} fd={d}",
1591+ .{ @errorName(err), client.socket_fd },
1592+ );
15211593 const last = daemon.closeClient(client, i, false);
15221594 if (last) break :daemon_loop;
15231595 continue;
......@@ -1550,7 +1622,10 @@ fn daemonLoop(daemon: *Daemon, server_sock_fd: i32, pty_fd: i32) !void {
15501622 .History => try daemon.handleHistory(client, &term, msg.payload),
15511623 .Run => try daemon.handleRun(client, pty_fd, msg.payload),
15521624 .Output, .Ack => {},
1553- _ => std.log.warn("ignoring unknown IPC tag={d}", .{@intFromEnum(msg.header.tag)}),
1625+ _ => std.log.warn(
1626+ "ignoring unknown IPC tag={d}",
1627+ .{@intFromEnum(msg.header.tag)},
1628+ ),
15541629 }
15551630 }
15561631 }
+10, -2
......@@ -58,7 +58,11 @@ pub fn createSocket(fname: []const u8) !i32 {
5858 // AF.UNIX: Unix domain socket for local IPC with client processes
5959 // SOCK.STREAM: Reliable, bidirectional communication
6060 // SOCK.NONBLOCK: Set socket to non-blocking
61- const fd = try posix.socket(posix.AF.UNIX, posix.SOCK.STREAM | posix.SOCK.NONBLOCK | posix.SOCK.CLOEXEC, 0);
61+ const fd = try posix.socket(
62+ posix.AF.UNIX,
63+ posix.SOCK.STREAM | posix.SOCK.NONBLOCK | posix.SOCK.CLOEXEC,
64+ 0,
65+ );
6266 errdefer posix.close(fd);
6367
6468 var unix_addr = try std.net.Address.initUnix(fname);
......@@ -74,7 +78,11 @@ pub const max_socket_path_len: usize = @typeInfo(
7478 @TypeOf(@as(posix.sockaddr.un, undefined).path),
7579 ).array.len - 1;
7680
77-pub fn getSocketPath(alloc: std.mem.Allocator, socket_dir: []const u8, session_name: []const u8) error{ NameTooLong, OutOfMemory }![]const u8 {
81+pub fn getSocketPath(
82+ alloc: std.mem.Allocator,
83+ socket_dir: []const u8,
84+ session_name: []const u8,
85+) error{ NameTooLong, OutOfMemory }![]const u8 {
7886 const dir = socket_dir;
7987 const path_len = dir.len + 1 + session_name.len;
8088 if (path_len > max_socket_path_len) return error.NameTooLong;
+17, -4
......@@ -27,7 +27,10 @@ pub const SessionEntry = struct {
2727 }
2828 };
2929
30-pub fn get_session_entries(alloc: std.mem.Allocator, socket_dir: []const u8) !std.ArrayList(SessionEntry) {
30+pub fn get_session_entries(
31+ alloc: std.mem.Allocator,
32+ socket_dir: []const u8,
33+) !std.ArrayList(SessionEntry) {
3134 var dir = try std.fs.openDirAbsolute(socket_dir, .{ .iterate = true });
3235 defer dir.close();
3336 var iter = dir.iterate();
......@@ -102,7 +105,8 @@ pub fn shellNeedsQuoting(arg: []const u8) bool {
102105 if (arg.len == 0) return true;
103106 for (arg) |ch| {
104107 switch (ch) {
105- ' ', '\t', '"', '\'', '\\', '$', '`', '!', '(', ')', '{', '}', '[', ']', '|', '&', ';', '<', '>', '?', '*', '~', '#', '\n' => return true,
108+ ' ', '\t', '"', '\'', '\\', '$', '`', '!', '(', ')', '{', '}', '[', ']' => return true,
109+ '|', '&', ';', '<', '>', '?', '*', '~', '#', '\n' => return true,
106110 else => {},
107111 }
108112 }
......@@ -333,7 +337,11 @@ pub const HistoryFormat = enum(u8) {
333337 html = 2,
334338 };
335339
336-pub fn serializeTerminal(alloc: std.mem.Allocator, term: *ghostty_vt.Terminal, format: HistoryFormat) ?[]const u8 {
340+pub fn serializeTerminal(
341+ alloc: std.mem.Allocator,
342+ term: *ghostty_vt.Terminal,
343+ format: HistoryFormat,
344+) ?[]const u8 {
337345 var builder: std.Io.Writer.Allocating = .init(alloc);
338346 defer builder.deinit();
339347
......@@ -378,7 +386,12 @@ pub fn detectShell() [:0]const u8 {
378386
379387 /// Formats a session entry for list output (only the name when `short` is
380388 /// true), adding a prefix to indicate the current session, if there is one.
381-pub fn writeSessionLine(writer: *std.Io.Writer, session: SessionEntry, short: bool, current_session: ?[]const u8) !void {
389+pub fn writeSessionLine(
390+ writer: *std.Io.Writer,
391+ session: SessionEntry,
392+ short: bool,
393+ current_session: ?[]const u8,
394+) !void {
382395 const current_arrow = "→";
383396 const prefix = if (current_session) |current|
384397 if (std.mem.eql(u8, current, session.name)) current_arrow ++ " " else " "