libghostty: export benchmark CLI API

This commit is contained in:
Mitchell Hashimoto
2025-07-09 11:33:00 -07:00
parent 01b2545d1d
commit 20bb71c627
9 changed files with 114 additions and 31 deletions

View File

@@ -4,7 +4,7 @@ const cli = @import("../cli.zig");
/// The available actions for the CLI. This is the list of available
/// benchmarks.
const Action = enum {
pub const Action = enum {
@"terminal-stream",
/// Returns the struct associated with the action. The struct
@@ -24,31 +24,57 @@ const Action = enum {
/// An entrypoint for the benchmark CLI.
pub fn main() !void {
// TODO: Better terminal output throughout this, use libvaxis.
const alloc = std.heap.c_allocator;
const action_ = try cli.action.detectArgs(Action, alloc);
const action = action_ orelse return error.NoAction;
// We need a comptime action to get the struct type and do the
// rest.
return switch (action) {
inline else => |comptime_action| {
const BenchmarkImpl = Action.Struct(comptime_action);
try mainAction(BenchmarkImpl, alloc);
},
};
try mainAction(alloc, action, .cli);
}
fn mainAction(comptime BenchmarkImpl: type, alloc: Allocator) !void {
/// Arguments that can be passed to the benchmark.
pub const Args = union(enum) {
/// The arguments passed to the CLI via argc/argv.
cli,
/// Simple string arguments, parsed via std.process.ArgIteratorGeneral.
string: []const u8,
};
pub fn mainAction(
alloc: Allocator,
action: Action,
args: Args,
) !void {
switch (action) {
inline else => |comptime_action| {
const BenchmarkImpl = Action.Struct(comptime_action);
try mainActionImpl(BenchmarkImpl, alloc, args);
},
}
}
fn mainActionImpl(
comptime BenchmarkImpl: type,
alloc: Allocator,
args: Args,
) !void {
// First, parse our CLI options.
const Options = BenchmarkImpl.Options;
var opts: Options = .{};
defer if (@hasDecl(Options, "deinit")) opts.deinit();
{
var iter = try cli.args.argsIterator(alloc);
defer iter.deinit();
try cli.args.parse(Options, alloc, &opts, &iter);
switch (args) {
.cli => {
var iter = try cli.args.argsIterator(alloc);
defer iter.deinit();
try cli.args.parse(Options, alloc, &opts, &iter);
},
.string => |str| {
var iter = try std.process.ArgIteratorGeneral(.{}).init(
alloc,
str,
);
defer iter.deinit();
try cli.args.parse(Options, alloc, &opts, &iter);
},
}
// Create our implementation