~mil/mepo

0bfdd22eb4f3c4b04f83fa464f57cdaf973aa48f — Miles Alan 3 months ago 50303e3
shellpipe: break out env vars map creation to helper fn
1 files changed, 35 insertions(+), 33 deletions(-)

M src/api/shellpipe.zig
M src/api/shellpipe.zig => src/api/shellpipe.zig +35 -33
@@ 46,7 46,7 @@ fn shellpipe(mepo: *Mepo, as_async: bool, cmd: []const u8) !void {
}

fn async_shellpipe_run(userdata: ?*anyopaque) callconv(.C) c_int {
    var shellpipe_request : *ShellpipeRequest = @ptrCast(*ShellpipeRequest, @alignCast(@alignOf(*ShellpipeRequest), userdata.?));
    var shellpipe_request: *ShellpipeRequest = @ptrCast(*ShellpipeRequest, @alignCast(@alignOf(*ShellpipeRequest), userdata.?));
    // TODO: defer free cmd
    shellpipe_run(shellpipe_request.mepo, shellpipe_request.cmd, true) catch return 1;
    return 0;


@@ 65,38 65,9 @@ fn shellpipe_run(mepo: *Mepo, cmd: []const u8, as_async: bool) !void {
        try std.fmt.allocPrint(arena.allocator(), "Shellpipe ({s}) in progress - run!", .{cmd}),
    );
    try refresh_ui(mepo, as_async);
    const env_vars = env_vars: {
        var v = try std.process.getEnvMap(arena.allocator());
        const bbox = mepo.bounding_box();

        // TODO: maybe this should be in seperate/helper fn?
        var cursor_x: c_int = undefined;
        var cursor_y: c_int = undefined;
        _ = sdl.SDL_GetMouseState(&cursor_x, &cursor_y);
        const cursor_lat = utilconversion.px_y_to_lat(mepo.get_y() - @divTrunc(@intCast(i32, mepo.win_h), 2) + cursor_y, mepo.zoom);
        const cursor_lon = utilconversion.px_x_to_lon(mepo.get_x() - @divTrunc(@intCast(i32, mepo.win_w), 2) + cursor_x, mepo.zoom);

        // Window and zoom info
        try v.put("MEPO_WIN_W", try std.fmt.allocPrint(arena.allocator(), "{d}", .{mepo.win_w}));
        try v.put("MEPO_WIN_H", try std.fmt.allocPrint(arena.allocator(), "{d}", .{mepo.win_h}));
        try v.put("MEPO_ZOOM", try std.fmt.allocPrint(arena.allocator(), "{d}", .{mepo.zoom}));

        // Center and bbox
        try v.put("MEPO_CENTER_LAT", try std.fmt.allocPrint(arena.allocator(), "{d}", .{mepo.lat}));
        try v.put("MEPO_CENTER_LON", try std.fmt.allocPrint(arena.allocator(), "{d}", .{mepo.lon}));
        try v.put("MEPO_TL_LAT", try std.fmt.allocPrint(arena.allocator(), "{d}", .{bbox.topleft_lat}));
        try v.put("MEPO_TL_LON", try std.fmt.allocPrint(arena.allocator(), "{d}", .{bbox.topleft_lon}));
        try v.put("MEPO_BR_LAT", try std.fmt.allocPrint(arena.allocator(), "{d}", .{bbox.bottomright_lat}));
        try v.put("MEPO_BR_LON", try std.fmt.allocPrint(arena.allocator(), "{d}", .{bbox.bottomright_lon}));

        // Cursor position
        try v.put("MEPO_CURSOR_LAT", try std.fmt.allocPrint(arena.allocator(), "{d}", .{cursor_lat}));
        try v.put("MEPO_CURSOR_LON", try std.fmt.allocPrint(arena.allocator(), "{d}", .{cursor_lon}));

        break :env_vars v;
    };

    const args = [_][]const u8{ "sh", "-c", cmd};
    const env_vars = try get_env_vars(mepo, arena.allocator());

    const args = [_][]const u8{ "sh", "-c", cmd };
    const process_result = try std.ChildProcess.exec(.{
        .allocator = arena.allocator(),
        .argv = args[0..],


@@ 120,3 91,34 @@ fn shellpipe_run(mepo: *Mepo, cmd: []const u8, as_async: bool) !void {
fn refresh_ui(mepo: *Mepo, as_async: bool) !void {
    if (as_async) utilsdl.sdl_push_resize_event() else try mepo.blit();
}

fn get_env_vars(mepo: *Mepo, allocator: std.mem.Allocator) !std.BufMap {
    var v = try std.process.getEnvMap(allocator);
    const bbox = mepo.bounding_box();

    // TODO: maybe this should be in seperate/helper fn?
    var cursor_x: c_int = undefined;
    var cursor_y: c_int = undefined;
    _ = sdl.SDL_GetMouseState(&cursor_x, &cursor_y);
    const cursor_lat = utilconversion.px_y_to_lat(mepo.get_y() - @divTrunc(@intCast(i32, mepo.win_h), 2) + cursor_y, mepo.zoom);
    const cursor_lon = utilconversion.px_x_to_lon(mepo.get_x() - @divTrunc(@intCast(i32, mepo.win_w), 2) + cursor_x, mepo.zoom);

    // Window and zoom info
    try v.put("MEPO_WIN_W", try std.fmt.allocPrint(allocator, "{d}", .{mepo.win_w}));
    try v.put("MEPO_WIN_H", try std.fmt.allocPrint(allocator, "{d}", .{mepo.win_h}));
    try v.put("MEPO_ZOOM", try std.fmt.allocPrint(allocator, "{d}", .{mepo.zoom}));

    // Center and bbox
    try v.put("MEPO_CENTER_LAT", try std.fmt.allocPrint(allocator, "{d}", .{mepo.lat}));
    try v.put("MEPO_CENTER_LON", try std.fmt.allocPrint(allocator, "{d}", .{mepo.lon}));
    try v.put("MEPO_TL_LAT", try std.fmt.allocPrint(allocator, "{d}", .{bbox.topleft_lat}));
    try v.put("MEPO_TL_LON", try std.fmt.allocPrint(allocator, "{d}", .{bbox.topleft_lon}));
    try v.put("MEPO_BR_LAT", try std.fmt.allocPrint(allocator, "{d}", .{bbox.bottomright_lat}));
    try v.put("MEPO_BR_LON", try std.fmt.allocPrint(allocator, "{d}", .{bbox.bottomright_lon}));

    // Cursor position
    try v.put("MEPO_CURSOR_LAT", try std.fmt.allocPrint(allocator, "{d}", .{cursor_lat}));
    try v.put("MEPO_CURSOR_LON", try std.fmt.allocPrint(allocator, "{d}", .{cursor_lon}));

    return v;
}