support different base for palette keys in config (#4298)

motivated by the desire to align config lines

https://ziglang.org/documentation/master/std/#std.fmt.parseInt

unaligned

```ini
palette = 0=#333333

palette = 1=#FF9999
palette = 2=#99FF99
palette = 4=#9999FF

palette = 3=#FFFF33
palette = 5=#FF33FF
palette = 6=#33FFFF

palette = 7=#CCCCCC
palette = 8=#666666

palette = 9=#FF66CC
palette = 10=#CCFF66
palette = 12=#66CCFF

palette = 11=#FFCC66
palette = 13=#CC66FF
palette = 14=#66FFCC

palette = 15=#FFFFFF
```

expecting

```ini
palette = 0x0=#333

palette = 0x1=#F99
palette = 0x2=#9F9
palette = 0x4=#99F

palette = 0x3=#FF3
palette = 0x5=#F3F
palette = 0x6=#3FF

palette = 0x7=#CCC
palette = 0x8=#666

palette = 0x9=#F6C
palette = 0xA=#CF6
palette = 0xC=#6CF

palette = 0xB=#FC6
palette = 0xD=#C6F
palette = 0xE=#6FC

palette = 0xF=#FFF
```
This commit is contained in:
Mitchell Hashimoto
2025-01-02 15:37:20 -08:00
committed by GitHub

View File

@ -447,6 +447,10 @@ foreground: Color = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF },
/// the 256 colors in the terminal color table) and `COLOR` is a typical RGB
/// color code such as `#AABBCC` or `AABBCC`, or a named X11 color.
///
/// The palette index can be in decimal, binary, octal, or hexadecimal.
/// Decimal is assumed unless a prefix is used: `0b` for binary, `0o` for octal,
/// and `0x` for hexadecimal.
///
/// For definitions on the color indices and what they canonically map to,
/// [see this cheat sheet](https://www.ditig.com/256-colors-cheat-sheet).
palette: Palette = .{},
@ -4125,7 +4129,7 @@ pub const Palette = struct {
const eqlIdx = std.mem.indexOf(u8, value, "=") orelse
return error.InvalidValue;
const key = try std.fmt.parseInt(u8, value[0..eqlIdx], 10);
const key = try std.fmt.parseInt(u8, value[0..eqlIdx], 0);
const rgb = try Color.parseCLI(value[eqlIdx + 1 ..]);
self.value[key] = .{ .r = rgb.r, .g = rgb.g, .b = rgb.b };
}
@ -4165,6 +4169,28 @@ pub const Palette = struct {
try testing.expect(p.value[0].b == 0xCC);
}
test "parseCLI base" {
const testing = std.testing;
var p: Self = .{};
try p.parseCLI("0b1=#014589");
try p.parseCLI("0o7=#234567");
try p.parseCLI("0xF=#ABCDEF");
try testing.expect(p.value[0b1].r == 0x01);
try testing.expect(p.value[0b1].g == 0x45);
try testing.expect(p.value[0b1].b == 0x89);
try testing.expect(p.value[0o7].r == 0x23);
try testing.expect(p.value[0o7].g == 0x45);
try testing.expect(p.value[0o7].b == 0x67);
try testing.expect(p.value[0xF].r == 0xAB);
try testing.expect(p.value[0xF].g == 0xCD);
try testing.expect(p.value[0xF].b == 0xEF);
}
test "parseCLI overflow" {
const testing = std.testing;