aboutsummaryrefslogtreecommitdiff
path: root/zig/qm-file-manager.zig
blob: b8b06e54a0e185fc2194f05b5db00aec4e0e1d88 (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const std = @import("std");

const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();

pub fn main() !u8 {
    const action = std.os.getenv("ACTION") orelse return error.MissingAction;
    if (std.mem.eql(u8, action, "get")) {
        try get(std.os.getenv("URL") orelse return error.MissingUrl);
        return 0;
    } else {
        try stdout.print("error unexpected action {s}\n", .{action});
        try bw.flush();
        return 1;
    }
}

fn get(url: []const u8) !void {
    try stdout.print("title {s}\n", .{url});
    try stdout.print("add_tab\n", .{});

    if (!std.mem.eql(u8, url, "/")) {
        try stdout.print("title Go to parent directory\n", .{});
        try stdout.print("url ..\n", .{});
        try stdout.print("thumbnail_url folder.png\n", .{});
        try stdout.print("thumbnail_size 32x32\n", .{});
        try stdout.print("add_body_item\n", .{});
    }

    var dirIt = try std.fs.cwd().openIterableDir(url, .{ .no_follow = true });
    defer dirIt.close();

    var pathBuf: [std.fs.MAX_PATH_BYTES]u8 = undefined;

    var it = dirIt.iterate();
    while (try it.next()) |*entry| {
        var fba = std.heap.FixedBufferAllocator.init(&pathBuf);
        var allocator = fba.allocator();
        const fullFilepath = try std.fs.path.join(allocator, &[_][]const u8{ url, entry.name });

        try stdout.print("title {s}\n", .{fullFilepath});
        try stdout.print("url {s}\n", .{fullFilepath});
        if (entry.kind == .directory) {
            try stdout.print("thumbnail_url folder.png\n", .{});
        } else {
            try stdout.print("thumbnail_url file.png\n", .{});
        }
        try stdout.print("thumbnail_size 32x32\n", .{});
        try stdout.print("add_body_item\n", .{});
    }

    try bw.flush();
}