summaryrefslogtreecommitdiff
path: root/xdg.zig
blob: 8466066a1ca7916da78aa5564473b1406e5c2d79 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const std = @import("std");

const Allocator = std.mem.Allocator;
const Dir = std.fs.Dir;

pub fn getDataHome(allocator: Allocator, app_name: []const u8) ![]u8 {
    if (std.os.getenv("XDG_DATA_HOME")) |data_home| {
        return std.fs.path.join(allocator, &.{ data_home, app_name });
    }

    if (std.os.getenv("HOME")) |home| {
        return std.fs.path.join(allocator, &.{ home, ".local", "share", app_name });
    }

    return error.HomeNotFound;
}

pub fn getBinHome(allocator: Allocator) ![]u8 {
    if (std.os.getenv("HOME")) |home| {
        return std.fs.path.join(allocator, &.{ home, ".local", "bin" });
    }

    return error.HomeNotFound;
}

pub fn openDataHome(allocator: Allocator, app_name: []const u8) !Dir {
    var data_home = try getDataHome(allocator, app_name);
    defer allocator.free(data_home);
    return try std.fs.cwd().makeOpenPath(data_home, .{});
}

pub fn openBinHome(allocator: Allocator) !Dir {
    var bin_home = try getBinHome(allocator);
    defer allocator.free(bin_home);
    return try std.fs.cwd().makeOpenPath(bin_home, .{});
}