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

@@ -45,7 +45,7 @@ pub fn run(
const signpost: if (builtin.target.os.tag.isDarwin()) struct {
log: *macos.os.Log,
id: macos.os.signpost.Id,
} else void = if (comptime builtin.os.tag == .macos) macos: {
} else void = if (builtin.target.os.tag.isDarwin()) darwin: {
macos.os.signpost.init();
const log = macos.os.Log.create(
build_config.bundle_id,
@@ -53,9 +53,9 @@ pub fn run(
);
const id = macos.os.signpost.Id.forPointer(log, self.ptr);
macos.os.signpost.intervalBegin(log, id, signpost_name);
break :macos .{ .log = log, .id = id };
break :darwin .{ .log = log, .id = id };
} else {};
defer if (comptime builtin.os.tag == .macos) {
defer if (comptime builtin.target.os.tag.isDarwin()) {
macos.os.signpost.intervalEnd(
signpost.log,
signpost.id,

34
src/benchmark/CApi.zig Normal file
View File

@@ -0,0 +1,34 @@
const std = @import("std");
const cli = @import("cli.zig");
const state = &@import("../global.zig").state;
const log = std.log.scoped(.benchmark);
/// Run the Ghostty benchmark CLI with the given action and arguments.
export fn ghostty_benchmark_cli(
action_name_: [*:0]const u8,
args: [*:0]const u8,
) bool {
const action_name = std.mem.sliceTo(action_name_, 0);
const action: cli.Action = std.meta.stringToEnum(
cli.Action,
action_name,
) orelse {
log.warn("unknown action={s}", .{action_name});
return false;
};
cli.mainAction(
state.alloc,
action,
.{ .string = std.mem.sliceTo(args, 0) },
) catch |err| {
log.warn("failed to run action={s} err={}", .{
@tagName(action),
err,
});
return false;
};
return true;
}

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

View File

@@ -1,5 +1,6 @@
pub const cli = @import("cli.zig");
pub const Benchmark = @import("Benchmark.zig");
pub const CApi = @import("CApi.zig");
pub const TerminalStream = @import("TerminalStream.zig");
test {