Skip to content

Commit

Permalink
defines a QuickTerminalSize type, for use with the quick-terminal-siz…
Browse files Browse the repository at this point in the history
…e config

It's not yet a full implementation, but it's a start

WIP of #2384
  • Loading branch information
lucymhdavies committed Oct 10, 2024
1 parent 75b8dc1 commit d2cf51a
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions src/config/Config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,10 @@ keybind: Keybinds = .{},
/// by the operating system.
@"quick-terminal-screen": QuickTerminalScreen = .main,

/// TODO: DOCS
/// https://github.com/ghostty-org/ghostty/issues/2384#issuecomment-2401129283
@"quick-terminal-size": QuickTerminalSize = .{ .value = 0.25, .mode = QuickTerminalSizeMode.percentage },

/// Whether to enable shell integration auto-injection or not. Shell integration
/// greatly enhances the terminal experience by enabling a number of features:
///
Expand Down Expand Up @@ -4832,3 +4836,103 @@ test "test entryFormatter" {
try p.formatEntry(formatterpkg.entryFormatter("a", buf.writer()));
try std.testing.expectEqualStrings("a = 584y 49w 23h 34m 33s 709ms 551µs 615ns\n", buf.items);
}

/// Initial implementation, only using 1 dimension for now
/// Per https://github.com/ghostty-org/ghostty/issues/2384, we may eventually
/// support 2 dimensions
pub const QuickTerminalSizeMode = enum {
pixels,
percentage,
};

pub const QuickTerminalSize = struct {
const Self = @This();

value: f64 = 0,
mode: QuickTerminalSizeMode = QuickTerminalSizeMode.percentage,

pub fn clone(self: Self, _: Allocator) !Self {
return self;
}

pub fn equal(self: Self, other: Self) bool {
return std.meta.eql(self, other);
}

pub fn parseCLI(input_: ?[]const u8) !QuickTerminalSize {
const input = input_ orelse return error.ValueRequired;

// strip the whitespace and see what we're left with
const whitespace = " \t";
var trimmedInput = std.mem.trim(u8, input, whitespace);

// 0 chars: trivially invalid
// 1 char: invalid because there's no room for both a number and a unit
if (trimmedInput.len <= 1) {
return error.InvalidValue;
}

var mode: QuickTerminalSizeMode = undefined;

if (trimmedInput[trimmedInput.len - 1] == '%') {
mode = QuickTerminalSizeMode.percentage;
trimmedInput = trimmedInput[0..(trimmedInput.len - 1)];
} else if (trimmedInput.len >= 2 and std.mem.endsWith(u8, trimmedInput, "px")) {
mode = QuickTerminalSizeMode.pixels;
trimmedInput = trimmedInput[0..(trimmedInput.len - 2)];
} else {
return error.InvalidValue;
}

var value = std.fmt.parseFloat(
f64,
trimmedInput,
) catch return error.InvalidValue;

if (mode == QuickTerminalSizeMode.percentage) {
value = value / 100;
}

return .{ .value = value, .mode = mode };
}

pub fn formatEntry(self: Self, formatter: anytype) !void {
// placeholder for now
_ = self;
_ = formatter;
}

// Reminder for myself: zig build test -Dtest-filter=QuickTerminalSize
test "parse QuickTerminalSize" {
const testing = std.testing;

{
const v = try QuickTerminalSize.parseCLI("25%");
try testing.expectEqual(QuickTerminalSize{
.value = 0.25,
.mode = QuickTerminalSizeMode.percentage,
}, v);
}

{
const v = try QuickTerminalSize.parseCLI(" 42% ");
try testing.expectEqual(QuickTerminalSize{
.value = 0.42,
.mode = QuickTerminalSizeMode.percentage,
}, v);
}

{
const v = try QuickTerminalSize.parseCLI("42px");
try testing.expectEqual(QuickTerminalSize{
.value = 42,
.mode = QuickTerminalSizeMode.pixels,
}, v);
}

try testing.expectError(error.ValueRequired, QuickTerminalSize.parseCLI(null));
try testing.expectError(error.InvalidValue, QuickTerminalSize.parseCLI(""));
try testing.expectError(error.InvalidValue, QuickTerminalSize.parseCLI(" "));
try testing.expectError(error.InvalidValue, QuickTerminalSize.parseCLI("a"));
}
};

0 comments on commit d2cf51a

Please sign in to comment.