summaryrefslogtreecommitdiff
path: root/src/host/root.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/host/root.zig')
-rw-r--r--src/host/root.zig164
1 files changed, 79 insertions, 85 deletions
diff --git a/src/host/root.zig b/src/host/root.zig
index 9c7ad77..42362d3 100644
--- a/src/host/root.zig
+++ b/src/host/root.zig
@@ -1,21 +1,6 @@
//! The sketchbook host: runs a sketch linked in, or loaded from a library
-//! that it hot-reloads whenever a build replaces it.
-//!
-//! sketchbook [options] [LIBRARY]
-//! --state FILE the save (default: $XDG_DATA_HOME/<name>/<name>.xitdb;
-//! sketch.xitdb for a script or an export)
-//! --headless offscreen video, silent audio
-//! --script FILE play a script instead of reading devices
-//! --fresh start from default state (the save's history is kept)
-//! --timing-marker encode frame ID and draw sample time for video analysis
-//! --profile-gpu time each GPU command alone, print the times at exit
-//! --console LINE run a console line once the sketch is loaded (repeatable)
-//! --define NAME=VALUE substitute {NAME} in the script
-//! --export DIR|- headless, from fresh state: every frame as DIR/000000.png, ...
-//! or for - as raw RGBA on stdout, until the sketch quits
-//! --fps N export: frames a second (60); each tick is exactly 1/N s
-//! --frames N export: stop after N frames
-//! --size WxH the first canvas and, headless, the room for it
+//! that it hot-reloads whenever a build replaces it. Its options are
+//! `flags`, which parse the command line and print `--help`.
const std = @import("std");
const c = @import("c");
const abi = @import("abi");
@@ -39,58 +24,52 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void {
var args = init.minimal.args.iterate();
_ = args.skip();
while (args.next()) |arg| {
- if (std.mem.eql(u8, arg, "--state")) {
- options.state = args.next() orelse fatal("--state needs a file");
- } else if (std.mem.eql(u8, arg, "--9p")) {
- options.ninep = args.next() orelse fatal("--9p needs a socket path");
- } else if (std.mem.eql(u8, arg, "--fresh")) {
- options.fresh = true;
- } else if (std.mem.eql(u8, arg, "--headless")) {
- options.headless = true;
- } else if (std.mem.eql(u8, arg, "--timing-marker")) {
- options.timing_marker = true;
- } else if (std.mem.eql(u8, arg, "--profile-gpu")) {
- options.profile_gpu = true;
- } else if (std.mem.eql(u8, arg, "--console")) {
- try lines.append(gpa, args.next() orelse fatal("--console needs a line"));
- } else if (std.mem.eql(u8, arg, "--script")) {
- script_path = args.next() orelse fatal("--script needs a file");
- options.script = true;
- } else if (std.mem.eql(u8, arg, "--define")) {
- const pair = args.next() orelse fatal("--define needs NAME=VALUE");
- const eq = std.mem.indexOfScalar(u8, pair, '=') orelse fatal("--define needs NAME=VALUE");
- try defines.append(gpa, .{ pair[0..eq], pair[eq + 1 ..] });
- } else if (std.mem.eql(u8, arg, "--export")) {
- options.export_to = args.next() orelse fatal("--export needs a directory, or - for stdout");
- options.headless = true;
- options.fresh = true;
- } else if (std.mem.eql(u8, arg, "--fps")) {
- options.fps = std.fmt.parseInt(u32, args.next() orelse "", 10) catch 0;
- if (options.fps == 0) fatal("--fps needs a whole number of frames a second");
- } else if (std.mem.eql(u8, arg, "--frames")) {
- options.frames = std.fmt.parseInt(u64, args.next() orelse "", 10) catch fatal("--frames needs a number");
- } else if (std.mem.eql(u8, arg, "--size")) {
- const size = args.next() orelse "";
- const x = std.mem.indexOfScalar(u8, size, 'x') orelse fatal("--size needs WxH");
- const w = std.fmt.parseInt(u32, size[0..x], 10) catch 0;
- const h = std.fmt.parseInt(u32, size[x + 1 ..], 10) catch 0;
- if (w == 0 or h == 0) fatal("--size needs WxH");
- options.size = .{ w, h };
- } else if (std.mem.eql(u8, arg, "-h") or std.mem.eql(u8, arg, "--help")) {
- std.debug.print("{s}", .{usage});
- return;
- } else if (std.mem.startsWith(u8, arg, "-")) {
- fatal("unknown option (see --help)");
- } else {
+ if (!std.mem.startsWith(u8, arg, "-")) {
options.library = arg;
+ continue;
}
+ const flag: Flag = if (std.mem.eql(u8, arg, "-h")) .help else (if (std.mem.startsWith(u8, arg, "--")) std.meta.stringToEnum(Flag, arg[2..]) else null) orelse fatal("{s} is no option (see --help)", .{arg});
+ const v = if (flags.get(flag).arg.len == 0) "" else args.next() orelse fatal("{s} needs {s}", .{ arg, flags.get(flag).arg });
+ switch (flag) {
+ .@"9p" => options.ninep = v,
+ .state => options.state = v,
+ .headless => options.headless = true,
+ .script => {
+ script_path = v;
+ options.script = true;
+ },
+ .fresh => options.fresh = true,
+ .@"timing-marker" => options.timing_marker = true,
+ .@"profile-gpu" => options.profile_gpu = true,
+ .console => try lines.append(gpa, v),
+ .define => {
+ const eq = std.mem.indexOfScalar(u8, v, '=') orelse fatal("{s} needs {s}", .{ arg, "NAME=VALUE" });
+ try defines.append(gpa, .{ v[0..eq], v[eq + 1 ..] });
+ },
+ .@"export" => {
+ options.export_to = v;
+ options.headless = true;
+ options.fresh = true;
+ },
+ .fps => options.fps = std.fmt.parseInt(u32, v, 10) catch 0,
+ .frames => options.frames = std.fmt.parseInt(u64, v, 10) catch fatal("{s} needs {s}", .{ arg, "a number" }),
+ .size => {
+ const x = std.mem.indexOfScalar(u8, v, 'x') orelse 0;
+ const w = std.fmt.parseInt(u32, v[0..x], 10) catch 0;
+ const h = std.fmt.parseInt(u32, v[x + 1 ..], 10) catch 0;
+ if (w == 0 or h == 0) fatal("{s} needs {s}", .{ arg, "WxH" });
+ options.size = .{ w, h };
+ },
+ .help => return std.debug.print("{s}", .{usage}),
+ }
+ if (options.fps == 0) fatal("{s} needs {s}", .{ arg, "a whole number of frames a second" });
}
- if (linked == null and options.library == null) fatal("no sketch library given (see --help)");
+ if (linked == null and options.library == null) fatal("no sketch library given (see --help)", .{});
if (options.export_to) |to| {
- if (script_path != null) fatal("--export plays no script: it runs the sketch on its own");
+ if (script_path != null) fatal("--export plays no script: it runs the sketch on its own", .{});
if (!std.mem.eql(u8, to, "-")) {
try std.Io.Dir.cwd().createDirPath(init.io, to);
- } else if (try std.Io.File.stdout().isTty(init.io)) fatal("--export -: stdout is a terminal; pipe it to ffmpeg");
+ } else if (try std.Io.File.stdout().isTty(init.io)) fatal("--export -: stdout is a terminal; pipe it to ffmpeg", .{});
}
if (options.state.len == 0 and (script_path != null or options.export_to != null)) options.state = "sketch.xitdb";
@@ -112,26 +91,41 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void {
if (sdl_heap.leaked()) return error.LeakedSdlMemory;
}
-const usage =
- \\usage: sketchbook [options] [LIBRARY]
- \\ --9p SOCKET local 9P controls: ctl, status, log, screen.png
- \\ --state FILE the save, one file: FILE, or FILE.xitdb for a FILE.state (where a
- \\ state file from before is imported from, once); default:
- \\ $XDG_DATA_HOME/<name>/<name>.xitdb, sketch.xitdb for --script, --export
- \\ --headless offscreen video, silent audio, no vsync
- \\ --script FILE play a script instead of reading devices
- \\ --fresh start from default state (the save's history is kept)
- \\ --timing-marker encode frame ID and draw sample time for video analysis
- \\ --profile-gpu time each GPU command alone, print the times at exit
- \\ --console LINE run a console line once the sketch is loaded (repeatable)
- \\ --define NAME=VALUE substitute {NAME} in the script
- \\ --export DIR|- headless, from fresh state: every frame as DIR/000000.png, ...
- \\ or for - as raw RGBA on stdout, until the sketch quits
- \\ --fps N export: frames a second (60); each tick is exactly 1/N s
- \\ --frames N export: stop after N frames
- \\ --size WxH the first canvas and, headless, the room for it
- \\
-;
+/// The options: a row each, what parses the command line (the switch in
+/// `main` sets what each one means) and prints `--help`.
+const Flag = enum { @"9p", state, headless, script, fresh, @"timing-marker", @"profile-gpu", console, define, @"export", fps, frames, size, help };
+const flags = std.enums.EnumArray(Flag, struct { arg: []const u8 = "", doc: []const u8 }).init(.{
+ .@"9p" = .{ .arg = "SOCKET", .doc = "local 9P controls: ctl, status, log, screen.png" },
+ .state = .{ .arg = "FILE", .doc = "the save, one file: FILE, or FILE.xitdb for a FILE.state (where a\nstate file from before is imported from, once); default:\n$XDG_DATA_HOME/<name>/<name>.xitdb, sketch.xitdb for --script, --export" },
+ .headless = .{ .doc = "offscreen video, silent audio, no vsync" },
+ .script = .{ .arg = "FILE", .doc = "play a script instead of reading devices" },
+ .fresh = .{ .doc = "start from default state (the save's history is kept)" },
+ .@"timing-marker" = .{ .doc = "encode frame ID and draw sample time for video analysis" },
+ .@"profile-gpu" = .{ .doc = "time each GPU command alone, print the times at exit" },
+ .console = .{ .arg = "LINE", .doc = "run a console line once the sketch is loaded (repeatable)" },
+ .define = .{ .arg = "NAME=VALUE", .doc = "substitute {NAME} in the script" },
+ .@"export" = .{ .arg = "DIR|-", .doc = "headless, from fresh state: every frame as DIR/000000.png, ...\nor for - as raw RGBA on stdout, until the sketch quits" },
+ .fps = .{ .arg = "N", .doc = "export: frames a second (60); each tick is exactly 1/N s" },
+ .frames = .{ .arg = "N", .doc = "export: stop after N frames" },
+ .size = .{ .arg = "WxH", .doc = "the first canvas and, headless, the room for it" },
+ .help = .{ .doc = "this, also -h" },
+});
+
+/// `--help`: a line per flag, its doc's lines under one another.
+const usage = blk: {
+ @setEvalBranchQuota(100_000);
+ const pad: [23]u8 = @splat(' ');
+ var text: []const u8 = "usage: sketchbook [options] [LIBRARY]\n";
+ for (std.enums.values(Flag)) |flag| {
+ const f = flags.get(flag);
+ const head = " --" ++ @tagName(flag) ++ (if (f.arg.len > 0) " " ++ f.arg else "");
+ text = text ++ head ++ pad[head.len..];
+ var lines = std.mem.splitScalar(u8, f.doc, '\n');
+ text = text ++ lines.first() ++ "\n";
+ while (lines.next()) |more| text = text ++ pad ++ more ++ "\n";
+ }
+ break :blk text;
+};
/// A save in a directory named `.sketchbook` (`zig build run`'s): the
/// directory is made, with a `.gitignore` of `*` (the save grows, and a jj
@@ -159,8 +153,8 @@ fn sketchbookDir(io: std.Io, state: []const u8) void {
}
}
-fn fatal(message: []const u8) noreturn {
- std.debug.print("sketchbook: {s}\n", .{message});
+fn fatal(comptime fmt: []const u8, args: anytype) noreturn {
+ std.debug.print("sketchbook: " ++ fmt ++ "\n", args);
std.process.exit(2);
}