diff options
| author | 2022-01-05 17:58:07 +0100 | |
|---|---|---|
| committer | 2022-01-05 17:58:07 +0100 | |
| commit | 0b08e8e330597ee2a6b6e6828e2a33dab6ae54f6 (patch) | |
| tree | f038670acd36897a3851113ab0302d395a8a7172 | |
| parent | Update usage of process.ArgIterator to be inline with newest zig (diff) | |
| download | zig-clap-0b08e8e330597ee2a6b6e6828e2a33dab6ae54f6.tar.gz zig-clap-0b08e8e330597ee2a6b6e6828e2a33dab6ae54f6.tar.xz zig-clap-0b08e8e330597ee2a6b6e6828e2a33dab6ae54f6.zip | |
Avoid using the print API
The current Zig print API is quite good at slowing down compilation and
producing binary bloat. This commit makes an attempt to avoid using it
when not nessesary.
| -rw-r--r-- | clap.zig | 60 |
1 files changed, 37 insertions, 23 deletions
| @@ -405,18 +405,21 @@ pub fn helpFull( | |||
| 405 | continue; | 405 | continue; |
| 406 | 406 | ||
| 407 | var cs = io.countingWriter(stream); | 407 | var cs = io.countingWriter(stream); |
| 408 | try stream.print("\t", .{}); | 408 | try stream.writeAll("\t"); |
| 409 | try printParam(cs.writer(), Id, param, Error, context, valueText); | 409 | try printParam(cs.writer(), Id, param, Error, context, valueText); |
| 410 | try stream.writeByteNTimes(' ', max_spacing - @intCast(usize, cs.bytes_written)); | 410 | try stream.writeByteNTimes(' ', max_spacing - @intCast(usize, cs.bytes_written)); |
| 411 | |||
| 411 | const help_text = try helpText(context, param); | 412 | const help_text = try helpText(context, param); |
| 412 | var help_text_line_it = mem.split(u8, help_text, "\n"); | 413 | var help_text_line_it = mem.split(u8, help_text, "\n"); |
| 413 | var indent_line = false; | 414 | var indent_line = false; |
| 414 | while (help_text_line_it.next()) |line| : (indent_line = true) { | 415 | while (help_text_line_it.next()) |line| : (indent_line = true) { |
| 415 | if (indent_line) { | 416 | if (indent_line) { |
| 416 | try stream.print("\t", .{}); | 417 | try stream.writeAll("\t"); |
| 417 | try stream.writeByteNTimes(' ', max_spacing); | 418 | try stream.writeByteNTimes(' ', max_spacing); |
| 418 | } | 419 | } |
| 419 | try stream.print("\t{s}\n", .{line}); | 420 | try stream.writeAll("\t"); |
| 421 | try stream.writeAll(line); | ||
| 422 | try stream.writeAll("\n"); | ||
| 420 | } | 423 | } |
| 421 | } | 424 | } |
| 422 | } | 425 | } |
| @@ -430,25 +433,29 @@ fn printParam( | |||
| 430 | valueText: fn (@TypeOf(context), Param(Id)) Error![]const u8, | 433 | valueText: fn (@TypeOf(context), Param(Id)) Error![]const u8, |
| 431 | ) !void { | 434 | ) !void { |
| 432 | if (param.names.short) |s| { | 435 | if (param.names.short) |s| { |
| 433 | try stream.print("-{c}", .{s}); | 436 | try stream.writeAll(&[_]u8{ '-', s }); |
| 434 | } else { | 437 | } else { |
| 435 | try stream.print(" ", .{}); | 438 | try stream.writeAll(" "); |
| 436 | } | 439 | } |
| 437 | if (param.names.long) |l| { | 440 | if (param.names.long) |l| { |
| 438 | if (param.names.short) |_| { | 441 | if (param.names.short) |_| { |
| 439 | try stream.print(", ", .{}); | 442 | try stream.writeAll(", "); |
| 440 | } else { | 443 | } else { |
| 441 | try stream.print(" ", .{}); | 444 | try stream.writeAll(" "); |
| 442 | } | 445 | } |
| 443 | 446 | ||
| 444 | try stream.print("--{s}", .{l}); | 447 | try stream.writeAll("--"); |
| 448 | try stream.writeAll(l); | ||
| 445 | } | 449 | } |
| 446 | 450 | ||
| 447 | switch (param.takes_value) { | 451 | if (param.takes_value == .none) |
| 448 | .none => {}, | 452 | return; |
| 449 | .one => try stream.print(" <{s}>", .{valueText(context, param)}), | 453 | |
| 450 | .many => try stream.print(" <{s}>...", .{valueText(context, param)}), | 454 | try stream.writeAll(" <"); |
| 451 | } | 455 | try stream.writeAll(try valueText(context, param)); |
| 456 | try stream.writeAll(">"); | ||
| 457 | if (param.takes_value == .many) | ||
| 458 | try stream.writeAll("..."); | ||
| 452 | } | 459 | } |
| 453 | 460 | ||
| 454 | /// A wrapper around helpFull for simple helpText and valueText functions that | 461 | /// A wrapper around helpFull for simple helpText and valueText functions that |
| @@ -567,7 +574,7 @@ pub fn usageFull( | |||
| 567 | try cs.writeByte(name); | 574 | try cs.writeByte(name); |
| 568 | } | 575 | } |
| 569 | if (cos.bytes_written != 0) | 576 | if (cos.bytes_written != 0) |
| 570 | try cs.writeByte(']'); | 577 | try cs.writeAll("]"); |
| 571 | 578 | ||
| 572 | var positional: ?Param(Id) = null; | 579 | var positional: ?Param(Id) = null; |
| 573 | for (params) |param| { | 580 | for (params) |param| { |
| @@ -587,13 +594,17 @@ pub fn usageFull( | |||
| 587 | }; | 594 | }; |
| 588 | 595 | ||
| 589 | if (cos.bytes_written != 0) | 596 | if (cos.bytes_written != 0) |
| 590 | try cs.writeByte(' '); | 597 | try cs.writeAll(" "); |
| 591 | 598 | ||
| 592 | try cs.print("[{s}{s}", .{ prefix, name }); | 599 | try cs.writeAll("["); |
| 593 | switch (param.takes_value) { | 600 | try cs.writeAll(prefix); |
| 594 | .none => {}, | 601 | try cs.writeAll(name); |
| 595 | .one => try cs.print(" <{s}>", .{try valueText(context, param)}), | 602 | if (param.takes_value != .none) { |
| 596 | .many => try cs.print(" <{s}>...", .{try valueText(context, param)}), | 603 | try cs.writeAll(" <"); |
| 604 | try cs.writeAll(try valueText(context, param)); | ||
| 605 | try cs.writeAll(">"); | ||
| 606 | if (param.takes_value == .many) | ||
| 607 | try cs.writeAll("..."); | ||
| 597 | } | 608 | } |
| 598 | 609 | ||
| 599 | try cs.writeByte(']'); | 610 | try cs.writeByte(']'); |
| @@ -601,8 +612,11 @@ pub fn usageFull( | |||
| 601 | 612 | ||
| 602 | if (positional) |p| { | 613 | if (positional) |p| { |
| 603 | if (cos.bytes_written != 0) | 614 | if (cos.bytes_written != 0) |
| 604 | try cs.writeByte(' '); | 615 | try cs.writeAll(" "); |
| 605 | try cs.print("<{s}>", .{try valueText(context, p)}); | 616 | |
| 617 | try cs.writeAll("<"); | ||
| 618 | try cs.writeAll(try valueText(context, p)); | ||
| 619 | try cs.writeAll(">"); | ||
| 606 | } | 620 | } |
| 607 | } | 621 | } |
| 608 | 622 | ||