67 lines
1.8 KiB
Zig
67 lines
1.8 KiB
Zig
const std = @import("std");
|
|
const eql = std.mem.eql;
|
|
|
|
|
|
pub fn main() !void {
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
|
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);
|
|
|
|
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, .{});
|
|
defer file.close();
|
|
|
|
const data = try file.reader().readAllAlloc(allocator, 32768);
|
|
defer allocator.free(data);
|
|
|
|
const Smth = struct {
|
|
name: []const u8,
|
|
desc: []const u8,
|
|
};
|
|
|
|
const Smths = []Smth;
|
|
|
|
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| {
|
|
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 :(", .{});
|
|
}
|
|
}
|