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 { | |
| 129 | 129 | .created_at = @intCast(std.time.timestamp()), | |
| 130 | 130 | }; | |
| 131 | 131 | 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), | |
| 133 | 133 | error.OutOfMemory => return err, | |
| 134 | 134 | }; | |
| 135 | 135 | std.log.info("socket path={s}", .{daemon.socket_path}); |
| ... | ... | @@ -164,7 +164,7 @@ pub fn main() !void { | |
| 164 | 164 | .task_command = cmd_args_raw.items, | |
| 165 | 165 | }; | |
| 166 | 166 | 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), | |
| 168 | 168 | error.OutOfMemory => return err, | |
| 169 | 169 | }; | |
| 170 | 170 | std.log.info("socket path={s}", .{daemon.socket_path}); |
| ... | ... | @@ -717,23 +717,6 @@ fn help() !void { | |
| 717 | 717 | try w.interface.flush(); | |
| 718 | 718 | } | |
| 719 | 719 | ||
| 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 | - | ||
| 737 | 720 | fn wait(cfg: *Cfg, session_names: std.ArrayList([]const u8)) !void { | |
| 738 | 721 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | |
| 739 | 722 | defer _ = gpa.deinit(); |
| ... | ... | @@ -884,7 +867,7 @@ fn detachAll(cfg: *Cfg) !void { | |
| 884 | 867 | defer dir.close(); | |
| 885 | 868 | ||
| 886 | 869 | 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), | |
| 888 | 871 | error.OutOfMemory => return err, | |
| 889 | 872 | }; | |
| 890 | 873 | defer alloc.free(socket_path); |
| ... | ... | @@ -906,7 +889,7 @@ fn kill(cfg: *Cfg, session_name: []const u8) !void { | |
| 906 | 889 | const alloc = gpa.allocator(); | |
| 907 | 890 | ||
| 908 | 891 | 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), | |
| 910 | 893 | error.OutOfMemory => return err, | |
| 911 | 894 | }; | |
| 912 | 895 | defer alloc.free(socket_path); |
| ... | ... | @@ -953,7 +936,7 @@ fn history(cfg: *Cfg, session_name: []const u8, format: util.HistoryFormat) !voi | |
| 953 | 936 | const alloc = gpa.allocator(); | |
| 954 | 937 | ||
| 955 | 938 | 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), | |
| 957 | 940 | error.OutOfMemory => return err, | |
| 958 | 941 | }; | |
| 959 | 942 | defer alloc.free(socket_path); |
+17,
-0
| ... | ... | @@ -85,6 +85,23 @@ pub fn getSocketPath(alloc: std.mem.Allocator, socket_dir: []const u8, session_n | |
| 85 | 85 | return fname; | |
| 86 | 86 | } | |
| 87 | 87 | ||
| 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 | + | ||
| 88 | 105 | /// Returns the maximum session name length for a given socket directory, | |
| 89 | 106 | /// or null if the socket directory itself is already too long. | |
| 90 | 107 | pub fn maxSessionNameLen(socket_dir: []const u8) ?usize { |