make quit-after-last-window-closed-delay an optional

This commit is contained in:
Jeffrey C. Ollie
2024-08-01 10:51:08 -05:00
parent 143e503d43
commit 3d6ca14dc6
2 changed files with 85 additions and 60 deletions

View File

@ -512,11 +512,10 @@ pub fn run(self: *App) !void {
if (self.config.@"quit-after-last-window-closed") { if (self.config.@"quit-after-last-window-closed") {
// If the background timeout is not zero, check to see if // If the background timeout is not zero, check to see if
// the timeout has elapsed. // the timeout has elapsed.
if (self.config.@"quit-after-last-window-closed-delay".duration != 0) { if (self.config.@"quit-after-last-window-closed-delay") |duration| {
const now = try std.time.Instant.now(); const now = try std.time.Instant.now();
if (now.since(last_one) > self.config.@"quit-after-last-window-closed-delay".duration) { if (now.since(last_one) > duration.duration) {
log.info("timeout elapsed", .{});
// The timeout has elapsed, quit. // The timeout has elapsed, quit.
break :q true; break :q true;
} }
@ -525,7 +524,7 @@ pub fn run(self: *App) !void {
break :q false; break :q false;
} }
// `quit-after-last-window-closed-delay` is zero, don't quit. // `quit-after-last-window-closed-delay` is not set, don't quit.
break :q false; break :q false;
} }

View File

@ -887,13 +887,13 @@ keybind: Keybinds = .{},
@"quit-after-last-window-closed": bool = false, @"quit-after-last-window-closed": bool = false,
/// If `quit-after-last-window-closed` is `true`, this controls how long /// If `quit-after-last-window-closed` is `true`, this controls how long
/// Ghostty will stay running after the last open surface has been closed. If /// Ghostty will stay running after the last open surface has been closed. If
/// `quit-after-last-window-closed-delay` is set to 0 ns Ghostty will remain /// `quit-after-last-window-closed-delay` is unset Ghostty will remain running
/// running indefinitely. The duration should be at least long enough to allow /// indefinitely. The duration should be at least long enough to allow Ghostty
/// Ghostty to initialize and open it's first window, but this is not enforced /// to initialize and open it's first window, but this is not enforced nor
/// nor will a warning be issued. The duration is specified as a series of /// will a warning be issued. The duration is specified as a series of numbers
/// numbers followed by time units. Whitespace is allowed between numbers and /// followed by time units. Whitespace is allowed between numbers and units. The
/// units. The allowed time units are as follows: /// allowed time units are as follows:
/// ///
/// * `y` - 365 SI days, or 8760 hours, or 31536000 seconds. No adjustments /// * `y` - 365 SI days, or 8760 hours, or 31536000 seconds. No adjustments
/// are made for leap years or leap seconds. /// are made for leap years or leap seconds.
@ -911,10 +911,10 @@ keybind: Keybinds = .{},
/// ///
/// The maximum value is `584y 49w 23h 34m 33s 709ms 551µs 615ns`. /// The maximum value is `584y 49w 23h 34m 33s 709ms 551µs 615ns`.
/// ///
/// By default `quit-after-last-window-closed-delay` is set to `0 ns`. /// By default `quit-after-last-window-closed-delay` is unset.
/// ///
/// Only implemented on Linux. /// Only implemented on Linux.
@"quit-after-last-window-closed-delay": Duration = .{ .duration = 0 }, @"quit-after-last-window-closed-delay": ?Duration = null,
/// This controls whether an initial window is created when Ghostty is run. Only /// This controls whether an initial window is created when Ghostty is run. Only
/// implemented on Linux. /// implemented on Linux.
@ -3801,11 +3801,8 @@ pub const Duration = struct {
return self.duration == other.duration; return self.duration == other.duration;
} }
pub fn parseCLI(self: *@This(), _: Allocator, input: ?[]const u8) !void { pub fn parseCLI(input: ?[]const u8) !Duration {
var remaining = input orelse { var remaining = input orelse return error.ValueRequired;
self.duration = 0;
return;
};
const units = [_]struct { const units = [_]struct {
name: []const u8, name: []const u8,
@ -3887,7 +3884,7 @@ pub const Duration = struct {
value = number * factor; value = number * factor;
} }
self.duration = value orelse 0; return if (value) |v| .{ .duration = v } else error.ValueRequired;
} }
pub fn formatEntry(self: @This(), formatter: anytype) !void { pub fn formatEntry(self: @This(), formatter: anytype) !void {
@ -3929,63 +3926,92 @@ pub const Duration = struct {
}; };
test "parse duration" { test "parse duration" {
var d: Duration = undefined; {
const d = try Duration.parseCLI("0ns");
try std.testing.expectEqual(@as(u64, 0), d.duration);
}
try d.parseCLI(std.testing.allocator, ""); {
try std.testing.expectEqual(@as(u64, 0), d.duration); const d = try Duration.parseCLI("1ns");
try std.testing.expectEqual(@as(u64, 1), d.duration);
}
try d.parseCLI(std.testing.allocator, "0ns"); {
try std.testing.expectEqual(@as(u64, 0), d.duration); const d = try Duration.parseCLI("100ns");
try std.testing.expectEqual(@as(u64, 100), d.duration);
}
try d.parseCLI(std.testing.allocator, "1ns"); {
try std.testing.expectEqual(@as(u64, 1), d.duration); const d = try Duration.parseCLI("1µs");
try std.testing.expectEqual(@as(u64, 1000), d.duration);
}
try d.parseCLI(std.testing.allocator, "100ns"); {
try std.testing.expectEqual(@as(u64, 100), d.duration); const d = try Duration.parseCLI("1µs1ns");
try std.testing.expectEqual(@as(u64, 1001), d.duration);
}
try d.parseCLI(std.testing.allocator, "1µs"); {
try std.testing.expectEqual(@as(u64, 1000), d.duration); const d = try Duration.parseCLI("1µs 1ns");
try std.testing.expectEqual(@as(u64, 1001), d.duration);
}
try d.parseCLI(std.testing.allocator, "1µs1ns"); {
try std.testing.expectEqual(@as(u64, 1001), d.duration); const d = try Duration.parseCLI(" 1µs1ns");
try std.testing.expectEqual(@as(u64, 1001), d.duration);
}
try d.parseCLI(std.testing.allocator, "1µs 1ns"); {
try std.testing.expectEqual(@as(u64, 1001), d.duration); const d = try Duration.parseCLI("1µs1ns ");
try std.testing.expectEqual(@as(u64, 1001), d.duration);
}
try d.parseCLI(std.testing.allocator, " 1µs1ns"); {
try std.testing.expectEqual(@as(u64, 1001), d.duration); const d = try Duration.parseCLI("1y");
try std.testing.expectEqual(@as(u64, 365 * std.time.ns_per_day), d.duration);
}
try d.parseCLI(std.testing.allocator, "1µs1ns "); {
try std.testing.expectEqual(@as(u64, 1001), d.duration); const d = try Duration.parseCLI("1d");
try std.testing.expectEqual(@as(u64, std.time.ns_per_day), d.duration);
}
try d.parseCLI(std.testing.allocator, "1y"); {
try std.testing.expectEqual(@as(u64, 365 * std.time.ns_per_day), d.duration); const d = try Duration.parseCLI("1h");
try std.testing.expectEqual(@as(u64, std.time.ns_per_hour), d.duration);
}
try d.parseCLI(std.testing.allocator, "1d"); {
try std.testing.expectEqual(@as(u64, std.time.ns_per_day), d.duration); const d = try Duration.parseCLI("1m");
try std.testing.expectEqual(@as(u64, std.time.ns_per_min), d.duration);
}
try d.parseCLI(std.testing.allocator, "1h"); {
try std.testing.expectEqual(@as(u64, std.time.ns_per_hour), d.duration); const d = try Duration.parseCLI("1s");
try std.testing.expectEqual(@as(u64, std.time.ns_per_s), d.duration);
}
try d.parseCLI(std.testing.allocator, "1m"); {
try std.testing.expectEqual(@as(u64, std.time.ns_per_min), d.duration); const d = try Duration.parseCLI("1ms");
try std.testing.expectEqual(@as(u64, std.time.ns_per_ms), d.duration);
}
try d.parseCLI(std.testing.allocator, "1s"); {
try std.testing.expectEqual(@as(u64, std.time.ns_per_s), d.duration); const d = try Duration.parseCLI("30s");
try std.testing.expectEqual(@as(u64, 30 * std.time.ns_per_s), d.duration);
}
try d.parseCLI(std.testing.allocator, "1ms"); {
try std.testing.expectEqual(@as(u64, std.time.ns_per_ms), d.duration); const d = try Duration.parseCLI("584y 49w 23h 34m 33s 709ms 551µs 615ns");
try std.testing.expectEqual(std.math.maxInt(u64), d.duration);
}
try d.parseCLI(std.testing.allocator, "30s"); try std.testing.expectError(error.ValueRequired, Duration.parseCLI(null));
try std.testing.expectEqual(@as(u64, 30 * std.time.ns_per_s), d.duration); try std.testing.expectError(error.ValueRequired, Duration.parseCLI(""));
try std.testing.expectError(error.InvalidValue, Duration.parseCLI("1"));
try d.parseCLI(std.testing.allocator, "584y 49w 23h 34m 33s 709ms 551µs 615ns"); try std.testing.expectError(error.InvalidValue, Duration.parseCLI("s"));
try std.testing.expectEqual(std.math.maxInt(u64), d.duration); try std.testing.expectError(error.InvalidValue, Duration.parseCLI("1x"));
try std.testing.expectError(error.InvalidValue, Duration.parseCLI("1 "));
try std.testing.expectError(error.InvalidValue, d.parseCLI(std.testing.allocator, "1"));
try std.testing.expectError(error.InvalidValue, d.parseCLI(std.testing.allocator, "s"));
try std.testing.expectError(error.InvalidValue, d.parseCLI(std.testing.allocator, "1x"));
try std.testing.expectError(error.InvalidValue, d.parseCLI(std.testing.allocator, "1 "));
} }
test "format duration" { test "format duration" {