mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-08-02 14:57:31 +03:00
Add support for whitespace in color and palette parsing (#5926)
Resolves #5921
This commit is contained in:
@ -4206,14 +4206,16 @@ pub const Color = struct {
|
||||
|
||||
pub fn parseCLI(input_: ?[]const u8) !Color {
|
||||
const input = input_ orelse return error.ValueRequired;
|
||||
// Trim any whitespace before processing
|
||||
const trimmed = std.mem.trim(u8, input, " \t");
|
||||
|
||||
if (terminal.x11_color.map.get(input)) |rgb| return .{
|
||||
if (terminal.x11_color.map.get(trimmed)) |rgb| return .{
|
||||
.r = rgb.r,
|
||||
.g = rgb.g,
|
||||
.b = rgb.b,
|
||||
};
|
||||
|
||||
return fromHex(input);
|
||||
return fromHex(trimmed);
|
||||
}
|
||||
|
||||
/// Deep copy of the struct. Required by Config.
|
||||
@ -4301,6 +4303,18 @@ pub const Color = struct {
|
||||
try color.formatEntry(formatterpkg.entryFormatter("a", buf.writer()));
|
||||
try std.testing.expectEqualSlices(u8, "a = #0a0b0c\n", buf.items);
|
||||
}
|
||||
|
||||
test "parseCLI with whitespace" {
|
||||
const testing = std.testing;
|
||||
try testing.expectEqual(
|
||||
Color{ .r = 0xAA, .g = 0xBB, .b = 0xCC },
|
||||
try Color.parseCLI(" #AABBCC "),
|
||||
);
|
||||
try testing.expectEqual(
|
||||
Color{ .r = 0, .g = 0, .b = 0 },
|
||||
try Color.parseCLI(" black "),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
pub const ColorList = struct {
|
||||
@ -4446,7 +4460,14 @@ 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], 0);
|
||||
// Parse the key part (trim whitespace)
|
||||
const key = try std.fmt.parseInt(
|
||||
u8,
|
||||
std.mem.trim(u8, value[0..eqlIdx], " \t"),
|
||||
0,
|
||||
);
|
||||
|
||||
// Parse the color part (Color.parseCLI will handle whitespace)
|
||||
const rgb = try Color.parseCLI(value[eqlIdx + 1 ..]);
|
||||
self.value[key] = .{ .r = rgb.r, .g = rgb.g, .b = rgb.b };
|
||||
}
|
||||
@ -4524,6 +4545,27 @@ pub const Palette = struct {
|
||||
try list.formatEntry(formatterpkg.entryFormatter("a", buf.writer()));
|
||||
try std.testing.expectEqualSlices(u8, "a = 0=#1d1f21\n", buf.items[0..14]);
|
||||
}
|
||||
|
||||
test "parseCLI with whitespace" {
|
||||
const testing = std.testing;
|
||||
|
||||
var p: Self = .{};
|
||||
try p.parseCLI("0 = #AABBCC");
|
||||
try p.parseCLI(" 1= #DDEEFF ");
|
||||
try p.parseCLI(" 2 = #123456 ");
|
||||
|
||||
try testing.expect(p.value[0].r == 0xAA);
|
||||
try testing.expect(p.value[0].g == 0xBB);
|
||||
try testing.expect(p.value[0].b == 0xCC);
|
||||
|
||||
try testing.expect(p.value[1].r == 0xDD);
|
||||
try testing.expect(p.value[1].g == 0xEE);
|
||||
try testing.expect(p.value[1].b == 0xFF);
|
||||
|
||||
try testing.expect(p.value[2].r == 0x12);
|
||||
try testing.expect(p.value[2].g == 0x34);
|
||||
try testing.expect(p.value[2].b == 0x56);
|
||||
}
|
||||
};
|
||||
|
||||
/// RepeatableString is a string value that can be repeated to accumulate
|
||||
|
Reference in New Issue
Block a user