From 53a5734d09f0c588a445e4fcdaa1fd13ca1daf6d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 9 Nov 2023 15:14:33 -0800 Subject: [PATCH] terminal: change mask from u256 to StaticBitSet --- src/inspector/termio.zig | 5 ++--- src/terminal/Terminal.zig | 3 ++- src/termio/Exec.zig | 18 +++++++----------- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/inspector/termio.zig b/src/inspector/termio.zig index a9a2f5cde..1b9a45581 100644 --- a/src/inspector/termio.zig +++ b/src/inspector/termio.zig @@ -241,7 +241,8 @@ pub const VTEvent = struct { 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)); inline for (u.fields) |field| { if (std.mem.eql(u8, field.name, tag_name)) { @@ -256,8 +257,6 @@ pub const VTEvent = struct { try md.put(key, s); } } - } else { - @compileError("Unions must have a tag"); }, else => switch (Value) { diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index 1ea3c94fc..c5f777006 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -82,8 +82,9 @@ default_palette: color.Palette = color.default, /// The color palette to use. The mask indicates which palette indices have been /// modified with OSC 4 color_palette: struct { + const Mask = std.StaticBitSet(@typeInfo(color.Palette).Array.len); colors: color.Palette = color.default, - mask: u256 = 0, + mask: Mask = Mask.initEmpty(), } = .{}, /// The previous printed character. This is used for the repeat previous diff --git a/src/termio/Exec.zig b/src/termio/Exec.zig index 5e2d3c021..03891364f 100644 --- a/src/termio/Exec.zig +++ b/src/termio/Exec.zig @@ -2257,7 +2257,7 @@ const StreamHandler = struct { switch (kind) { .palette => |i| { self.terminal.color_palette.colors[i] = color; - self.terminal.color_palette.mask |= @as(u256, 1) << i; + self.terminal.color_palette.mask.set(i); }, .foreground => { self.foreground_color = color; @@ -2287,19 +2287,15 @@ const StreamHandler = struct { ) !void { switch (kind) { .palette => { - var mask = self.terminal.color_palette.mask; - defer self.terminal.color_palette.mask = mask; - + const mask = &self.terminal.color_palette.mask; if (value.len == 0) { // Find all bit positions in the mask which are set and // reset those indices to the default palette - while (mask != 0) { - // Safe to truncate, mask is non-zero so @ctz can never - // return a u9 - const i: u8 = @truncate(@ctz(mask)); + var it = mask.iterator(.{}); + while (it.next()) |i| { log.warn("Resetting palette color {}", .{i}); self.terminal.color_palette.colors[i] = self.terminal.default_palette[i]; - mask ^= @as(u256, 1) << i; + mask.unset(i); } } else { var it = std.mem.tokenizeScalar(u8, value, ';'); @@ -2307,9 +2303,9 @@ const StreamHandler = struct { // Skip invalid parameters const i = std.fmt.parseUnsigned(u8, param, 10) catch continue; 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]; - mask ^= @as(u256, 1) << i; + mask.unset(i); } } }