Skip to content

Commit

Permalink
refactor: add version support, pidof support user_only option
Browse files Browse the repository at this point in the history
  • Loading branch information
jiacai2050 committed Jun 29, 2023
1 parent 07422c1 commit 286b07c
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 69 deletions.
60 changes: 11 additions & 49 deletions README.org
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 0 additions & 5 deletions src/common.zig

This file was deleted.

13 changes: 10 additions & 3 deletions src/loc.zig
Original file line number Diff line number Diff line change
@@ -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" };
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 13 additions & 7 deletions src/pidof.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
};
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
15 changes: 12 additions & 3 deletions src/tree.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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__ = .{
Expand All @@ -58,6 +60,7 @@ pub const WalkOptions = struct {
.size = .s,
.directory = .d,
.level = .L,
.version = .v,
.help = .h,
};

Expand All @@ -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.",
};
};

Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/util.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const info = @import("build_info");
const mem = std.mem;

pub const StringUtil = struct {
Expand All @@ -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,
});
}

0 comments on commit 286b07c

Please sign in to comment.