From 9198cb76429bab4483a0bc0ec99255fc80650ffc Mon Sep 17 00:00:00 2001 From: Nathan Anderson Date: Tue, 24 Oct 2023 17:56:49 -0600 Subject: [PATCH] Working initial version with linux and firefox limitation, added `ref` and `std` subcommands --- src/main.zig | 60 +++++++++++++++++++++++++++++++++++----------------- src/zim.zig | 59 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 97 insertions(+), 22 deletions(-) diff --git a/src/main.zig b/src/main.zig index 4366388..56520c3 100644 --- a/src/main.zig +++ b/src/main.zig @@ -10,12 +10,14 @@ pub const ZIM_BUILD = 1; /// The availble arguments for the ZiM program const Args = enum(u8) { + gir, help, init, install, list, + ref, + std, use, - gir, version, }; @@ -41,6 +43,8 @@ fn get_arg_parse_type(arg: Args) ArgType { .init => ArgType.single, .install => ArgType.expectsOneParam, .list => ArgType.expectsOneParam, + .ref => ArgType.single, + .std => ArgType.single, .use => ArgType.expectsOneParam, .version => ArgType.single, }; @@ -54,8 +58,10 @@ fn command_desc(arg: Args) string { .init => " init\t\tAdds the necessary PATH to the system env", .install => " install\tInstall the zig version", .list => " list\t\tLists all versions available on the system", + .ref => " ref\t\tOpens the zig lang reference", + .std => " std\t\tOpens the std docs for the current zig version", .use => " use\t\tSets the active version of zig to use", - .version => " version\t\tPrint out the zim version you are using", + .version => " version\tPrint out the zim version you are using", }; } @@ -106,7 +112,7 @@ fn show_context_help(arg: Args) void { \\ , .{}); }, - .gir, .help, .init, .version => { + .gir, .help, .init, .ref, .std, .version => { return; }, } @@ -124,25 +130,11 @@ fn do_arg_action(allocator: std.mem.Allocator, arg: Args, param: ?[]const u8) vo .init => { zim.init(allocator) catch |err| { std.debug.print("Error running `init`\n{any}\n", .{err}); + std.os.exit(1); }; - std.os.exit(1); + std.os.exit(0); return; }, - .version => { - const ver = versionStr(allocator) catch { - std.debug.print("Error getting version\n", .{}); - return; - }; - defer allocator.free(ver); - - if (param) |command| { - std.debug.print("{s}\nRunning ZiM Version {s}\n", .{ command, ver }); - } else { - std.debug.print("Running ZiM Version {s}\n", .{ver}); - } - - utils.printGir(); - }, .install => { if (param == null) { std.debug.print("Error: Expected index or version name to follow.\nRun `zim install help` for details.\n\n", .{}); @@ -181,6 +173,21 @@ fn do_arg_action(allocator: std.mem.Allocator, arg: Args, param: ?[]const u8) vo std.os.exit(1); }; }, + .ref => { + zim.open_ref(allocator) catch |err| { + std.debug.print("Error running `ref`\n{any}\n", .{err}); + std.os.exit(1); + }; + std.os.exit(0); + return; + }, + .std => { + zim.open_std(allocator) catch |err| { + std.debug.print("Error running `ref`\n{any}\n", .{err}); + std.os.exit(1); + }; + std.os.exit(0); + }, .use => { if (param == null) { std.debug.print("Error: Expected version or index to follow.\nRun `zim use help` for details.\n\n", .{}); @@ -197,6 +204,21 @@ fn do_arg_action(allocator: std.mem.Allocator, arg: Args, param: ?[]const u8) vo std.os.exit(1); }; }, + .version => { + const ver = versionStr(allocator) catch { + std.debug.print("Error getting version\n", .{}); + return; + }; + defer allocator.free(ver); + + if (param) |command| { + std.debug.print("{s}\nRunning ZiM Version {s}\n", .{ command, ver }); + } else { + std.debug.print("Running ZiM Version {s}\n", .{ver}); + } + + utils.printGir(); + }, } } diff --git a/src/zim.zig b/src/zim.zig index 6b9cebe..bca7c72 100644 --- a/src/zim.zig +++ b/src/zim.zig @@ -115,7 +115,7 @@ fn getLocalVersionsList( fn getRemoteVersionsSlice(allocator: std.mem.Allocator) ZimError![]const ZigVersion { // Check local file first for chached network call first const zim_dir = try openZimDir(allocator); - const now = @as(i128, @intCast(std.time.timestamp())) * 1000 * 1000; + const now = @as(i128, @intCast(std.time.timestamp())) * 1000 * 1000 * 1000; const yesterday = now - (24 * 60 * 60 * 1000 * 1000); var remote_versions_file = zim_dir.createFile("versions/remote_versions.json", .{ .read = true, .truncate = false }) catch { return ZimError.BadIO; @@ -129,14 +129,16 @@ fn getRemoteVersionsSlice(allocator: std.mem.Allocator) ZimError![]const ZigVers return ZimError.BadIO; }; + // std.debug.print("Got times modified: {any}, yesterday: {any}\n", .{file_metadata.modified(), yesterday}); if (file_metadata.modified() > yesterday) { + // std.debug.print("Getting cached result\n", .{}); body = remote_versions_file.readToEndAlloc(allocator, SIXTY_FOUR_KILOBYTES) catch |err| { std.debug.print("Could not read remote versions file: {any}\n", .{err}); return ZimError.Unexpected; }; } if (body.len == 0) { - std.debug.print("No cached versions, getting remote versions\n", .{}); + // std.debug.print("No cached versions, getting remote versions\n", .{}); // Otherwise, make network call and parse const versions_json_url = "https://ziglang.org/download/index.json"; @@ -506,7 +508,7 @@ pub fn list(allocator: std.mem.Allocator, param: string) !void { std.debug.print(" [{d}]\tzig-{s}\n", .{ index, remote_version.version_string }); } } - std.debug.print("\nZig versions can be installed using `zim install `\n\n", .{}); + std.debug.print("\nZig versions can be installed using `zim install `\n\n", .{}); return; }, } @@ -517,3 +519,54 @@ pub fn init(allocator: std.mem.Allocator) !void { _ = allocator; return ZimError.NotImplemented; } + +/// Runs the `ref` subcommand +pub fn open_ref(allocator: std.mem.Allocator) !void { + const ref_path = "~/.config/zim/bin/zig/doc/langref.html"; + + var zig_ver_res = std.ChildProcess.exec(.{.allocator = allocator, .argv = &.{ "zig", "version" }, .max_output_bytes = 100},) catch |err| { + std.debug.print("Error executing `zig version`: {any}\n", .{err}); + return ZimError.Unexpected; + }; + defer allocator.free(zig_ver_res.stdout); + defer allocator.free(zig_ver_res.stderr); + + if (zig_ver_res.term != .Exited) { + std.debug.print("Expected `zig version` to exit, instead got {any}\n", .{zig_ver_res.term}); + return ZimError.Unexpected; + } + const zig_ver = zig_ver_res.stdout[0..zig_ver_res.stdout.len - 1]; + std.debug.print("Opening language reference for zig-{s}...\n", .{zig_ver}); + + var proc = std.ChildProcess.init(&.{ "firefox", ref_path }, allocator); + _ = proc.spawn() catch |err| { + std.debug.print("Error while opening browser with local language reference\n{any}\n", .{err}); + return ZimError.Unexpected; + }; + return; +} + +pub fn open_std(allocator: std.mem.Allocator) !void { + const std_path = "~/.config/zim/bin/zig/doc/std/index.html"; + + var zig_ver_res = std.ChildProcess.exec(.{.allocator = allocator, .argv = &.{ "zig", "version" }, .max_output_bytes = 100},) catch |err| { + std.debug.print("Error executing `zig version`: {any}\n", .{err}); + return ZimError.Unexpected; + }; + defer allocator.free(zig_ver_res.stdout); + defer allocator.free(zig_ver_res.stderr); + + if (zig_ver_res.term != .Exited) { + std.debug.print("Expected `zig version` to exit, instead got {any}\n", .{zig_ver_res.term}); + return ZimError.Unexpected; + } + const zig_ver = zig_ver_res.stdout[0..zig_ver_res.stdout.len - 1]; + std.debug.print("Opening zig-{s} std lib docs...\n", .{zig_ver}); + + var proc = std.ChildProcess.init(&.{ "firefox", std_path }, allocator); + _ = proc.spawn() catch |err| { + std.debug.print("Error while opening browser with local language reference\n{any}\n", .{err}); + return ZimError.Unexpected; + }; + return; +}