~mil/mepo

cec69b56b4319da149f5a5a8f252117a74f9732c — Miles Alan 2 years ago ba9f2de xdg-compat
Handle XDG_CACHE_HOME in addition to XDG_CONFIG_HOME
4 files changed, 26 insertions(+), 9 deletions(-)

M src/Downloader.zig
M src/api/clipcopy.zig
M src/config.zig
M src/util/utilfile.zig
M src/Downloader.zig => src/Downloader.zig +1 -1
@@ 5,7 5,7 @@ const TileCache = @import("./TileCache.zig");
pub fn cache_dlbbox(tile_cache: *TileCache, download_request_str: []const u8) !void {
    std.debug.print("Downloading based on BBox request: {s}\n", .{download_request_str});

    try tile_cache.set_cache_dir("~/.cache/mepo/tiles");
    try tile_cache.set_cache_dir("$XDG_CACHE_HOME/mepo/tiles");
    try tile_cache.set_cache_url("https://tile.openstreetmap.org/%3$d/%1$d/%2$d.png");

    const delay_seconds = 3;

M src/api/clipcopy.zig => src/api/clipcopy.zig +1 -1
@@ 15,7 15,7 @@ fn execute(mepo: *Mepo, _: [types.MepoFnNargs]types.MepoArg) !void {
    const copy_text = try std.fmt.allocPrintZ(
        mepo.allocator,
        "{d:.5}, {d:.5}",
        .{mepo.lat, mepo.lon},
        .{ mepo.lat, mepo.lon },
    );
    defer mepo.allocator.free(copy_text);
    try utilsdl.errorcheck(sdl.SDL_SetClipboardText(copy_text));

M src/config.zig => src/config.zig +1 -1
@@ 115,7 115,7 @@ pub const DefaultBaseConfig =
    \\ prefset zoom 14;
    \\ prefset tile_cache_network 1;
    \\ prefset tile_cache_max_n_transfers 20;
    \\ prefset tile_cache_dir ~/.cache/mepo/tiles;
    \\ prefset tile_cache_dir $XDG_CACHE_HOME/mepo/tiles;
    \\ prefset tile_cache_url https://tile.openstreetmap.org/%3$d/%1$d/%2$d.png;
    \\ # E.g. default 30 day expiry: 60s * 60m * 24h * 30 days;
    \\ prefset tile_cache_expiry_seconds 2592000;

M src/util/utilfile.zig => src/util/utilfile.zig +23 -6
@@ 6,17 6,34 @@ const cstdlib = @cImport({
    @cInclude("stdlib.h");
});


/// Runs underlying wordexp (see man 3 wordexp) to expand globs & handle tilde
/// expansion.
///
/// Caller responsible for freeing returned ptr memory
pub fn wordexp_filepath(allocator: std.mem.Allocator, path: []const u8) ![:0]const u8 {
    if (std.os.getenv("XDG_CONFIG_HOME") == null) {
        if (std.os.getenv("HOME")) |home| {
            const xdg_home = try std.fmt.allocPrintZ(allocator, "{s}/.config", .{home});
            defer allocator.free(xdg_home);
            _ = cstdlib.setenv("XDG_CONFIG_HOME", xdg_home, 1);

    // Within config, we assume user can pass XDG_{CONFIG,CACHE}_HOME to
    // fileload etc. and callers of this fn; so here we just follow XDG default
    // and provide default XDG dir if unset; e.g. if XDG_CONFIG_HOME unset
    // use ~/.config
    for (&[_]struct {
        env_var: [:0]const u8,
        default_home_path: [:0]const u8,
    }{
        .{
            .env_var = "XDG_CONFIG_HOME",
            .default_home_path = ".config",
        },
        .{
            .env_var = "XDG_CACHE_HOME",
            .default_home_path = ".cache",
        },
    }) |default_mapping| {
        if (std.os.getenv(default_mapping.env_var) != null) continue;
        if (std.os.getenv("HOME")) |home_dir| {
            const value = try std.fmt.allocPrintZ(allocator, "{s}/{s}", .{ home_dir, default_mapping.default_home_path });
            defer allocator.free(value);
            _ = cstdlib.setenv(default_mapping.env_var, value, 1);
        }
    }