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
This commit is contained in:
@ -4204,14 +4204,16 @@ pub const Color = struct {
|
|||||||
|
|
||||||
pub fn parseCLI(input_: ?[]const u8) !Color {
|
pub fn parseCLI(input_: ?[]const u8) !Color {
|
||||||
const input = input_ orelse return error.ValueRequired;
|
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,
|
.r = rgb.r,
|
||||||
.g = rgb.g,
|
.g = rgb.g,
|
||||||
.b = rgb.b,
|
.b = rgb.b,
|
||||||
};
|
};
|
||||||
|
|
||||||
return fromHex(input);
|
return fromHex(trimmed);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deep copy of the struct. Required by Config.
|
/// Deep copy of the struct. Required by Config.
|
||||||
@ -4299,6 +4301,18 @@ pub const Color = struct {
|
|||||||
try color.formatEntry(formatterpkg.entryFormatter("a", buf.writer()));
|
try color.formatEntry(formatterpkg.entryFormatter("a", buf.writer()));
|
||||||
try std.testing.expectEqualSlices(u8, "a = #0a0b0c\n", buf.items);
|
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 {
|
pub const ColorList = struct {
|
||||||
@ -4444,7 +4458,14 @@ pub const Palette = struct {
|
|||||||
const eqlIdx = std.mem.indexOf(u8, value, "=") orelse
|
const eqlIdx = std.mem.indexOf(u8, value, "=") orelse
|
||||||
return error.InvalidValue;
|
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 ..]);
|
const rgb = try Color.parseCLI(value[eqlIdx + 1 ..]);
|
||||||
self.value[key] = .{ .r = rgb.r, .g = rgb.g, .b = rgb.b };
|
self.value[key] = .{ .r = rgb.r, .g = rgb.g, .b = rgb.b };
|
||||||
}
|
}
|
||||||
@ -4522,6 +4543,27 @@ pub const Palette = struct {
|
|||||||
try list.formatEntry(formatterpkg.entryFormatter("a", buf.writer()));
|
try list.formatEntry(formatterpkg.entryFormatter("a", buf.writer()));
|
||||||
try std.testing.expectEqualSlices(u8, "a = 0=#1d1f21\n", buf.items[0..14]);
|
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
|
/// RepeatableString is a string value that can be repeated to accumulate
|
||||||
|
Reference in New Issue
Block a user