Added help/-c options and added patch for zig 0.13.0 build. Also switched to zig 0.14.x and added user input.

This commit is contained in:
Medvidek77 2025-04-20 16:54:02 +02:00
parent 782e43bf11
commit c69bde76bc
4 changed files with 117 additions and 62 deletions

View file

@ -1,4 +1,5 @@
const std = @import("std");
const eql = std.mem.eql;
pub fn main() !void {
@ -6,10 +7,31 @@ pub fn main() !void {
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const stdin = std.io.getStdIn().reader();
const stdout = std.io.getStdOut().writer();
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
var path: []const u8 = undefined;
if (args.len > 2) {
if (eql(u8, args[1], "-c")) {
path = args[2];
}
}
else if (args.len == 2) {
if (eql(u8, args[1], "-h") or eql(u8, args[1], "help")) {
try stdout.print("(opt) -c /path/to/json_file\n", .{});
try stdout.print("(opt) help / -h", .{});
std.posix.exit(1);
}
}
const env = try std.process.getEnvVarOwned(allocator, "USER");
defer allocator.free(env);
const path = try std.fmt.allocPrint(allocator, "/home/{s}/.config/lister/data.json", .{env});
path = try std.fmt.allocPrint(allocator, "/home/{s}/.config/lister/data.json", .{env});
defer allocator.free(path);
const file = try std.fs.cwd().openFile(path, .{});
@ -27,8 +49,19 @@ pub fn main() !void {
const json = try std.json.parseFromSlice(Smths, allocator, data, .{});
defer json.deinit();
var buff: [256]u8 = undefined;
try stdout.print("Enter name: ", .{});
const inp_wthd = try stdin.readUntilDelimiter(buff[0..], '\n');
var found = false;
for (json.value) |smth| {
std.debug.print("{s} -> {s}\n", .{smth.name, smth.desc});
if (eql(u8, smth.name, inp_wthd)) {
try stdout.print("{s} -> {s}\n", .{smth.name, smth.desc});
found = true;
}
}
if (!found) {
try stdout.print("Bad value :(", .{});
}
}