Pārlūkot izejas kodu

Move error handling outside of runCommand

Luna Stadler 4 gadi atpakaļ
vecāks
revīzija
be8669a611
1 mainītis faili ar 4 papildinājumiem un 4 dzēšanām
  1. 4 4
      zig/sdl/hello_sdl.zig

+ 4 - 4
zig/sdl/hello_sdl.zig

@ -113,7 +113,7 @@ pub fn main() !void {
113 113
                                msg[pos] = '_';
114 114
                            },
115 115
                            c.SDLK_RETURN => {
116
                                result = runCommand(&msg, gpa);
116
                                result = try runCommand(&msg, gpa);
117 117
                                var i: usize = 0;
118 118
                                while (i < max_chars) : (i += 1) {
119 119
                                    msg[i] = ' ';
@ -153,7 +153,7 @@ pub fn main() !void {
153 153
    }
154 154
}
155 155
156
fn runCommand(raw_cmd: []const u8, allocator: *std.mem.Allocator) ?[*:0]u8 {
156
fn runCommand(raw_cmd: []const u8, allocator: *std.mem.Allocator) !?[*:0]u8 {
157 157
    const cmd = std.mem.trim(u8, std.mem.sliceTo(raw_cmd, 0), &std.ascii.spaces);
158 158
    const argv = if (std.mem.startsWith(u8, cmd, "go "))
159 159
        &[_][]const u8{ "go", "doc", cmd[3..] }
@ -162,8 +162,8 @@ fn runCommand(raw_cmd: []const u8, allocator: *std.mem.Allocator) ?[*:0]u8 {
162 162
    for (argv) |arg| {
163 163
        std.debug.print("'{s}' ", .{arg});
164 164
    }
165
    const result = std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv }) catch |err| return "oopsie";
166
    const buf = allocator.allocSentinel(u8, 100, 0) catch |err| return "alloc";
165
    const result = try std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv });
166
    const buf = try allocator.allocSentinel(u8, 100, 0);
167 167
    std.mem.copy(u8, buf, result.stdout[0..std.math.min(100, result.stdout.len)]);
168 168
    var i: usize = result.stdout.len;
169 169
    while (i < buf.len) : (i += 1) {