authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-05-07 20:05:08 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-05-07 20:05:08 -07:00
logab662f27cf388cb3ce1a93dbd77c1f5b290672b7
tree421a4e098854fb123818430d79e420ad6e7773fc
parent611db8a779a7121ceefc766ef89d49412bf161ee

add `zpm search` command


2 files changed, 83 insertions(+), 0 deletions(-)

src/cmd/zpm.zig+2
......@@ -13,6 +13,7 @@ pub const commands = struct {
1313 pub const add = @import("./zpm/add.zig");
1414 pub const showjson = @import("./zpm/showjson.zig");
1515 pub const tags = @import("./zpm/tags.zig");
16 pub const search = @import("./zpm/search.zig");
1617};
1718
1819pub const server_root = "https://zpm.random-projects.net/api";
......@@ -37,6 +38,7 @@ pub fn execute(args: [][]u8) !void {
3738 \\ - add Append this package to your dependencies
3839 \\ - showjson Print raw json from queried API responses
3940 \\ - tags Print the list of tags available on the server.
41 \\ - search Search the api for available packages and print them.
4042 });
4143 return;
4244 }
src/cmd/zpm/search.zig created+81
......@@ -0,0 +1,81 @@
1const std = @import("std");
2const gpa = std.heap.c_allocator;
3const out = std.io.getStdOut().writer();
4
5const zfetch = @import("zfetch");
6const json = @import("json");
7const range = @import("range").range;
8
9const u = @import("./../../util/index.zig");
10const zpm = @import("./../zpm.zig");
11
12//
13//
14
15pub fn execute(args: [][]u8) !void {
16 const url = try std.mem.join(gpa, "/", &.{ zpm.server_root, "packages" });
17 const val = try zpm.server_fetch(url);
18
19 const arr = &std.ArrayList(zpm.Package).init(gpa);
20 defer arr.deinit();
21
22 for (val.Array) |item| {
23 if (item.get("root_file")) |_| {} else {
24 continue;
25 }
26 try arr.append(zpm.Package{
27 .name = item.get("name").?.String,
28 .author = item.get("author").?.String,
29 .description = item.get("description").?.String,
30 .tags = &.{},
31 .git = "",
32 .root_file = "",
33 });
34 }
35 const list = arr.items;
36
37 const name_col_width = blk: {
38 var w: usize = 4;
39 for (list) |pkg| {
40 const len = pkg.name.len;
41 if (len > w) {
42 w = len;
43 }
44 }
45 break :blk w + 2;
46 };
47
48 const author_col_width = blk: {
49 var w: usize = 6;
50 for (list) |pkg| {
51 const len = pkg.author.len;
52 if (len > w) {
53 w = len;
54 }
55 }
56 break :blk w + 2;
57 };
58
59 try out.writeAll("NAME");
60 try print_c_n(' ', name_col_width - 4);
61 try out.writeAll("AUTHOR");
62 try print_c_n(' ', author_col_width - 6);
63 try out.writeAll("DESCRIPTION\n");
64
65 for (list) |pkg| {
66 try out.writeAll(pkg.name);
67 try print_c_n(' ', name_col_width - pkg.name.len);
68
69 try out.writeAll(pkg.author);
70 try print_c_n(' ', author_col_width - pkg.author.len);
71
72 try out.writeAll(pkg.description);
73 try out.writeAll("\n");
74 }
75}
76
77fn print_c_n(c: u8, n: usize) !void {
78 for (range(n)) |_| {
79 try out.writeAll(&.{c});
80 }
81}