Skip to content

Commit 9b81bf6

Browse files
committed
commands: improve debug logging of options
1 parent ce35a36 commit 9b81bf6

File tree

6 files changed

+76
-64
lines changed

6 files changed

+76
-64
lines changed

src/commands/basename.zig

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -51,59 +51,14 @@ const impl = struct {
5151
_ = cwd;
5252

5353
const options = try parseArguments(allocator, io, args, exe_path);
54-
log.debug("options={}", .{options});
54+
log.debug("{}", .{options});
5555

5656
return switch (options.mode) {
5757
.single => singleArgument(allocator, io, args, exe_path, options),
5858
.multiple => multipleArguments(io, args, options),
5959
};
6060
}
6161

62-
const BasenameOptions = struct {
63-
line_end: LineEnd = .newline,
64-
mode: Mode = .single,
65-
first_arg: []const u8 = undefined,
66-
67-
const LineEnd = enum(u8) {
68-
newline = '\n',
69-
zero = 0,
70-
};
71-
72-
const Mode = union(enum) {
73-
single,
74-
/// Value is optional suffix
75-
multiple: ?[]const u8,
76-
};
77-
78-
pub fn format(
79-
options: BasenameOptions,
80-
comptime _: []const u8,
81-
_: std.fmt.FormatOptions,
82-
writer: anytype,
83-
) !void {
84-
try writer.writeAll("BasenameOptions{ .line_end = .");
85-
try writer.writeAll(@tagName(options.line_end));
86-
87-
try writer.writeAll(", .mode = ");
88-
switch (options.mode) {
89-
.single => try writer.writeAll(".single"),
90-
.multiple => |opt_suffix| {
91-
if (opt_suffix) |suffix| {
92-
try writer.writeAll("{ .multiple = .{ .suffix = \"");
93-
try writer.writeAll(suffix);
94-
try writer.writeAll("\" } }");
95-
} else {
96-
try writer.writeAll(".multiple");
97-
}
98-
},
99-
}
100-
101-
try writer.writeAll(", .first_arg = \"");
102-
try writer.writeAll(options.first_arg);
103-
try writer.writeAll("\" }");
104-
}
105-
};
106-
10762
fn singleArgument(
10863
allocator: std.mem.Allocator,
10964
io: IO,
@@ -178,6 +133,53 @@ const impl = struct {
178133
return basename[0..end_index];
179134
}
180135

136+
const BasenameOptions = struct {
137+
line_end: LineEnd = .newline,
138+
mode: Mode = .single,
139+
first_arg: []const u8 = undefined,
140+
141+
const LineEnd = enum(u8) {
142+
newline = '\n',
143+
zero = 0,
144+
};
145+
146+
const Mode = union(enum) {
147+
single,
148+
/// Value is optional suffix
149+
multiple: ?[]const u8,
150+
};
151+
152+
pub fn format(
153+
options: BasenameOptions,
154+
comptime _: []const u8,
155+
_: std.fmt.FormatOptions,
156+
writer: anytype,
157+
) !void {
158+
try writer.writeAll("BasenameOptions {");
159+
160+
try writer.writeAll(comptime "\n" ++ shared.option_log_indentation ++ ".line_end = .");
161+
try writer.writeAll(@tagName(options.line_end));
162+
163+
try writer.writeAll(comptime ",\n" ++ shared.option_log_indentation ++ ".mode = ");
164+
switch (options.mode) {
165+
.single => try writer.writeAll(".single"),
166+
.multiple => |opt_suffix| {
167+
if (opt_suffix) |suffix| {
168+
try writer.writeAll("{ .multiple = .{ .suffix = \"");
169+
try writer.writeAll(suffix);
170+
try writer.writeAll("\" } }");
171+
} else {
172+
try writer.writeAll(".multiple");
173+
}
174+
},
175+
}
176+
177+
try writer.writeAll(comptime ",\n" ++ shared.option_log_indentation ++ ".first_arg = \"");
178+
try writer.writeAll(options.first_arg);
179+
try writer.writeAll("\",\n}");
180+
}
181+
};
182+
181183
fn parseArguments(
182184
allocator: std.mem.Allocator,
183185
io: IO,

src/commands/clear.zig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const impl = struct {
4444
_ = cwd;
4545

4646
const options = try parseArguments(allocator, io, args, exe_path);
47-
log.debug("options={}", .{options});
47+
log.debug("{}", .{options});
4848

4949
try io.stdoutWriteAll(
5050
if (options.clear_scrollback)
@@ -63,9 +63,10 @@ const impl = struct {
6363
_: std.fmt.FormatOptions,
6464
writer: anytype,
6565
) !void {
66-
try writer.writeAll("ClearOptions{ .clear_scrollback = ");
66+
try writer.writeAll("ClearOptions {");
67+
try writer.writeAll(comptime "\n" ++ shared.option_log_indentation ++ ".clear_scrollback = .");
6768
try writer.writeAll(if (options.clear_scrollback) "true" else "false");
68-
try writer.writeAll(" }");
69+
try writer.writeAll(",\n}");
6970
}
7071
};
7172

src/commands/dirname.zig

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const impl = struct {
4646
_ = cwd;
4747

4848
const options = try parseArguments(allocator, io, args, exe_path);
49-
log.debug("options={}", .{options});
49+
log.debug("{}", .{options});
5050

5151
return performDirname(io, args, options);
5252
}
@@ -66,11 +66,14 @@ const impl = struct {
6666
_: std.fmt.FormatOptions,
6767
writer: anytype,
6868
) !void {
69-
try writer.writeAll("DirnameOptions{ .line_end = .");
69+
try writer.writeAll("DirnameOptions {");
70+
71+
try writer.writeAll(comptime ",\n" ++ shared.option_log_indentation ++ ".line_end = .");
7072
try writer.writeAll(@tagName(options.line_end));
71-
try writer.writeAll(", .first_arg = \"");
73+
74+
try writer.writeAll(comptime ",\n" ++ shared.option_log_indentation ++ ".first_arg = \"");
7275
try writer.writeAll(options.first_arg);
73-
try writer.writeAll("\" }");
76+
try writer.writeAll("\",\n}");
7477
}
7578
};
7679

src/commands/template.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const impl = struct {
4949
_ = cwd;
5050

5151
const options = try parseArguments(allocator, io, args, exe_path);
52-
log.debug("options={}", .{options});
52+
log.debug("{}", .{options});
5353
}
5454

5555
const TemplateOptions = struct { // CHANGE THIS - IF NO OPTIONS ARE NEEDED DELETE THIS
@@ -60,7 +60,7 @@ const impl = struct {
6060
writer: anytype,
6161
) !void {
6262
_ = options;
63-
try writer.writeAll("TemplateOptions{ }");
63+
try writer.writeAll("TemplateOptions {}");
6464
}
6565
};
6666

src/commands/touch.zig

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const impl = struct {
5757
defer z.end();
5858

5959
const options = try parseArguments(allocator, io, args, exe_path);
60-
log.debug("options={}", .{options});
60+
log.debug("{}", .{options});
6161

6262
return performTouch(allocator, io, args, options, cwd);
6363
}
@@ -205,22 +205,26 @@ const impl = struct {
205205
_ = options;
206206
_ = fmt;
207207

208-
try writer.writeAll("TouchOptions{ .update = .");
208+
try writer.writeAll("TouchOptions {");
209+
210+
try writer.writeAll(comptime "\n" ++ shared.option_log_indentation ++ ".update = .");
209211
try writer.writeAll(@tagName(value.update));
210212

211-
try writer.writeAll(", .create = ");
213+
try writer.writeAll(comptime ",\n" ++ shared.option_log_indentation ++ ".create = ");
212214
const create = if (value.create) "true" else "false";
213215
try writer.writeAll(create);
214216

215-
try writer.writeAll(", .dereference = ");
216-
const dereference = if (value.dereference) "true" else "false";
217-
try writer.writeAll(dereference);
217+
try writer.writeAll(comptime ",\n" ++ shared.option_log_indentation ++ ".dereference = ");
218+
try writer.writeAll(if (value.dereference) "true" else "false");
219+
220+
try writer.writeAll(comptime ",\n" ++ shared.option_log_indentation ++ ".time_to_use = ");
218221

219-
try writer.writeAll(", .time_to_use = ");
220222
switch (value.time_to_use) {
221-
.current_time => try writer.writeAll(".current_time }"),
222-
inline else => |val| try writer.print(".{{ .{s} = \"{s}\" }} }}", .{ @tagName(value.time_to_use), val }),
223+
.current_time => try writer.writeAll(".current_time"),
224+
inline else => |val| try writer.print(".{{ .{s} = \"{s}\" }}", .{ @tagName(value.time_to_use), val }),
223225
}
226+
227+
try writer.writeAll(",\n}");
224228
}
225229
};
226230

src/shared.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ pub const version_string = "{NAME} - " ++ base_version_string;
88
pub const is_debug_or_test = builtin.is_test or builtin.mode == .Debug;
99
pub const free_on_close = is_debug_or_test or options.trace;
1010

11+
pub const option_log_indentation = " ";
12+
1113
pub fn mapFile(
1214
command: Command,
1315
allocator: std.mem.Allocator,

0 commit comments

Comments
 (0)