Skip to content

Commit 9e11894

Browse files
committed
Merge branch 'zig-0.15'
2 parents bdaab80 + 65992fb commit 9e11894

13 files changed

Lines changed: 108 additions & 160 deletions

File tree

.github/workflows/build-and-test.yaml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
name: build-and-test
22
on:
33
push:
4-
branches:
5-
- main
64
pull_request:
7-
branches:
8-
- main
5+
schedule:
6+
- cron: 0 4 * * *
7+
8+
env:
9+
zig_version: 0.15.1
910

1011
jobs:
11-
test-linux:
12+
build-and-test-linux:
1213
runs-on: ubuntu-latest
1314
steps:
1415
- uses: actions/checkout@v4
15-
- uses: goto-bus-stop/setup-zig@v2
16+
- uses: mlugg/setup-zig@v2
1617
with:
17-
version: 0.14.1
18+
version: ${{ env.zig_version }}
1819
- run: zig fmt --check *.zig src/*.zig
1920
- run: zig build
2021
- run: zig build test
21-
- run: cd examples/standalone && zig build
2222
- run: zig build docs
2323
- name: Upload docs artifact
2424
if: github.ref == 'refs/heads/main' && github.event_name != 'schedule'
2525
uses: actions/upload-pages-artifact@v3
2626
with:
2727
path: ./zig-out/docs/
2828

29-
test-windows:
29+
build-and-test-windows:
3030
runs-on: windows-latest
3131
steps:
3232
- uses: actions/checkout@v4
33-
- uses: goto-bus-stop/setup-zig@v2
33+
- uses: mlugg/setup-zig@v2
3434
with:
35-
version: 0.14.1
35+
version: ${{ env.zig_version }}
3636
- run: zig build
3737
- run: zig build test
3838

3939
deploy-pages:
4040
runs-on: ubuntu-latest
41-
needs: test-linux
41+
needs: build-and-test-linux
4242
if: github.ref == 'refs/heads/main' && github.event_name != 'schedule'
4343
permissions:
4444
pages: write # to deploy to Pages

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,12 @@ fn run_server() !void {
7373
### Using with the Zig package manager
7474
Add `cli` to your `build.zig.zon`
7575
```
76+
# For zig 0.14.x
7677
zig fetch --save git+https://github.com/sam701/zig-cli
78+
79+
# For zig master
80+
zig fetch --save git+https://github.com/sam701/zig-cli#zig-0.15
7781
```
78-
See the [`standalone`](./examples/standalone) example in the `examples` folder.
7982

8083
## Printing help
8184
See [`simple.zig`](./examples/simple.zig)

build.zig

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,45 @@ pub fn build(b: *std.Build) void {
1818
b.installArtifact(lib);
1919

2020
const main_tests = b.addTest(.{
21-
.root_source_file = b.path("src/tests.zig"),
22-
.target = target,
23-
.optimize = optimize,
21+
.root_module = lib_mod,
2422
});
2523
const run_tests = b.addRunArtifact(main_tests);
2624
const test_step = b.step("test", "Run library tests");
2725
test_step.dependOn(&run_tests.step);
2826

29-
const simple = b.addExecutable(.{
30-
.target = target,
31-
.name = "simple",
32-
.root_source_file = b.path("examples/simple.zig"),
33-
.optimize = optimize,
34-
});
35-
simple.root_module.addImport("cli", lib_mod);
36-
b.installArtifact(simple);
37-
b.default_step.dependOn(&simple.step);
27+
// Example simple
28+
{
29+
const simple_mod = b.addModule("simple", .{
30+
.root_source_file = b.path("examples/simple.zig"),
31+
.target = target,
32+
.optimize = optimize,
33+
});
3834

39-
const short = b.addExecutable(.{
40-
.target = target,
41-
.name = "short",
42-
.root_source_file = b.path("examples/short.zig"),
43-
.optimize = optimize,
44-
});
45-
short.root_module.addImport("cli", lib_mod);
46-
b.installArtifact(short);
47-
b.default_step.dependOn(&short.step);
35+
const simple = b.addExecutable(.{
36+
.name = "simple",
37+
.root_module = simple_mod,
38+
});
39+
simple.root_module.addImport("cli", lib_mod);
40+
b.installArtifact(simple);
41+
b.default_step.dependOn(&simple.step);
42+
}
43+
44+
// Examples short
45+
{
46+
const short_mod = b.addModule("simple", .{
47+
.root_source_file = b.path("examples/short.zig"),
48+
.target = target,
49+
.optimize = optimize,
50+
});
51+
52+
const short = b.addExecutable(.{
53+
.name = "short",
54+
.root_module = short_mod,
55+
});
56+
short.root_module.addImport("cli", lib_mod);
57+
b.installArtifact(short);
58+
b.default_step.dependOn(&short.step);
59+
}
4860

4961
// Docs
5062
{

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.{
22
.name = .cli,
33
.fingerprint = 0x48f6513cff9ee2d9,
4-
.version = "0.9.0",
4+
.version = "0.10.0",
55
.paths = .{ "./src", "./build.zig", "./build.zig.zon" },
66
}

examples/standalone/build.zig

Lines changed: 0 additions & 74 deletions
This file was deleted.

examples/standalone/build.zig.zon

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Printer.zig

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,45 @@ const command = @import("./command.zig");
33

44
const Self = @This();
55

6-
out: std.fs.File.Writer,
6+
writer: *std.Io.Writer,
77
use_color: bool,
88

99
const color_clear = "0";
1010

11-
pub fn init(file: std.fs.File, color: command.ColorUsage) Self {
11+
pub fn init(writer: *std.Io.Writer, use_color: bool) Self {
1212
return .{
13-
.out = file.writer(),
14-
.use_color = switch (color) {
15-
.always => true,
16-
.never => false,
17-
.auto => std.posix.isatty(file.handle),
18-
},
13+
.writer = writer,
14+
.use_color = use_color,
1915
};
2016
}
2117

22-
pub inline fn write(self: *const Self, text: []const u8) void {
23-
_ = self.out.write(text) catch unreachable;
18+
pub fn flush(self: *Self) void {
19+
self.writer.flush() catch unreachable;
2420
}
2521

26-
pub inline fn printNewLine(self: *const Self) void {
27-
self.write("\n");
22+
pub inline fn write(self: *Self, text: []const u8) void {
23+
_ = self.writer.writeAll(text) catch unreachable;
2824
}
2925

30-
pub inline fn format(self: *const Self, comptime text: []const u8, args: anytype) void {
31-
std.fmt.format(self.out, text, args) catch unreachable;
26+
pub inline fn printNewLine(self: *Self) void {
27+
self.writer.writeByte('\n') catch unreachable;
3228
}
3329

34-
pub inline fn printColor(self: *const Self, color: []const u8) void {
30+
pub inline fn format(self: *Self, comptime fmt: []const u8, args: anytype) void {
31+
self.writer.print(fmt, args) catch unreachable;
32+
}
33+
34+
pub inline fn printColor(self: *Self, color: []const u8) void {
3535
if (self.use_color)
3636
self.format("{c}[{s}m", .{ 0x1b, color });
3737
}
3838

39-
pub inline fn printInColor(self: *const Self, color: []const u8, text: []const u8) void {
39+
pub inline fn printInColor(self: *Self, color: []const u8, text: []const u8) void {
4040
self.printColor(color);
4141
self.write(text);
4242
self.printColor(color_clear);
4343
}
4444

45-
pub inline fn printSpaces(self: *const Self, cnt: usize) void {
46-
var i: usize = 0;
47-
while (i < cnt) : (i += 1) {
48-
self.write(" ");
49-
}
45+
pub inline fn printSpaces(self: *Self, cnt: usize) void {
46+
self.writer.splatByteAll(' ', cnt) catch unreachable;
5047
}

src/app_runner.zig

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub const AppRunner = struct {
5050
return self.allocSlice(command.Command, args);
5151
}
5252

53-
pub const Error = Allocator.Error;
53+
pub const Error = Allocator.Error || error{WriteFailed};
5454

5555
/// `getAction` returns the action function that should be called by the main app.
5656
pub fn getAction(self: *Self, app: *const App) Error!command.ExecFn {
@@ -66,8 +66,8 @@ pub const AppRunner = struct {
6666
} else |err| {
6767
processError(err, cr.error_data orelse unreachable, app);
6868
if (app.help_config.print_help_on_error) {
69-
_ = std.io.getStdOut().write("\n") catch unreachable;
70-
try help.print_command_help(app, try cr.command_path.toOwnedSlice(), cr.global_options);
69+
_ = std.fs.File.stdout().write("\n") catch unreachable;
70+
try help.print_command_help(app, try cr.command_path.toOwnedSlice(self.orig_allocator), cr.global_options);
7171
}
7272
std.posix.exit(1);
7373
}
@@ -117,7 +117,15 @@ fn processError(err: parser.ParseError, err_data: parser.ErrorData, app: *const
117117
}
118118

119119
pub fn printError(app: *const App, comptime fmt: []const u8, args: anytype) void {
120-
var p = Printer.init(std.io.getStdErr(), app.help_config.color_usage);
120+
var buf: [4096]u8 = undefined;
121+
var stderr = std.fs.File.stderr();
122+
var w = stderr.writer(&buf);
123+
const use_color = switch (app.help_config.color_usage) {
124+
.always => true,
125+
.never => false,
126+
.auto => std.posix.isatty(stderr.handle),
127+
};
128+
var p = Printer.init(&w.interface, use_color);
121129

122130
p.printInColor(app.help_config.color_error, "ERROR");
123131
p.format(": ", .{});

src/help.zig

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,28 @@ pub fn print_command_help(
1414
command_path: []const *const command.Command,
1515
global_options: *const GlobalOptions,
1616
) !void {
17-
const stdout = std.io.getStdOut();
17+
const stdout: std.fs.File = .stdout();
1818
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
1919
const allocator = gpa.allocator();
2020
var arena = std.heap.ArenaAllocator.init(allocator);
21+
defer arena.deinit();
22+
23+
const use_color = switch (global_options.color_usage) {
24+
.always => true,
25+
.never => false,
26+
.auto => std.posix.isatty(stdout.handle),
27+
};
28+
29+
var buf: [4096]u8 = undefined;
30+
var w = stdout.writer(&buf);
31+
var printer = Printer.init(&w.interface, use_color);
32+
defer printer.flush();
2133
var help_printer = HelpPrinter{
2234
.app = app,
23-
.printer = Printer.init(stdout, global_options.color_usage),
35+
.printer = &printer,
2436
.global_options = global_options,
2537
.allocator = arena.allocator(),
2638
};
27-
defer arena.deinit();
2839

2940
if (command_path.len == 1) {
3041
help_printer.printAppHelp(app, command_path);
@@ -35,7 +46,7 @@ pub fn print_command_help(
3546

3647
const HelpPrinter = struct {
3748
app: *const command.App,
38-
printer: Printer,
49+
printer: *Printer,
3950
global_options: *const GlobalOptions,
4051
allocator: Allocator,
4152

@@ -171,7 +182,7 @@ const HelpPrinter = struct {
171182
}
172183
}
173184

174-
fn printOption(self: *const HelpPrinter, option: *const command.Option, option_column_width: usize) void {
185+
fn printOption(self: *HelpPrinter, option: *const command.Option, option_column_width: usize) void {
175186
if (option.short_alias) |alias| {
176187
self.printer.printSpaces(2);
177188
self.printer.printColor(self.app.help_config.color_option);

0 commit comments

Comments
 (0)