aboutsummaryrefslogtreecommitdiff
path: root/zig
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2023-10-30 10:23:49 +0100
committerdec05eba <dec05eba@protonmail.com>2023-10-30 10:37:58 +0100
commitf2bbfe83b4b23e87d8a049a2d698c6576a5bcd8c (patch)
tree0734cabcfe51036a011e46cd83ae7ac15c5dfa95 /zig
Initial commit
Diffstat (limited to 'zig')
-rw-r--r--zig/qm-file-manager.zig54
1 files changed, 54 insertions, 0 deletions
diff --git a/zig/qm-file-manager.zig b/zig/qm-file-manager.zig
new file mode 100644
index 0000000..b8b06e5
--- /dev/null
+++ b/zig/qm-file-manager.zig
@@ -0,0 +1,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();
+}