lib/tracy/src/cli/run/dispatch.zig

daab053ee43316e1809a84551d573ddd1e5bf3d2

 1 const std = @import("std");
 2 const sys = @import("sys");
 3 const cli = @import("../root.zig");
 4 
 5 const activity = @import("activity.zig");
 6 const inspect = @import("inspect.zig");
 7 const report = @import("report.zig");
 8 const special = @import("special.zig");
 9 const usage = cli.usage;
10 
11 const Runner = *const fn (std.mem.Allocator, []const []const u8) anyerror!u8;
12 
13 const Command = struct {
14     name: []const u8,
15     run: Runner,
16 };
17 
18 const commands = [_]Command{
19     .{ .name = "summary", .run = report.summary },
20     .{ .name = "context", .run = activity.context },
21     .{ .name = "tree", .run = report.tree },
22     .{ .name = "stats", .run = report.stats },
23     .{ .name = "frames", .run = report.frames },
24     .{ .name = "memory", .run = activity.memory },
25     .{ .name = "locks", .run = activity.locks },
26     .{ .name = "messages", .run = activity.messages },
27     .{ .name = "samples", .run = activity.samples },
28     .{ .name = "fibers", .run = activity.fibers },
29     .{ .name = "gpu", .run = activity.gpu },
30     .{ .name = "system", .run = activity.system },
31     .{ .name = "plots", .run = activity.plots },
32     .{ .name = "source", .run = inspect.source },
33     .{ .name = "find", .run = inspect.find },
34     .{ .name = "flame", .run = special.flame },
35     .{ .name = "timeline", .run = inspect.timeline },
36     .{ .name = "compare", .run = special.compare },
37     .{ .name = "export", .run = special.exportCommand },
38 };
39 
40 pub fn dispatch(allocator: std.mem.Allocator, args: []const []const u8) !u8 {
41     if (args.len < 2) {
42         try usage.write(allocator, sys.stdio.stderr());
43         return 1;
44     }
45 
46     const command = args[1];
47     if (isHelpCommand(command)) {
48         try usage.write(allocator, sys.stdio.stdout());
49         return 0;
50     }
51     for (commands) |entry| {
52         if (std.mem.eql(u8, command, entry.name)) return try entry.run(allocator, args[2..]);
53     }
54     return error.UnknownCommand;
55 }
56 
57 fn isHelpCommand(command: []const u8) bool {
58     return std.mem.eql(u8, command, "help") or
59         std.mem.eql(u8, command, "--help") or
60         std.mem.eql(u8, command, "-h");
61 }