create common zig lib for scripts

This commit is contained in:
LordMZTE 2024-03-20 20:45:59 +01:00
parent 105fb94ef7
commit 64a3b6c74a
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
48 changed files with 196 additions and 69 deletions

1
lib/common-zig/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/zig-*

1
lib/common-zig/README.md Normal file
View file

@ -0,0 +1 @@
Common library for all my Zig scripts.

View file

@ -1,15 +1,19 @@
//! Shared code for script build scripts
const std = @import("std");
pub const confgen_json_opt = std.json.ParseOptions{ .ignore_unknown_fields = true };
pub fn build(b: *std.Build) void {
_ = b.addModule("common", .{
.root_source_file = .{ .path = "src/main.zig" },
});
}
// TODO: make confgen generate zon and delete
/// Retrieve some confgen options given a relative path to the dotfile root and a struct type
/// with a field for each option.
pub fn confgenGet(comptime T: type, root_path: []const u8, alloc: std.mem.Allocator) !T {
const optsjson = try std.fs.path.join(alloc, &.{ root_path, "cgout", "opts.json" });
defer alloc.free(optsjson);
var file = try std.fs.cwd().openFile(optsjson, .{});
pub fn confgenGet(comptime T: type, alloc: std.mem.Allocator) !T {
const optjson_path = comptime std.fs.path.dirname(@src().file).? ++ "/../../cgout/opts.json";
var file = try std.fs.cwd().openFile(optjson_path, .{});
defer file.close();
var buf_reader = std.io.bufferedReader(file.reader());

View file

@ -0,0 +1,53 @@
const std = @import("std");
var stderr_isatty: ?bool = null;
pub var log_file: ?std.fs.File = null;
pub fn logFn(
comptime level: std.log.Level,
comptime scope: @TypeOf(.enum_literal),
comptime fmt: []const u8,
args: anytype,
) void {
const log_pfx: ?[]const u8 = if (@hasDecl(@import("root"), "mztecommon_log_pfx"))
@import("root").mztecommon_log_pfx
else
null;
const color = log_file == null and stderr_isatty orelse blk: {
const isatty = std.os.isatty(std.os.STDERR_FILENO);
stderr_isatty = isatty;
break :blk isatty;
};
const logfile = log_file orelse std.io.getStdErr();
const scope_prefix = if (log_pfx) |lpfx|
if (scope != .default)
"[" ++ lpfx ++ " " ++ @tagName(scope) ++ "] "
else
"[" ++ lpfx ++ "] "
else if (scope != .default)
"[" ++ @tagName(scope) ++ "] "
else
"";
switch (color) {
inline else => |col| {
const lvl_prefix = comptime if (col) switch (level) {
.debug => "\x1b[1;34mD:\x1b[0m ",
.info => "\x1b[1;32mI:\x1b[0m ",
.warn => "\x1b[1;33mW:\x1b[0m ",
.err => "\x1b[1;31mE:\x1b[0m ",
} else switch (level) {
.debug => "D: ",
.info => "I: ",
.warn => "W: ",
.err => "E: ",
};
logfile.writer().print(scope_prefix ++ lvl_prefix ++ fmt ++ "\n", args) catch {};
},
}
}

View file

@ -1,5 +1,5 @@
const std = @import("std");
const common = @import("build_common.zig");
const common = @import("common");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
@ -21,7 +21,7 @@ pub fn build(b: *std.Build) !void {
const cg_opt = try common.confgenGet(struct {
term_font: []u8, // TODO: this being non-const is a workaround for an std bug
}, "..", b.allocator);
}, b.allocator);
const opts = b.addOptions();
opts.addOption([]const u8, "font", cg_opt.term_font);

View file

@ -3,6 +3,7 @@
.version = "0.0.0",
.dependencies = .{
.common = .{ .path = "../lib/common-zig" },
.znvim = .{
.url = "git+https://git.mzte.de/LordMZTE/znvim.git#8e52c461dc071e6b88c8e77e49aa2805f225e7da",
.hash = "122029929d792aa32a71c0b98bb67d344d971d269d6fea5b8f8693e29f0f45924951",

View file

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

View file

@ -7,11 +7,11 @@ pub fn build(b: *std.Build) void {
const lib = b.addSharedLibrary(.{
.name = "mzte-mpv",
.root_source_file = .{ .path = "src/main.zig" },
.link_libc = true,
.target = target,
.optimize = optimize,
});
lib.linkLibC();
lib.root_module.addImport("common", b.dependency("common", .{}).module("common"));
const install_step = b.addInstallArtifact(lib, .{
// this is not a standard MPV installation path, but instead one that makes sense.

View file

@ -0,0 +1,8 @@
.{
.name = "mzte-mpv",
.version = "0.0.0",
.paths = .{""},
.dependencies = .{
.common = .{ .path = "../../lib/common-zig" },
}
}

View file

@ -6,22 +6,11 @@ const util = @import("util.zig");
pub const std_options = std.Options{
.log_level = .debug,
.logFn = struct {
fn logFn(
comptime message_level: std.log.Level,
comptime scope: @TypeOf(.enum_literal),
comptime format: []const u8,
args: anytype,
) void {
_ = scope;
const stderr = std.io.getStdErr().writer();
stderr.print("[mzte-mpv {s}] " ++ format ++ "\n", .{@tagName(message_level)} ++ args) catch return;
}
}.logFn,
.logFn = @import("common").logFn,
};
pub const mztecommon_log_pfx = "mzte-mpv";
export fn mpv_open_cplugin(handle: *c.mpv_handle) callconv(.C) c_int {
tryMain(handle) catch |e| {
if (@errorReturnTrace()) |ert|

View file

@ -35,7 +35,7 @@ pub fn onEvent(self: *LiveChat, mpv: *c.mpv_handle, ev: *c.mpv_event) !void {
errdefer file.close();
std.log.info("initializing subtitle transcoder: {s}", .{fname});
const pipe = try std.os.pipe2(0);
const pipe = try std.os.pipe2(.{});
// This needs to be done here instead of the separate thread. MPV will instantly
// give up if there's nothing to be read from the pipe when the command is called.

View file

@ -11,6 +11,8 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
exe.root_module.addImport("common", b.dependency("common", .{}).module("common"));
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);

View file

@ -0,0 +1,8 @@
.{
.name = "alecor",
.version = "0.0.0",
.paths = .{""},
.dependencies = .{
.common = .{ .path = "../../lib/common-zig" },
},
}

View file

@ -5,6 +5,7 @@ const util = @import("util.zig");
pub const std_options = std.Options{
.log_level = .debug,
.logFn = @import("common").logFn,
};
pub fn main() !void {

View file

@ -11,6 +11,8 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
exe.root_module.addImport("common", b.dependency("common", .{}).module("common"));
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);

View file

@ -0,0 +1,8 @@
.{
.name = "hyprtool",
.version = "0.0.0",
.paths = .{""},
.dependencies = .{
.common = .{ .path = "../../lib/common-zig" },
},
}

View file

@ -2,6 +2,7 @@ const std = @import("std");
pub const std_options = std.Options{
.log_level = .debug,
.logFn = @import("common").logFn,
};
pub fn main() !void {

View file

@ -1,11 +1,12 @@
const std = @import("std");
const common = @import("build_common.zig");
const common = @import("common");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const ansi_term_mod = b.dependency("ansi_term", .{}).module("ansi-term");
const common_mod = b.dependency("common", .{}).module("common");
const exe = b.addExecutable(.{
.name = "mzteinit",
@ -23,11 +24,12 @@ pub fn build(b: *std.Build) !void {
inline for (.{ mzteinitctl, exe }) |e| {
e.root_module.addImport("ansi-term", ansi_term_mod);
e.root_module.addImport("common", common_mod);
}
const cg_opt = try common.confgenGet(struct {
gtk_theme: []u8, // TODO: this being non-const is a workaround for an std bug
}, "../..", b.allocator);
}, b.allocator);
const opts = b.addOptions();
opts.addOption([]const u8, "gtk_theme", cg_opt.gtk_theme);

View file

@ -3,6 +3,7 @@
.version = "0.0.0",
.dependencies = .{
.common = .{ .path = "../../lib/common-zig" },
.ansi_term = .{
.url = "git+https://github.com/LordMZTE/ansi-term.git#73c03175068679685535111dbea72cade075719e",
.hash = "1220ea86ace34b38e49c1d737c5f857d88346af10695a992b38e10cb0a73b6a19ef7",

View file

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

View file

@ -1,5 +1,7 @@
const std = @import("std");
const at = @import("ansi-term");
const common = @import("common");
const env = @import("env.zig");
const command = @import("command.zig");
const util = @import("util.zig");
@ -11,36 +13,12 @@ const msg = @import("message.zig").msg;
pub const std_options = std.Options{
.log_level = .debug,
.logFn = struct {
pub fn logFn(
comptime msg_level: std.log.Level,
comptime scope: @TypeOf(.enum_literal),
comptime fmt: []const u8,
args: anytype,
) void {
const logfile = log_file orelse return;
if (scope != .default) {
logfile.writer().print("[{s}] ", .{@tagName(scope)}) catch return;
}
logfile.writer().writeAll(switch (msg_level) {
.err => "E: ",
.warn => "W: ",
.info => "I: ",
.debug => "D: ",
}) catch return;
logfile.writer().print(fmt ++ "\n", args) catch return;
}
}.logFn,
.logFn = common.logFn,
};
var log_file: ?std.fs.File = null;
pub fn main() void {
log_file = createLogFile() catch null;
defer if (log_file) |lf| lf.close();
common.log_file = createLogFile() catch null;
defer if (common.log_file) |lf| lf.close();
tryMain() catch |e| {
std.log.err("FATAL ERROR: {}", .{e});

View file

@ -4,6 +4,7 @@ const Client = @import("sock/Client.zig");
pub const std_options = std.Options{
.log_level = .debug,
.logFn = @import("common").logFn,
};
pub fn main() !void {

View file

@ -1,5 +1,5 @@
const std = @import("std");
const common = @import("build_common.zig");
const common = @import("common");
const Scanner = @import("wayland").Scanner;
@ -22,7 +22,7 @@ pub fn build(b: *std.Build) !void {
theme: [:0]const u8,
size: u32,
},
}, "../..", b.allocator);
}, b.allocator);
const opts = b.addOptions();
opts.addOption([:0]const u8, "catppuccin_red", cg_opt.catppuccin.red);
@ -43,6 +43,7 @@ pub fn build(b: *std.Build) !void {
.optimize = optimize,
});
exe.root_module.addImport("common", b.dependency("common", .{}).module("common"));
exe.root_module.addImport("opts", opts.createModule());
exe.root_module.addImport("wayland", scanner.mod);

View file

@ -3,6 +3,7 @@
.version = "0.0.0",
.dependencies = .{
.common = .{ .path = "../../lib/common-zig" },
.wayland = .{
.url = "git+https://git.mzte.de/LordMZTE/zig-wayland#4de9f2d6d5fddae1fbb29e21fd1dae69e646ab7d",
.hash = "1220d6448c277e5c41348aa95ce2ba2fc92a92cb7a9e9783edf0f816cd0260122d31",

View file

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

View file

@ -1,7 +1,7 @@
const std = @import("std");
const opts = @import("opts");
const log = std.log.scoped(.mzteriver);
const log = std.log.scoped(.init);
const Connection = @import("Connection.zig");
@ -198,7 +198,7 @@ pub fn init(alloc: std.mem.Allocator, initial: bool) !void {
defer alloc.free(cgfs_eval_path);
const evalf = std.fs.cwd().openFile(cgfs_eval_path, .{ .mode = .write_only }) catch {
std.log.warn("unable to open confgenfs eval file", .{});
log.warn("unable to open confgenfs eval file", .{});
break :confgenfs;
};
defer evalf.close();
@ -209,7 +209,7 @@ pub fn init(alloc: std.mem.Allocator, initial: bool) !void {
}
if (initial) {
std.log.info("spawning processes", .{});
log.info("spawning processes", .{});
var child_arena = std.heap.ArenaAllocator.init(alloc);
defer child_arena.deinit();

View file

@ -1,8 +1,6 @@
const std = @import("std");
const opts = @import("opts");
const log = std.log.scoped(.mzteriver);
const init = @import("init.zig").init;
pub const std_options = std.Options{
@ -10,8 +8,11 @@ pub const std_options = std.Options{
.Debug => .debug,
else => .info,
},
.logFn = @import("common").logFn,
};
pub const mztecommon_log_pfx = "mzteriver";
pub fn main() !void {
var dbg_gpa = if (@import("builtin").mode == .Debug) std.heap.GeneralPurposeAllocator(.{}){} else {};
defer if (@TypeOf(dbg_gpa) != void) {
@ -22,15 +23,15 @@ pub fn main() !void {
if (std.mem.endsWith(u8, std.mem.span(std.os.argv[0]), "init") or
(std.os.argv.len >= 2 and std.mem.orderZ(u8, std.os.argv[1], "init") == .eq))
{
log.info("running in init mode", .{});
std.log.info("running in init mode", .{});
try init(alloc, true);
} else if (std.mem.endsWith(u8, std.mem.span(std.os.argv[0]), "reinit") or
(std.os.argv.len >= 2 and std.mem.orderZ(u8, std.os.argv[1], "reinit") == .eq))
{
log.info("running in reinit mode", .{});
std.log.info("running in reinit mode", .{});
try init(alloc, false);
} else {
log.info("running in launch mode", .{});
std.log.info("running in launch mode", .{});
const logfd = logf: {
const logf_path = try std.fmt.allocPrintZ(
@ -40,7 +41,7 @@ pub fn main() !void {
);
defer alloc.free(logf_path);
log.info("river log file: {s}", .{logf_path});
std.log.info("river log file: {s}", .{logf_path});
break :logf try std.os.openatZ(
std.os.AT.FDCWD,

View file

@ -10,6 +10,9 @@ pub fn build(b: *std.Build) void {
.target = target,
.optimize = mode,
});
exe.root_module.addImport("common", b.dependency("common", .{}).module("common"));
b.installArtifact(exe);
const desktop_install_step = b.addInstallFile(

View file

@ -0,0 +1,8 @@
.{
.name = "openbrowser",
.version = "0.0.0",
.paths = .{""},
.dependencies = .{
.common = .{ .path = "../../lib/common-zig" },
},
}

View file

@ -3,6 +3,7 @@ const info = @import("info.zig");
pub const std_options = std.Options{
.log_level = .debug,
.logFn = @import("common").logFn,
};
const browsers = &[_][]const u8{

View file

@ -11,6 +11,8 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
exe.root_module.addImport("common", b.dependency("common", .{}).module("common"));
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);

View file

@ -0,0 +1,8 @@
.{
.name = "playvid",
.version = "0.0.0",
.paths = .{""},
.dependencies = .{
.common = .{ .path = "../../lib/common-zig" },
},
}

View file

@ -1,5 +1,10 @@
const std = @import("std");
pub const std_options = std.Options{
.log_level = .debug,
.logFn = @import("common").logFn,
};
pub fn main() !u8 {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();

View file

@ -14,6 +14,7 @@ pub fn build(b: *std.Build) void {
exe.linkLibC();
exe.linkSystemLibrary("libgit2");
exe.root_module.addImport("common", b.dependency("common", .{}).module("common"));
exe.root_module.addImport("ansi-term", b.dependency("ansi_term", .{}).module("ansi-term"));
exe.root_module.addImport("known-folders", b.dependency("known_folders", .{}).module("known-folders"));

View file

@ -1,8 +1,10 @@
.{
.name = "promt",
.version = "0.0.0",
.paths = .{""},
.dependencies = .{
.common = .{ .path = "../../lib/common-zig" },
.ansi_term = .{
.url = "git+https://github.com/LordMZTE/ansi-term.git#73c03175068679685535111dbea72cade075719e",
.hash = "1220ea86ace34b38e49c1d737c5f857d88346af10695a992b38e10cb0a73b6a19ef7",
@ -12,5 +14,4 @@
.hash = "12209925016f4b5486a713828ead3bcc900fa4f039c93de1894aa7d5253f7633b92c",
},
},
.paths = .{""},
}

View file

@ -13,6 +13,11 @@ const fish_code =
\\end
;
pub const std_options = std.Options{
.log_level = .debug,
.logFn = @import("common").logFn,
};
pub fn main() !void {
if (std.os.argv.len < 2)
return error.NotEnoughArguments;

View file

@ -15,6 +15,8 @@ pub fn build(b: *std.Build) void {
exe.linkSystemLibrary("xcb");
exe.linkSystemLibrary("xcb-xinerama");
exe.root_module.addImport("common", b.dependency("common", .{}).module("common"));
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);

View file

@ -0,0 +1,8 @@
.{
.name = "randomwallpaper",
.version = "0.0.0",
.paths = .{""},
.dependencies = .{
.common = .{ .path = "../../lib/common-zig" },
},
}

View file

@ -4,6 +4,7 @@ const Walker = @import("Walker.zig");
pub const std_options = std.Options{
.log_level = .debug,
.logFn = @import("common").logFn,
};
pub fn main() !u8 {

View file

@ -16,6 +16,7 @@ pub fn build(b: *std.Build) void {
.optimize = mode,
});
exe.root_module.addImport("common", b.dependency("common", .{}).module("common"));
exe.root_module.addImport("wayland", wayland_mod);
scanner.addSystemProtocol("stable/xdg-shell/xdg-shell.xml");

View file

@ -3,6 +3,7 @@
.version = "0.0.0",
.paths = .{""},
.dependencies = .{
.common = .{ .path = "../../lib/common-zig" },
.wayland = .{
.url = "git+https://git.mzte.de/LordMZTE/zig-wayland#4de9f2d6d5fddae1fbb29e21fd1dae69e646ab7d",
.hash = "1220d6448c277e5c41348aa95ce2ba2fc92a92cb7a9e9783edf0f816cd0260122d31",

View file

@ -4,6 +4,7 @@ const ClipboardConnection = @import("ClipboardConnection.zig");
pub const std_options = std.Options{
.log_level = .debug,
.logFn = @import("common").logFn,
};
pub fn main() !void {

View file

@ -11,6 +11,8 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
exe.root_module.addImport("common", b.dependency("common", .{}).module("common"));
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);

View file

@ -0,0 +1,8 @@
.{
.name = "withjava",
.version = "0.0.0",
.paths = .{""},
.dependencies = .{
.common = .{ .path = "../../lib/common-zig" },
},
}

View file

@ -1,5 +1,10 @@
const std = @import("std");
pub const std_options = std.Options{
.log_level = .debug,
.logFn = @import("common").logFn,
};
pub fn main() !u8 {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();

View file

@ -16,6 +16,7 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
exe.root_module.addImport("common", b.dependency("common", .{}).module("common"));
exe.root_module.addImport("xev", b.dependency("xev", .{
.target = target,
.optimize = optimize,

View file

@ -3,6 +3,7 @@
.version = "0.0.0",
.paths = .{""},
.dependencies = .{
.common = .{ .path = "../../lib/common-zig" },
.wayland = .{
.url = "git+https://git.mzte.de/LordMZTE/zig-wayland#4de9f2d6d5fddae1fbb29e21fd1dae69e646ab7d",
.hash = "1220d6448c277e5c41348aa95ce2ba2fc92a92cb7a9e9783edf0f816cd0260122d31",

View file

@ -18,6 +18,7 @@ const zxdg = wayland.client.zxdg;
pub const std_options = std.Options{
.log_level = .debug,
.logFn = @import("common").logFn,
};
pub fn main() !void {