地圖 (Jido) is a lightweight Unix TUI file explorer designed for speed and simplicity.
at v0.2.0 66 lines 1.8 kB view raw
1const std = @import("std"); 2const builtin = @import("builtin"); 3 4const log = &@import("./log.zig").log; 5 6pub fn get_home_dir() !?std.fs.Dir { 7 return try std.fs.openDirAbsolute(std.posix.getenv("HOME") orelse { 8 return null; 9 }, .{ .iterate = true }); 10} 11 12pub fn get_xdg_config_home_dir() !?std.fs.Dir { 13 return try std.fs.openDirAbsolute(std.posix.getenv("XDG_CONFIG_HOME") orelse { 14 return null; 15 }, .{ .iterate = true }); 16} 17 18pub fn get_editor() ?[]const u8 { 19 const editor = std.posix.getenv("EDITOR"); 20 if (editor) |e| { 21 if (std.mem.trim(u8, e, " ").len > 0) { 22 return e; 23 } 24 } 25 return null; 26} 27 28pub fn open_file(alloc: std.mem.Allocator, dir: std.fs.Dir, file: []const u8, editor: []const u8) !void { 29 var path_buf: [std.fs.max_path_bytes]u8 = undefined; 30 const path = try dir.realpath(file, &path_buf); 31 32 var child = std.process.Child.init(&.{ editor, path }, alloc); 33 _ = try child.spawnAndWait(); 34} 35 36pub fn file_exists(dir: std.fs.Dir, path: []const u8) bool { 37 const result = blk: { 38 _ = dir.openFile(path, .{}) catch |err| { 39 switch (err) { 40 error.FileNotFound => break :blk false, 41 else => { 42 log.info("{}", .{err}); 43 break :blk true; 44 }, 45 } 46 }; 47 break :blk true; 48 }; 49 return result; 50} 51 52pub fn dir_exists(dir: std.fs.Dir, path: []const u8) bool { 53 const result = blk: { 54 _ = dir.openDir(path, .{}) catch |err| { 55 switch (err) { 56 error.FileNotFound => break :blk false, 57 else => { 58 log.info("{}", .{err}); 59 break :blk true; 60 }, 61 } 62 }; 63 break :blk true; 64 }; 65 return result; 66}