aboutsummaryrefslogtreecommitdiff
path: root/zig
diff options
context:
space:
mode:
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();
+}