-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathHelpMessageWriter.zig
376 lines (306 loc) · 11.5 KB
/
HelpMessageWriter.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
const HelpMessageWriter = @This();
const std = @import("std");
const Arg = @import("Arg.zig");
const BufferedWriter = std.io.BufferedWriter(4096, std.fs.File.Writer);
const Command = @import("Command.zig");
const ParsedCommand = @import("parser/ParseResult.zig").ParsedCommand;
/// The total number of space to use before any argument name.
///
/// **NOTE**: The argument refers to any argument not just the `Arg`.
const SIGNATURE_LEFT_PADDING = 4;
/// Used to store the help content before writing into the `stderr`.
buffer: BufferedWriter,
/// Command whose help to write.
command: *const ParsedCommand,
pub fn init(command: *const ParsedCommand) HelpMessageWriter {
return HelpMessageWriter{
.buffer = std.io.bufferedWriter(
std.io.getStdErr().writer(),
),
.command = command,
};
}
pub fn write(self: *HelpMessageWriter) !void {
try self.writeDescription();
try self.writeHeader();
try self.writePositionalArgs();
try self.writeSubcommands();
try self.writeOptions();
try self.writeFooter();
try self.buffer.flush();
}
fn writeDescription(self: *HelpMessageWriter) !void {
const writer = self.buffer.writer();
if (self.command.deref().description) |d| {
try writer.print("{s}\n\n", .{d});
}
}
fn writeHeader(self: *HelpMessageWriter) !void {
const writer = self.buffer.writer();
try writer.print("Usage: {s}", .{self.command.name()});
const command = self.command.deref();
if (command.countPositionalArgs() >= 1) {
try writer.print(
" {c}ARGS{c}",
getBraces(command.hasProperty(.positional_arg_required)),
);
}
if (command.countOptions() >= 1) {
try writer.writeAll(" [OPTIONS]");
}
if (command.countSubcommands() >= 1) {
try writer.print(
" {c}COMMAND{c}",
getBraces(command.hasProperty(.subcommand_required)),
);
}
// End of line.
try writer.writeByte('\n');
}
fn writePositionalArgs(self: *HelpMessageWriter) !void {
if (self.command.deref().countPositionalArgs() == 0) {
return;
}
const writer = self.buffer.writer();
try writer.writeAll("\nArgs:\n");
for (self.command.deref().positional_args.items) |arg| {
var line = Line.init();
try line.signature.addPadding(SIGNATURE_LEFT_PADDING);
try line.signature.writeAll(arg.name);
if (arg.hasProperty(.takes_multiple_values)) {
try line.signature.writeAll("...");
}
if (arg.description) |description| {
try line.description.writeAll(description);
}
try writer.print("{}", .{line});
}
}
fn writeSubcommands(self: *HelpMessageWriter) !void {
if (self.command.deref().countSubcommands() == 0) {
return;
}
const writer = self.buffer.writer();
try writer.writeAll("\nCommands:\n");
for (self.command.deref().subcommands.items) |*subcommand| {
var line = Line.init();
try line.signature.addPadding(SIGNATURE_LEFT_PADDING);
try line.signature.writeAll(subcommand.name);
if (subcommand.description) |description| {
try line.description.writeAll(description);
}
try writer.print("{}", .{line});
}
}
fn writeOptions(self: *HelpMessageWriter) !void {
if (self.command.deref().countOptions() == 0) {
return;
}
const writer = self.buffer.writer();
try writer.writeAll("\nOptions:\n");
for (self.command.deref().options.items) |*option| {
try self.writeOption(option);
}
const help = Arg.booleanOption("help", 'h', "Print this help and exit");
try self.writeOption(&help);
}
fn writeOption(self: *HelpMessageWriter, option: *const Arg) !void {
const writer = self.buffer.writer();
var line = Line.init();
try line.signature.addPadding(SIGNATURE_LEFT_PADDING);
// Option name.
if (option.short_name != null and option.long_name != null) {
try line.signature.print(
"-{c}, --{s}",
.{ option.short_name.?, option.long_name.? },
);
} else if (option.short_name) |short_name| {
try line.signature.print("-{c}", .{short_name});
} else if (option.long_name) |long_name| {
// When short name are not present extra padding is required to align
// all the long name in the same line.
//
// -t, --time
// --max-time
//
// If not set then it will look like:
//
// -t, --time
// --max-time
try line.signature.addPadding(SIGNATURE_LEFT_PADDING);
try line.signature.print("--{s}", .{long_name});
}
// Value name.
if (option.hasProperty(.takes_value)) {
// Print the option actual value name or option name itself.
const value_name = option.value_placeholder orelse option.name;
try line.signature.print("=<{s}>", .{value_name});
if (option.hasProperty(.takes_multiple_values)) {
try line.signature.writeAll("...");
}
}
// Description.
if (option.description) |description| {
try line.description.writeAll(description);
try writer.print("{}", .{line});
}
if (option.valid_values) |valid_values| {
// If the description is not set then print the values at the same line.
if (option.description == null) {
try line.description.print("values: {s}", .{valid_values});
return writer.print("{}", .{line});
}
// If the description is set then print the values at the new line
// but just below the description.
var new_line = Line.init();
try new_line.description.addPadding(2);
try new_line.description.print("values: {s}", .{valid_values});
try writer.print("{}", .{new_line});
}
}
fn writeFooter(self: *HelpMessageWriter) !void {
if (self.command.deref().countSubcommands() >= 1) {
try self.buffer.writer().print(
"\nRun '{s} <command>` with `-h/--h' flag to get help of any command.\n",
.{self.command.name()},
);
}
}
fn getBraces(required: bool) struct { u8, u8 } {
return if (required) .{ '<', '>' } else .{ '[', ']' };
}
/// Represents a line in the terminal.
///
/// As compere to regular line composed of single row and multiple columns,
/// this line is composed of two blocks named `signature` and `description`.
const Line = struct {
/// Represents the name of an argument.
///
/// **NOTE**: The argument refers to any argument not just the `Arg`.
const Signature = LineBlock(50);
/// Represents the description of an argument.
///
/// **NOTE**: The argument refers to any argument not just the `Arg`.
const Description = LineBlock(80);
/// Argument name or any other text which can be part of name.
///
/// For e.x.: Option name and its value placeholder makes up single
/// signature (`-t, --time=<SECS>`).
signature: Signature,
/// Argument description.
description: Description,
/// Creates an empty line.
pub fn init() Line {
return Line{ .signature = Signature.init(), .description = Description.init() };
}
pub fn format(
self: Line,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void {
_ = fmt;
_ = options;
try writer.print("{}{}\n", .{ self.signature, self.description });
const overflow_signature = self.signature.overflowContent();
const overflow_description = self.description.overflowContent();
if (overflow_signature == null and overflow_description == null) {
return;
}
var new_line = Line.init();
if (overflow_signature) |signature| {
// FIXME: Inherit padding from the previous line (i.e. this line).
try new_line.signature.addPadding(SIGNATURE_LEFT_PADDING);
try new_line.signature.writeAll(signature);
}
if (overflow_description) |description| {
try new_line.description.writeAll(description);
}
try writer.print("{}", .{new_line});
}
};
/// Represents a discrete area within a `Line` where signature or description
/// can be write.
fn LineBlock(comptime width: usize) type {
return struct {
const Self = @This();
/// A character used for padding.
const WHITE_SPACE = ' ';
/// Used for storing content.
const Array = std.BoundedArray(u8, width);
/// A custom writer over the `std.BoundedArray.Writer` for better error
/// and word wrap handling.
const ContentWriter = std.io.GenericWriter(
*Self,
ContentWriteError,
writeContent,
);
/// Required error type for the required method.
const ContentWriteError = error{Overflow};
/// Content that fits into this block.
visible_content: Array = Array{},
/// Content that cannot fit into this block.
overflow_content: Array = Array{},
/// Creates an empty block.
fn init() Self {
return Self{};
}
/// Returns the length of remaining space.
fn remainingSpaceLength(self: *const Self) usize {
return width - self.visible_content.len;
}
/// Returns the content that cannot fit into this block, if any.
fn overflowContent(self: *const Self) ?[]const u8 {
if (self.overflow_content.len == 0) {
return null;
}
return self.overflow_content.constSlice();
}
/// Adds the `n` number of space.
fn addPadding(self: *Self, n: usize) !void {
if (n > width) {
return self.addPadding(self.remainingSpaceLength());
}
try self.visible_content.appendNTimes(Self.WHITE_SPACE, n);
}
/// Appends the string based on the given format.
fn print(self: *Self, comptime fmt: []const u8, args: anytype) !void {
try self.contentWriter().print(fmt, args);
}
/// Appends the given string as-is.
fn writeAll(self: *Self, string: []const u8) !void {
try self.contentWriter().writeAll(string);
}
/// Returns the content writer.
fn contentWriter(self: *Self) ContentWriter {
return ContentWriter{ .context = self };
}
/// Writes the given bytes and returns the number of bytes written.
fn writeContent(self: *Self, bytes: []const u8) ContentWriteError!usize {
const remaining_space_length = self.remainingSpaceLength();
if (bytes.len <= remaining_space_length) {
try self.visible_content.appendSlice(bytes);
return bytes.len;
}
const writeable_portion = bytes[0..remaining_space_length];
try self.writeAll(writeable_portion);
const remaining_portion = bytes[remaining_space_length..];
try self.overflow_content.appendSlice(remaining_portion);
return writeable_portion.len;
}
pub fn format(
self: Self,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void {
_ = fmt;
_ = options;
var mut_self = self;
if (mut_self.remainingSpaceLength() != 0) {
mut_self.addPadding(mut_self.remainingSpaceLength()) catch {};
}
try writer.writeAll(mut_self.visible_content.constSlice());
}
};
}