- commit
- 1065bfd
- parent
- fb0e621
- author
- Eric Bower
- date
- 2026-03-13 09:48:42 -0400 EDT
refactor: move print fn to socket.zig
2 files changed,
+22,
-22
+5,
-22
1@@ -129,7 +129,7 @@ pub fn main() !void {
2 .created_at = @intCast(std.time.timestamp()),
3 };
4 daemon.socket_path = socket.getSocketPath(alloc, cfg.socket_dir, sesh) catch |err| switch (err) {
5- error.NameTooLong => return printSessionNameTooLong(sesh, &cfg),
6+ error.NameTooLong => return socket.printSessionNameTooLong(sesh, cfg.socket_dir),
7 error.OutOfMemory => return err,
8 };
9 std.log.info("socket path={s}", .{daemon.socket_path});
10@@ -164,7 +164,7 @@ pub fn main() !void {
11 .task_command = cmd_args_raw.items,
12 };
13 daemon.socket_path = socket.getSocketPath(alloc, cfg.socket_dir, sesh) catch |err| switch (err) {
14- error.NameTooLong => return printSessionNameTooLong(sesh, &cfg),
15+ error.NameTooLong => return socket.printSessionNameTooLong(sesh, cfg.socket_dir),
16 error.OutOfMemory => return err,
17 };
18 std.log.info("socket path={s}", .{daemon.socket_path});
19@@ -717,23 +717,6 @@ fn help() !void {
20 try w.interface.flush();
21 }
22
23-fn printSessionNameTooLong(session_name: []const u8, cfg: *Cfg) void {
24- var buf: [4096]u8 = undefined;
25- var w = std.fs.File.stderr().writer(&buf);
26- if (socket.maxSessionNameLen(cfg.socket_dir)) |max_len| {
27- w.interface.print(
28- "error: session name is too long ({d} bytes, max {d} for socket directory \"{s}\")\n",
29- .{ session_name.len, max_len, cfg.socket_dir },
30- ) catch {};
31- } else {
32- w.interface.print(
33- "error: socket directory path is too long (\"{s}\")\n",
34- .{cfg.socket_dir},
35- ) catch {};
36- }
37- w.interface.flush() catch {};
38-}
39-
40 fn wait(cfg: *Cfg, session_names: std.ArrayList([]const u8)) !void {
41 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
42 defer _ = gpa.deinit();
43@@ -884,7 +867,7 @@ fn detachAll(cfg: *Cfg) !void {
44 defer dir.close();
45
46 const socket_path = socket.getSocketPath(alloc, cfg.socket_dir, session_name) catch |err| switch (err) {
47- error.NameTooLong => return printSessionNameTooLong(session_name, cfg),
48+ error.NameTooLong => return socket.printSessionNameTooLong(session_name, cfg.socket_dir),
49 error.OutOfMemory => return err,
50 };
51 defer alloc.free(socket_path);
52@@ -906,7 +889,7 @@ fn kill(cfg: *Cfg, session_name: []const u8) !void {
53 const alloc = gpa.allocator();
54
55 const socket_path = socket.getSocketPath(alloc, cfg.socket_dir, session_name) catch |err| switch (err) {
56- error.NameTooLong => return printSessionNameTooLong(session_name, cfg),
57+ error.NameTooLong => return socket.printSessionNameTooLong(session_name, cfg.socket_dir),
58 error.OutOfMemory => return err,
59 };
60 defer alloc.free(socket_path);
61@@ -953,7 +936,7 @@ fn history(cfg: *Cfg, session_name: []const u8, format: util.HistoryFormat) !voi
62 const alloc = gpa.allocator();
63
64 const socket_path = socket.getSocketPath(alloc, cfg.socket_dir, session_name) catch |err| switch (err) {
65- error.NameTooLong => return printSessionNameTooLong(session_name, cfg),
66+ error.NameTooLong => return socket.printSessionNameTooLong(session_name, cfg.socket_dir),
67 error.OutOfMemory => return err,
68 };
69 defer alloc.free(socket_path);
+17,
-0
1@@ -85,6 +85,23 @@ pub fn getSocketPath(alloc: std.mem.Allocator, socket_dir: []const u8, session_n
2 return fname;
3 }
4
5+pub fn printSessionNameTooLong(session_name: []const u8, socket_dir: []const u8) void {
6+ var buf: [4096]u8 = undefined;
7+ var w = std.fs.File.stderr().writer(&buf);
8+ if (maxSessionNameLen(socket_dir)) |max_len| {
9+ w.interface.print(
10+ "error: session name is too long ({d} bytes, max {d} for socket directory \"{s}\")\n",
11+ .{ session_name.len, max_len, socket_dir },
12+ ) catch {};
13+ } else {
14+ w.interface.print(
15+ "error: socket directory path is too long (\"{s}\")\n",
16+ .{socket_dir},
17+ ) catch {};
18+ }
19+ w.interface.flush() catch {};
20+}
21+
22 /// Returns the maximum session name length for a given socket directory,
23 /// or null if the socket directory itself is already too long.
24 pub fn maxSessionNameLen(socket_dir: []const u8) ?usize {