terminal: change mask from u256 to StaticBitSet

This commit is contained in:
Mitchell Hashimoto
2023-11-09 15:14:33 -08:00
parent 171292a063
commit 53a5734d09
3 changed files with 11 additions and 15 deletions

View File

@ -241,7 +241,8 @@ pub const VTEvent = struct {
try alloc.dupeZ(u8, @tagName(value)), try alloc.dupeZ(u8, @tagName(value)),
), ),
.Union => |u| if (u.tag_type) |Tag| { .Union => |u| {
const Tag = u.tag_type orelse @compileError("Unions must have a tag");
const tag_name = @tagName(@as(Tag, value)); const tag_name = @tagName(@as(Tag, value));
inline for (u.fields) |field| { inline for (u.fields) |field| {
if (std.mem.eql(u8, field.name, tag_name)) { if (std.mem.eql(u8, field.name, tag_name)) {
@ -256,8 +257,6 @@ pub const VTEvent = struct {
try md.put(key, s); try md.put(key, s);
} }
} }
} else {
@compileError("Unions must have a tag");
}, },
else => switch (Value) { else => switch (Value) {

View File

@ -82,8 +82,9 @@ default_palette: color.Palette = color.default,
/// The color palette to use. The mask indicates which palette indices have been /// The color palette to use. The mask indicates which palette indices have been
/// modified with OSC 4 /// modified with OSC 4
color_palette: struct { color_palette: struct {
const Mask = std.StaticBitSet(@typeInfo(color.Palette).Array.len);
colors: color.Palette = color.default, colors: color.Palette = color.default,
mask: u256 = 0, mask: Mask = Mask.initEmpty(),
} = .{}, } = .{},
/// The previous printed character. This is used for the repeat previous /// The previous printed character. This is used for the repeat previous

View File

@ -2257,7 +2257,7 @@ const StreamHandler = struct {
switch (kind) { switch (kind) {
.palette => |i| { .palette => |i| {
self.terminal.color_palette.colors[i] = color; self.terminal.color_palette.colors[i] = color;
self.terminal.color_palette.mask |= @as(u256, 1) << i; self.terminal.color_palette.mask.set(i);
}, },
.foreground => { .foreground => {
self.foreground_color = color; self.foreground_color = color;
@ -2287,19 +2287,15 @@ const StreamHandler = struct {
) !void { ) !void {
switch (kind) { switch (kind) {
.palette => { .palette => {
var mask = self.terminal.color_palette.mask; const mask = &self.terminal.color_palette.mask;
defer self.terminal.color_palette.mask = mask;
if (value.len == 0) { if (value.len == 0) {
// Find all bit positions in the mask which are set and // Find all bit positions in the mask which are set and
// reset those indices to the default palette // reset those indices to the default palette
while (mask != 0) { var it = mask.iterator(.{});
// Safe to truncate, mask is non-zero so @ctz can never while (it.next()) |i| {
// return a u9
const i: u8 = @truncate(@ctz(mask));
log.warn("Resetting palette color {}", .{i}); log.warn("Resetting palette color {}", .{i});
self.terminal.color_palette.colors[i] = self.terminal.default_palette[i]; self.terminal.color_palette.colors[i] = self.terminal.default_palette[i];
mask ^= @as(u256, 1) << i; mask.unset(i);
} }
} else { } else {
var it = std.mem.tokenizeScalar(u8, value, ';'); var it = std.mem.tokenizeScalar(u8, value, ';');
@ -2307,9 +2303,9 @@ const StreamHandler = struct {
// Skip invalid parameters // Skip invalid parameters
const i = std.fmt.parseUnsigned(u8, param, 10) catch continue; const i = std.fmt.parseUnsigned(u8, param, 10) catch continue;
log.warn("Resetting palette color {}", .{i}); log.warn("Resetting palette color {}", .{i});
if (mask & (@as(u256, 1) << i) != 0) { if (mask.isSet(i)) {
self.terminal.color_palette.colors[i] = self.terminal.default_palette[i]; self.terminal.color_palette.colors[i] = self.terminal.default_palette[i];
mask ^= @as(u256, 1) << i; mask.unset(i);
} }
} }
} }