From ea6d1f7e23257af2aea2b572d2a738685197c061 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 20 Nov 2022 15:00:50 -0800 Subject: [PATCH] config file can accept colors --- src/config.zig | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/config.zig b/src/config.zig index 74926d65e..d9a0291d3 100644 --- a/src/config.zig +++ b/src/config.zig @@ -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