zig scripts can now use options from confgen!

This commit is contained in:
LordMZTE 2023-06-01 15:58:51 +02:00
parent 0cc96946f5
commit e7bd7d0f08
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
4 changed files with 47 additions and 2 deletions

32
build_common.zig Normal file
View file

@ -0,0 +1,32 @@
//! Shared code for script build scripts
const std = @import("std");
pub const confgen_json_opt = std.json.ParseOptions{ .ignore_unknown_fields = true };
/// Retrieve some confgen options given a path to the cgfile and a struct type
/// with a field for each option.
pub fn confgenGet(comptime T: type, cgfile: []const u8, alloc: std.mem.Allocator) !T {
const info = @typeInfo(T);
const field_names = comptime blk: {
var names: [info.Struct.fields.len][]const u8 = undefined;
for (info.Struct.fields, 0..) |f, i|
names[i] = f.name;
break :blk names;
};
const out = try std.ChildProcess.exec(.{
.allocator = alloc,
.argv = &[_][]const u8{ "confgen", "--json-opt", cgfile } ++ field_names,
});
defer {
alloc.free(out.stdout);
alloc.free(out.stderr);
}
if (!std.meta.eql(out.term, .{ .Exited = 0 }))
return error.UnexpectedExitCode;
return try std.json.parseFromSlice(T, alloc, out.stdout, confgen_json_opt);
}

View file

@ -1,4 +1,5 @@
const std = @import("std");
const common = @import("build_common.zig");
pub fn build(b: *std.build.Builder) !void {
const target = b.standardTargetOptions(.{});
@ -18,6 +19,14 @@ pub fn build(b: *std.build.Builder) !void {
const znvim_dep = b.dependency("znvim", .{ .target = target, .optimize = mode });
const cg_opt = try common.confgenGet(struct {
term_font: []const u8,
}, "../confgen.lua", b.allocator);
const opts = b.addOptions();
opts.addOption([]const u8, "font", cg_opt.term_font);
lib.addOptions("opts", opts);
lib.addModule("nvim", znvim_dep.module("nvim_c"));
lib.addModule("znvim", znvim_dep.module("znvim"));

1
mzte-nv/build_common.zig Symbolic link
View file

@ -0,0 +1 @@
../build_common.zig

View file

@ -2,10 +2,14 @@ const std = @import("std");
const znvim = @import("znvim");
const nvim = @import("nvim");
const opts = @import("opts");
const log = std.log.scoped(.options);
/// Initializes neovim options.
pub fn initOptions() !void {
var buf: [512]u8 = undefined;
// Shell (defaults to mzteinit since that's my login shell)
try setOption("shell", "fish");
@ -27,7 +31,7 @@ pub fn initOptions() !void {
try setOption("colorcolumn", "100");
try setOption("cursorcolumn", true);
try setOption("cursorline", true);
try setOption("guifont", "Iosevka Nerd Font Mono:h10");
try setOption("guifont", try std.fmt.bufPrintZ(&buf, "{s}:h10", .{opts.font}));
try setOption("mouse", "a");
try setOption("number", true);
try setOption("relativenumber", true);
@ -55,7 +59,6 @@ pub fn initOptions() !void {
"perl",
"node",
}) |garbage| {
var buf: [64]u8 = undefined;
const opt = try std.fmt.bufPrintZ(&buf, "g:loaded_{s}_provider", .{garbage});
setVar(opt, .{ .bool = false });
}