From 286b07c14e9dcc9072aa3ffdfee80628f281439f Mon Sep 17 00:00:00 2001 From: jiacai2050 Date: Thu, 29 Jun 2023 20:22:41 +0800 Subject: [PATCH] refactor: add version support, pidof support user_only option --- README.org | 60 +++++++++----------------------------------------- build.zig.zon | 4 ++-- src/common.zig | 5 ----- src/loc.zig | 13 ++++++++--- src/pidof.zig | 20 +++++++++++------ src/tree.zig | 15 ++++++++++--- src/util.zig | 8 +++++++ 7 files changed, 56 insertions(+), 69 deletions(-) delete mode 100644 src/common.zig diff --git a/README.org b/README.org index 72cd528..901c4bd 100644 --- a/README.org +++ b/README.org @@ -1,6 +1,6 @@ #+TITLE: Zigcli #+DATE: 2022-09-20T22:55:17+0800 -#+LASTMOD: 2023-06-28T20:22:53+0800 +#+LASTMOD: 2023-06-29T20:22:28+0800 #+AUTHOR: Jiacai Liu #+EMAIL: dev@liujiacai.net #+OPTIONS: toc:nil num:nil @@ -21,7 +21,7 @@ git clone https://github.com/jiacai2050/zigcli.git #+end_src Then build with #+begin_src bash -zig build -Doptimize=ReleaseFast +make build #+end_src #+RESULTS: @@ -39,36 +39,12 @@ Zigcli currently supports Zig master, which can be downloaded [[https://ziglang. zig-out └──bin ├──loc - └──tree + ├──pidof + ├──tree + └──yes -1 directories, 2 files +1 directories, 4 files #+end_src - -#+begin_src bash :results verbatim code :exports both -./zig-out/bin/tree -h -#+end_src - -#+RESULTS: -#+begin_src bash - USAGE: - ./zig-out/bin/tree [OPTIONS] [--] [directory] - - OPTIONS: - -m, --mode STRING Line drawing characters. (valid: ascii|box|dos)(default: box) - -a, --all All files are printed. - -s, --size Print the size of each file in bytes along with the name. - -d, --directory List directories only. - -L, --level INTEGER Max display depth of the directory tree. - -h, --help Prints help information. -#+end_src - -** Yes -Here [[https://linux.die.net/man/1/pv][pv]] is used to monitor io rate. -#+begin_src bash -./zig-out/bin/yes | pv -r >/dev/null -#+end_src -=[6.71GiB/s]= - * Loc #+begin_src bash :results verbatim code :exports both ./zig-out/bin/loc @@ -78,28 +54,14 @@ Here [[https://linux.die.net/man/1/pv][pv]] is used to monitor io rate. #+begin_src bash Language File Line Code Comment Blank Size -------- ---- ---- ---- ------- ----- ------- -Zig 7 896 756 22 118 24.72K -YAML 6 145 134 3 8 3.19K -Makefile 1 15 8 3 4 262.00B +Zig 7 1057 896 32 129 29.91K +YAML 6 164 154 2 8 3.96K +Makefile 1 12 8 0 4 206.00B Python 1 10 7 2 1 166.00B -C 1 9 2 6 1 34.00B +C 1 9 2 4 3 34.00B Ruby 1 8 5 2 1 201.00B -------- ---- ---- ---- ------- ----- ------- -Total 17 1083 912 38 133 28.56K -#+end_src - -#+begin_src bash :results verbatim code :exports both -./zig-out/bin/loc -h -#+end_src - -#+RESULTS: -#+begin_src bash - USAGE: - ./zig-out/bin/loc [OPTIONS] [file or directory] - - OPTIONS: - -s, --sort STRING Column to sort by (valid: language|file|line|code|comment|blank|size)(default: line) - -h, --help Prints help information +Total 17 1260 1072 42 146 34.46K #+end_src * Roadmap diff --git a/build.zig.zon b/build.zig.zon index 0f92359..e9667dc 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -3,8 +3,8 @@ .version = "0.1.0", .dependencies = .{ .simargs = .{ - .url = "https://github.com/jiacai2050/simargs/archive/77eb191.tar.gz", - .hash = "1220eef7753fccb692bbebae6b4ee2bab7900e954b5598ea25649ade4c062481cb49", + .url = "https://github.com/jiacai2050/simargs/archive/1003b5c.tar.gz", + .hash = "1220b5dafa54081ce54f108d0b7fa9451149bde7dc7c20f4e14e322fdeeb7a1dfd9e", }, .@"table-helper" = .{ .url = "https://github.com/jiacai2050/table-helper/archive/f0c8eb9.tar.gz", diff --git a/src/common.zig b/src/common.zig deleted file mode 100644 index 5e76355..0000000 --- a/src/common.zig +++ /dev/null @@ -1,5 +0,0 @@ -const info = @import("build_info"); - -pub fn print_build_info(writer: anytype) !void { - try writer.print("Build date: {s}\nGit commit: {s}", .{ info.build_date, info.git_commit }); -} diff --git a/src/loc.zig b/src/loc.zig index 291b64d..dc8a7e4 100644 --- a/src/loc.zig +++ b/src/loc.zig @@ -1,7 +1,8 @@ const std = @import("std"); const Table = @import("table-helper").Table; const simargs = @import("simargs"); -const StringUtil = @import("util.zig").StringUtil; +const util = @import("util.zig"); +const StringUtil = util.StringUtil; const fs = std.fs; const IGNORE_DIRS = [_][]const u8{ ".git", "zig-cache", "zig-out", "target", "vendor", "node_modules", "out" }; @@ -181,15 +182,21 @@ pub fn main() !void { const opt = try simargs.parse(allocator, struct { sort: Column = .line, + version: bool = false, help: bool = false, pub const __shorts__ = .{ .sort = .s, + .version = .v, .help = .h, }; - pub const __messages__ = .{ .help = "Prints help information", .sort = "Column to sort by" }; - }, "[file or directory]"); + pub const __messages__ = .{ + .help = "Print help information", + .version = "Print version", + .sort = "Column to sort by", + }; + }, "[file or directory]", util.get_build_info()); defer opt.deinit(); const file_or_dir = if (opt.positional_args.items.len == 0) diff --git a/src/pidof.zig b/src/pidof.zig index 408af0f..8d9ee87 100644 --- a/src/pidof.zig +++ b/src/pidof.zig @@ -4,26 +4,30 @@ const std = @import("std"); const simargs = @import("simargs"); -const common = @import("common.zig"); +const util = @import("util.zig"); const c = @cImport({ @cInclude("sys/sysctl.h"); + @cInclude("unistd.h"); }); pub const Options = struct { single: bool = false, separator: []const u8 = " ", + user_only: bool = false, version: bool = false, help: bool = false, pub const __shorts__ = .{ .single = .s, .separator = .S, + .user_only = .u, .version = .v, .help = .h, }; pub const __messages__ = .{ .single = "Single shot - this instructs the program to only return one pid.", .separator = "Use separator as a separator put between pids.", + .user_only = "Only show process belonging to current user.", .version = "Print version.", .help = "Print help message.", }; @@ -52,11 +56,18 @@ pub fn findPids(allocator: std.mem.Allocator, opt: Options, program: []const u8) // procSize may change between two calls of sysctl, so we cannot iterate // procList directly with for(procList) |proc|. var pids = std.ArrayList(c.pid_t).init(allocator); + const uid = if (opt.user_only) c.getuid() else null; for (0..procSize / @sizeOf(c.struct_kinfo_proc)) |i| { if (opt.single and pids.items.len == 1) { break; } const proc = procList[i]; + if (uid) |id| { + if (id != proc.kp_eproc.e_pcred.p_ruid) { + continue; + } + } + // p_comm is [17]u8 const name = std.mem.sliceTo(&proc.kp_proc.p_comm, 0); if (program.len >= name.len) { @@ -74,14 +85,9 @@ pub fn main() !void { defer arena.deinit(); const allocator = arena.allocator(); - const opt = try simargs.parse(allocator, Options, "[program]"); + const opt = try simargs.parse(allocator, Options, "[program]", util.get_build_info()); defer opt.deinit(); - if (opt.args.version) { - try common.print_build_info(std.io.getStdOut().writer()); - return; - } - if (opt.positional_args.items.len == 0) { std.debug.print("program is not given", .{}); std.os.exit(1); diff --git a/src/tree.zig b/src/tree.zig index 1c94a7e..b434a3c 100644 --- a/src/tree.zig +++ b/src/tree.zig @@ -7,7 +7,8 @@ const std = @import("std"); const simargs = @import("simargs"); -const StringUtil = @import("util.zig").StringUtil; +const util = @import("util.zig"); +const StringUtil = util.StringUtil; const process = std.process; const fs = std.fs; const mem = std.mem; @@ -50,6 +51,7 @@ pub const WalkOptions = struct { size: bool = false, directory: bool = false, level: ?usize, + version: bool = false, help: bool = false, pub const __shorts__ = .{ @@ -58,6 +60,7 @@ pub const WalkOptions = struct { .size = .s, .directory = .d, .level = .L, + .version = .v, .help = .h, }; @@ -67,7 +70,8 @@ pub const WalkOptions = struct { .size = "Print the size of each file in bytes along with the name.", .directory = "List directories only.", .level = "Max display depth of the directory tree.", - .help = "Prints help information.", + .version = "Print version.", + .help = "Print help information.", }; }; @@ -76,7 +80,12 @@ pub fn main() anyerror!void { defer arena.deinit(); const allocator = arena.allocator(); - const opt = try simargs.parse(allocator, WalkOptions, "[directory]"); + const opt = try simargs.parse( + allocator, + WalkOptions, + "[directory]", + util.get_build_info(), + ); defer opt.deinit(); const root_dir = if (opt.positional_args.items.len == 0) diff --git a/src/util.zig b/src/util.zig index 6042206..9adf118 100644 --- a/src/util.zig +++ b/src/util.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const info = @import("build_info"); const mem = std.mem; pub const StringUtil = struct { @@ -14,3 +15,10 @@ pub const StringUtil = struct { return std.fmt.allocPrint(allocator, "{d:.2}{s}", .{ remaining, SIZE_UNIT[i] }); } }; + +pub fn get_build_info() []const u8 { + return std.fmt.comptimePrint("Build date: {s}\nGit commit: {s}", .{ + info.build_date, + info.git_commit, + }); +}