implement dark/light theme filtering in theme preview

pull/7248/head
Maciej Bartczak 2025-05-01 18:51:24 +02:00
parent 2b4f1f8b84
commit 0b5160e9f0
1 changed files with 40 additions and 5 deletions

View File

@ -24,6 +24,12 @@ pub const Options = struct {
/// If true, force a plain list of themes.
plain: bool = false,
/// If true, print only the dark themes.
dark: bool = false,
/// If true, print only the light themes.
light: bool = false,
pub fn deinit(self: Options) void {
_ = self;
}
@ -137,11 +143,30 @@ pub fn run(gpa_alloc: std.mem.Allocator) !u8 {
if (std.mem.eql(u8, entry.name, ".DS_Store"))
continue;
count += 1;
try themes.append(.{
.location = loc.location,
.path = try std.fs.path.join(alloc, &.{ loc.dir, entry.name }),
.theme = try alloc.dupe(u8, entry.name),
});
const path = try std.fs.path.join(alloc, &.{ loc.dir, entry.name });
// if there is no need to filter just append the theme to the list
if (!opts.dark and !opts.light) {
try themes.append(.{
.path = path,
.location = loc.location,
.theme = try alloc.dupe(u8, entry.name),
});
continue;
}
// otherwise check if the theme should be included based on the provided options
var config = try Config.default(alloc);
defer config.deinit();
try config.loadFile(config._arena.?.allocator(), path);
if (shouldIncludeTheme(opts, config)) {
try themes.append(.{
.path = path,
.location = loc.location,
.theme = try alloc.dupe(u8, entry.name),
});
}
},
else => {},
}
@ -1594,3 +1619,13 @@ fn preview(allocator: std.mem.Allocator, themes: []ThemeListElement) !void {
defer app.deinit();
try app.run();
}
fn shouldIncludeTheme(opts: Options, theme_config: Config) bool {
const rf = @as(f32, @floatFromInt(theme_config.background.r)) / 255.0;
const gf = @as(f32, @floatFromInt(theme_config.background.g)) / 255.0;
const bf = @as(f32, @floatFromInt(theme_config.background.b)) / 255.0;
const luminance = 0.2126 * rf + 0.7152 * gf + 0.0722 * bf;
const is_dark = luminance < 0.5;
return (opts.dark and is_dark) or (opts.light and !is_dark);
}