Commit 1065bfd

Eric Bower  ·  2026-03-13 09:48:42 -0400 EDT
parent fb0e621
refactor: move print fn to socket.zig
2 files changed,  +22, -22
+5, -22
......@@ -129,7 +129,7 @@ pub fn main() !void {
129129 .created_at = @intCast(std.time.timestamp()),
130130 };
131131 daemon.socket_path = socket.getSocketPath(alloc, cfg.socket_dir, sesh) catch |err| switch (err) {
132- error.NameTooLong => return printSessionNameTooLong(sesh, &cfg),
132+ error.NameTooLong => return socket.printSessionNameTooLong(sesh, cfg.socket_dir),
133133 error.OutOfMemory => return err,
134134 };
135135 std.log.info("socket path={s}", .{daemon.socket_path});
......@@ -164,7 +164,7 @@ pub fn main() !void {
164164 .task_command = cmd_args_raw.items,
165165 };
166166 daemon.socket_path = socket.getSocketPath(alloc, cfg.socket_dir, sesh) catch |err| switch (err) {
167- error.NameTooLong => return printSessionNameTooLong(sesh, &cfg),
167+ error.NameTooLong => return socket.printSessionNameTooLong(sesh, cfg.socket_dir),
168168 error.OutOfMemory => return err,
169169 };
170170 std.log.info("socket path={s}", .{daemon.socket_path});
......@@ -717,23 +717,6 @@ fn help() !void {
717717 try w.interface.flush();
718718 }
719719
720-fn printSessionNameTooLong(session_name: []const u8, cfg: *Cfg) void {
721- var buf: [4096]u8 = undefined;
722- var w = std.fs.File.stderr().writer(&buf);
723- if (socket.maxSessionNameLen(cfg.socket_dir)) |max_len| {
724- w.interface.print(
725- "error: session name is too long ({d} bytes, max {d} for socket directory \"{s}\")\n",
726- .{ session_name.len, max_len, cfg.socket_dir },
727- ) catch {};
728- } else {
729- w.interface.print(
730- "error: socket directory path is too long (\"{s}\")\n",
731- .{cfg.socket_dir},
732- ) catch {};
733- }
734- w.interface.flush() catch {};
735-}
736-
737720 fn wait(cfg: *Cfg, session_names: std.ArrayList([]const u8)) !void {
738721 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
739722 defer _ = gpa.deinit();
......@@ -884,7 +867,7 @@ fn detachAll(cfg: *Cfg) !void {
884867 defer dir.close();
885868
886869 const socket_path = socket.getSocketPath(alloc, cfg.socket_dir, session_name) catch |err| switch (err) {
887- error.NameTooLong => return printSessionNameTooLong(session_name, cfg),
870+ error.NameTooLong => return socket.printSessionNameTooLong(session_name, cfg.socket_dir),
888871 error.OutOfMemory => return err,
889872 };
890873 defer alloc.free(socket_path);
......@@ -906,7 +889,7 @@ fn kill(cfg: *Cfg, session_name: []const u8) !void {
906889 const alloc = gpa.allocator();
907890
908891 const socket_path = socket.getSocketPath(alloc, cfg.socket_dir, session_name) catch |err| switch (err) {
909- error.NameTooLong => return printSessionNameTooLong(session_name, cfg),
892+ error.NameTooLong => return socket.printSessionNameTooLong(session_name, cfg.socket_dir),
910893 error.OutOfMemory => return err,
911894 };
912895 defer alloc.free(socket_path);
......@@ -953,7 +936,7 @@ fn history(cfg: *Cfg, session_name: []const u8, format: util.HistoryFormat) !voi
953936 const alloc = gpa.allocator();
954937
955938 const socket_path = socket.getSocketPath(alloc, cfg.socket_dir, session_name) catch |err| switch (err) {
956- error.NameTooLong => return printSessionNameTooLong(session_name, cfg),
939+ error.NameTooLong => return socket.printSessionNameTooLong(session_name, cfg.socket_dir),
957940 error.OutOfMemory => return err,
958941 };
959942 defer alloc.free(socket_path);
+17, -0
......@@ -85,6 +85,23 @@ pub fn getSocketPath(alloc: std.mem.Allocator, socket_dir: []const u8, session_n
8585 return fname;
8686 }
8787
88+pub fn printSessionNameTooLong(session_name: []const u8, socket_dir: []const u8) void {
89+ var buf: [4096]u8 = undefined;
90+ var w = std.fs.File.stderr().writer(&buf);
91+ if (maxSessionNameLen(socket_dir)) |max_len| {
92+ w.interface.print(
93+ "error: session name is too long ({d} bytes, max {d} for socket directory \"{s}\")\n",
94+ .{ session_name.len, max_len, socket_dir },
95+ ) catch {};
96+ } else {
97+ w.interface.print(
98+ "error: socket directory path is too long (\"{s}\")\n",
99+ .{socket_dir},
100+ ) catch {};
101+ }
102+ w.interface.flush() catch {};
103+}
104+
88105 /// Returns the maximum session name length for a given socket directory,
89106 /// or null if the socket directory itself is already too long.
90107 pub fn maxSessionNameLen(socket_dir: []const u8) ?usize {