Files
ghostty/src/cli/crash_report.zig
Chris Marchesi e8525c0fd9 Update to Zig 0.16.0
This commit represents the majority of the work necessary to upgrade
Ghostty to use Zig 0.16.0.

Key parts:

* In addition to its previous responsibilities, the global state now
  houses state for global I/O implementations and the process
  environment. It is now also utilized in the main application along
  with the C library. Where necessary, global state is isolated from key
  parts of the implementation (e.g., in libghostty subsystems), and it's
  expected that this list will grow.

* We currently manage our own C translation layer where necessary. In
  these cases, cImport has been removed in favor of the new external
  translate-c package. Due to fixes that have needed be made to properly
  translate the dependencies that were swapped out, as mentioned, we
  have had to backport fixes from the current translate-c package (and
  the upstream Arocc dependency). We will host this ourselves until Zig
  0.17.0 is released with these fixes.

* Where necessary (only a small number of cases), some stdlib code from
  0.15.2 (and even from 0.17.0) has been taken, adopted, and vendored in
  lib/compat.

Co-authored-by: Leah Amelia Chen <hi@pluie.me>
2026-07-21 12:35:05 -07:00

95 lines
2.9 KiB
Zig

const std = @import("std");
const Allocator = std.mem.Allocator;
const args = @import("args.zig");
const Action = @import("ghostty.zig").Action;
const Config = @import("../config.zig").Config;
const crash = @import("../crash/main.zig");
const global = @import("../global.zig");
pub const Options = struct {
pub fn deinit(self: Options) void {
_ = self;
}
/// Enables "-h" and "--help" to work.
pub fn help(self: Options) !void {
_ = self;
return Action.help_error;
}
};
/// The `crash-report` command is used to inspect and send crash reports.
///
/// When executed without any arguments, this will list existing crash reports.
///
/// This command currently only supports listing crash reports. Viewing
/// and sending crash reports is unimplemented and will be added in the future.
pub fn run(alloc_gpa: Allocator) !u8 {
// Use an arena for the whole command to avoid manual memory management.
var arena = std.heap.ArenaAllocator.init(alloc_gpa);
defer arena.deinit();
const alloc = arena.allocator();
var opts: Options = .{};
defer opts.deinit();
{
var iter = try args.argsIterator(alloc_gpa, global.args());
defer iter.deinit();
try args.parse(Options, alloc_gpa, &opts, &iter);
}
var buffer: [1024]u8 = undefined;
var stdout_file: std.Io.File = .stdout();
var stdout_writer = stdout_file.writer(global.io(), &buffer);
const stdout = &stdout_writer.interface;
const result = runInner(alloc, &stdout_file, stdout);
stdout.flush() catch {};
return result;
}
fn runInner(
alloc: Allocator,
stdout_file: *std.Io.File,
stdout: *std.Io.Writer,
) !u8 {
const crash_dir = try crash.defaultDir(alloc);
var reports: std.ArrayList(crash.Report) = .empty;
errdefer reports.deinit(alloc);
var it = try crash_dir.iterator();
while (try it.next()) |report| try reports.append(alloc, .{
.name = try alloc.dupe(u8, report.name),
.mtime = report.mtime,
});
// If we have no reports, then we're done. If we have a tty then we
// print a message, otherwise we do nothing.
if (reports.items.len == 0) {
if (try stdout_file.isTty(global.io())) {
try stdout.writeAll("No crash reports! 👻\n");
}
return 0;
}
std.mem.sort(crash.Report, reports.items, {}, lt);
for (reports.items) |report| {
var buf: [128]u8 = undefined;
const now = std.Io.Timestamp.now(global.io(), .real).toNanoseconds();
const diff = now - report.mtime;
const since = if (diff <= 0) "now" else s: {
const d = Config.Duration{ .duration = @intCast(diff) };
break :s try std.fmt.bufPrint(&buf, "{f} ago", .{d.round(std.time.ns_per_s)});
};
try stdout.print("{s} ({s})\n", .{ report.name, since });
}
return 0;
}
fn lt(_: void, lhs: crash.Report, rhs: crash.Report) bool {
return lhs.mtime > rhs.mtime;
}