Browse Source

Run line through qalc on enter

Neat!  Currency conversion, math, cm to inches, things!

Also I was struggling with this so much and in the end the fix was to
put `[]const u8` in the signature instead of `?[*:0]u8`.  No idea why, I
thought I had already tried that, but that did the trick.  Well.
Luna Stadler 4 years ago
parent
commit
0b40b45393
1 changed files with 15 additions and 3 deletions
  1. 15 3
      zig/sdl/hello_sdl.zig

+ 15 - 3
zig/sdl/hello_sdl.zig

@ -151,7 +151,19 @@ pub fn main() !void {
151 151
    }
152 152
}
153 153
154
fn runCommand(cmd: ?[*:0]u8) ?[*:0]u8 {
155
    c.SDL_Log("cmd is '%s'", cmd);
156
    return "unknown command";
154
fn runCommand(cmd: []const u8, allocator: *std.mem.Allocator) ?[*:0]u8 {
155
    const argv = &[_][]const u8{ "/usr/bin/qalc", "-terse", cmd };
156
    const result = std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv }) catch |err| return "oopsie";
157
    const buf = allocator.allocSentinel(u8, 100, 0) catch |err| return "alloc";
158
    std.mem.copy(u8, buf, result.stdout);
159
    var i: usize = result.stdout.len;
160
    while (i < buf.len) : (i += 1) {
161
        buf[i] = ' ';
162
    }
163
    buf[buf.len - 1] = 0;
164
    defer {
165
        allocator.free(result.stdout);
166
        allocator.free(result.stderr);
167
    }
168
    return buf;
157 169
}