ghostty/src/config.zig
2022-07-21 21:35:49 -07:00

78 lines
2.3 KiB
Zig

const std = @import("std");
const ArenaAllocator = std.heap.ArenaAllocator;
pub const Config = struct {
/// Font size
@"font-size": u8 = 14,
/// Background color for the window.
background: Color = .{ .r = 0, .g = 0, .b = 0 },
/// Foreground color for the window.
foreground: Color = .{ .r = 0xFF, .g = 0xA5, .b = 0 },
/// The command to run, usually a shell. If this is not an absolute path,
/// it'll be looked up in the PATH.
command: ?[]const u8 = null,
/// This is set by the CLI parser for deinit.
_arena: ?ArenaAllocator = null,
pub fn deinit(self: *Config) void {
if (self._arena) |arena| arena.deinit();
self.* = undefined;
}
};
/// Color represents a color using RGB.
pub const Color = struct {
r: u8,
g: u8,
b: u8,
pub const Error = error{
InvalidFormat,
};
pub fn parseCLI(input: ?[]const u8) !Color {
return fromHex(input orelse return error.ValueRequired);
}
/// fromHex parses a color from a hex value such as #RRGGBB. The "#"
/// is optional.
pub fn fromHex(input: []const u8) !Color {
// Trim the beginning '#' if it exists
const trimmed = if (input.len != 0 and input[0] == '#') input[1..] else input;
// We expect exactly 6 for RRGGBB
if (trimmed.len != 6) return Error.InvalidFormat;
// Parse the colors two at a time.
var result: Color = undefined;
comptime var i: usize = 0;
inline while (i < 6) : (i += 2) {
const v: u8 =
((try std.fmt.charToDigit(trimmed[i], 16)) * 16) +
try std.fmt.charToDigit(trimmed[i + 1], 16);
@field(result, switch (i) {
0 => "r",
2 => "g",
4 => "b",
else => unreachable,
}) = v;
}
return result;
}
};
test "Color.fromHex" {
const testing = std.testing;
try testing.expectEqual(Color{ .r = 0, .g = 0, .b = 0 }, try Color.fromHex("#000000"));
try testing.expectEqual(Color{ .r = 10, .g = 11, .b = 12 }, try Color.fromHex("#0A0B0C"));
try testing.expectEqual(Color{ .r = 10, .g = 11, .b = 12 }, try Color.fromHex("0A0B0C"));
try testing.expectEqual(Color{ .r = 255, .g = 255, .b = 255 }, try Color.fromHex("FFFFFF"));
}