Просмотр исходного кода

Move error handling outside of runCommand

Luna Stadler лет назад: 4
Родитель
Сommit
be8669a611
1 измененных файлов с 4 добавлено и 4 удалено
  1. 4 4
      zig/sdl/hello_sdl.zig

+ 4 - 4
zig/sdl/hello_sdl.zig

113
                                msg[pos] = '_';
113
                                msg[pos] = '_';
114
                            },
114
                            },
115
                            c.SDLK_RETURN => {
115
                            c.SDLK_RETURN => {
116
                                result = runCommand(&msg, gpa);
116
                                result = try runCommand(&msg, gpa);
117
                                var i: usize = 0;
117
                                var i: usize = 0;
118
                                while (i < max_chars) : (i += 1) {
118
                                while (i < max_chars) : (i += 1) {
119
                                    msg[i] = ' ';
119
                                    msg[i] = ' ';
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
    const cmd = std.mem.trim(u8, std.mem.sliceTo(raw_cmd, 0), &std.ascii.spaces);
157
    const cmd = std.mem.trim(u8, std.mem.sliceTo(raw_cmd, 0), &std.ascii.spaces);
158
    const argv = if (std.mem.startsWith(u8, cmd, "go "))
158
    const argv = if (std.mem.startsWith(u8, cmd, "go "))
159
        &[_][]const u8{ "go", "doc", cmd[3..] }
159
        &[_][]const u8{ "go", "doc", cmd[3..] }
162
    for (argv) |arg| {
162
    for (argv) |arg| {
163
        std.debug.print("'{s}' ", .{arg});
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
    std.mem.copy(u8, buf, result.stdout[0..std.math.min(100, result.stdout.len)]);
167
    std.mem.copy(u8, buf, result.stdout[0..std.math.min(100, result.stdout.len)]);
168
    var i: usize = result.stdout.len;
168
    var i: usize = result.stdout.len;
169
    while (i < buf.len) : (i += 1) {
169
    while (i < buf.len) : (i += 1) {