config file can accept colors

This commit is contained in:
Mitchell Hashimoto
2022-11-20 15:00:50 -08:00
parent 89834c1412
commit ea6d1f7e23

View File

@ -4,6 +4,7 @@ const Allocator = std.mem.Allocator;
const ArenaAllocator = std.heap.ArenaAllocator;
const inputpkg = @import("input.zig");
const passwd = @import("passwd.zig");
const terminal = @import("terminal/main.zig");
const log = std.log.scoped(.config);
@ -347,6 +348,49 @@ pub const Color = struct {
}
};
/// Palette is the 256 color palette for 256-color mode. This is still
/// used by many terminal applications.
pub const Palette = struct {
const Self = @This();
/// The actual value that is updated as we parse.
value: terminal.color.Palette = terminal.color.default,
pub const Error = error{
InvalidFormat,
};
pub fn parseCLI(
self: *Self,
input: ?[]const u8,
) !void {
const value = input orelse return error.ValueRequired;
const eqlIdx = std.mem.indexOf(u8, value, "=") orelse
return Error.InvalidFormat;
const key = try std.fmt.parseInt(u8, value[0..eqlIdx], 10);
const rgb = try Color.parseCLI(value[eqlIdx + 1 ..]);
self.value[key] = .{ .r = rgb.r, .g = rgb.g, .b = rgb.b };
}
test "parseCLI" {
const testing = std.testing;
var p: Self = .{};
try p.parseCLI("0=#AABBCC");
try testing.expect(p.value[0].r == 0xAA);
try testing.expect(p.value[0].g == 0xBB);
try testing.expect(p.value[0].b == 0xCC);
}
test "parseCLI overflow" {
const testing = std.testing;
var p: Self = .{};
try testing.expectError(error.Overflow, p.parseCLI("256=#AABBCC"));
}
};
/// RepeatableString is a string value that can be repeated to accumulate
/// a list of strings. This isn't called "StringList" because I find that
/// sometimes leads to confusion that it _accepts_ a list such as