this repo has no description
56
fork

Configure Feed

Select the types of activity you want to include in your feed.

cmd: add help and usage

+55 -14
+55 -14
src/main.zig
··· 5 5 6 6 const posix = std.posix; 7 7 8 + const usage = 9 + \\Usage: 10 + \\ els [options] [directory] 11 + \\ 12 + \\ --help Print this message and exit 13 + \\ 14 + \\DISPLAY OPTIONS 15 + \\ -a, --all Show files that start with a dot (ASCII 0x2E) 16 + \\ -A, --almost-all Like --all, but skips implicit "." and ".." directories 17 + \\ --color=WHEN When to use colors (always, auto, never) 18 + \\ --icons=WHEN When to display icons (always, auto, never) 19 + \\ -l, --long Display extended file metadata 20 + ; 21 + 8 22 const Options = struct { 9 23 all: bool = false, 10 24 @"almost-all": bool = false, 11 - color: enum { none, auto, always } = .auto, 25 + color: When = .auto, 12 26 @"group-directories-first": bool = true, 13 - icons: bool = true, 27 + icons: When = .auto, 14 28 long: bool = false, 15 29 16 30 directory: [:0]const u8 = ".", ··· 18 32 isatty: bool = false, 19 33 colors: Colors = .none, 20 34 35 + const When = enum { 36 + never, 37 + auto, 38 + always, 39 + }; 40 + 21 41 const Colors = struct { 22 42 reset: []const u8, 23 43 dir: []const u8, ··· 55 75 56 76 const bold = "\x1b[1m"; 57 77 }; 78 + 58 79 fn useColor(self: Options) bool { 59 80 switch (self.color) { 60 - .none => return false, 81 + .never => return false, 82 + .always => return true, 83 + .auto => return self.isatty, 84 + } 85 + } 86 + 87 + fn useIcons(self: Options) bool { 88 + switch (self.icons) { 89 + .never => return false, 61 90 .always => return true, 62 91 .auto => return self.isatty, 63 92 } ··· 129 158 else if (eql(val, "false")) 130 159 cmd.opts.@"group-directories-first" = false; 131 160 } else if (eql(opt, "color")) { 132 - if (eql(val, "always")) 133 - cmd.opts.color = .always 134 - else if (eql(val, "auto")) 135 - cmd.opts.color = .auto 136 - else if (eql(val, "none")) 137 - cmd.opts.color = .none; 161 + cmd.opts.color = std.meta.stringToEnum(Options.When, val) orelse { 162 + const w = std.io.getStdErr().writer(); 163 + try w.print("Invalid color option: '{s}'", .{val}); 164 + std.process.exit(1); 165 + }; 166 + } else if (eql(opt, "icons")) { 167 + cmd.opts.icons = std.meta.stringToEnum(Options.When, val) orelse { 168 + const w = std.io.getStdErr().writer(); 169 + try w.print("Invalid color option: '{s}'", .{val}); 170 + std.process.exit(1); 171 + }; 172 + } else if (eql(opt, "help")) { 173 + return std.io.getStdErr().writeAll(usage); 138 174 } else { 139 175 const w = std.io.getStdErr().writer(); 140 176 try w.print("Invalid opt: '{s}'", .{opt}); ··· 278 314 try writer.print("{d: >5} ", .{@as(u32, @intCast(time.year))}); 279 315 } 280 316 281 - if (cmd.opts.icons and cmd.opts.isatty) { 317 + if (cmd.opts.useIcons()) { 282 318 const icon = Icon.get(entry); 283 319 284 - try writer.writeAll(icon.color); 285 - try writer.writeAll(icon.icon); 286 - try writer.writeAll(colors.reset); 320 + if (cmd.opts.useColor()) { 321 + try writer.writeAll(icon.color); 322 + try writer.writeAll(icon.icon); 323 + try writer.writeAll(colors.reset); 324 + } else { 325 + try writer.writeAll(icon.icon); 326 + } 327 + 287 328 try writer.writeByte(' '); 288 329 } 289 330 ··· 487 528 488 529 var iter = dir.iterate(); 489 530 while (try iter.next()) |dirent| { 490 - if (!cmd.opts.all and std.mem.startsWith(u8, dirent.name, ".")) continue; 531 + if (!cmd.opts.@"almost-all" and std.mem.startsWith(u8, dirent.name, ".")) continue; 491 532 const nameZ = try cmd.arena.dupeZ(u8, dirent.name); 492 533 try results.append(cmd.arena, .{ 493 534 .name = nameZ,