implement dark/light theme filtering in theme preview
parent
2b4f1f8b84
commit
0b5160e9f0
|
|
@ -24,6 +24,12 @@ pub const Options = struct {
|
||||||
/// If true, force a plain list of themes.
|
/// If true, force a plain list of themes.
|
||||||
plain: bool = false,
|
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 {
|
pub fn deinit(self: Options) void {
|
||||||
_ = self;
|
_ = self;
|
||||||
}
|
}
|
||||||
|
|
@ -137,11 +143,30 @@ pub fn run(gpa_alloc: std.mem.Allocator) !u8 {
|
||||||
if (std.mem.eql(u8, entry.name, ".DS_Store"))
|
if (std.mem.eql(u8, entry.name, ".DS_Store"))
|
||||||
continue;
|
continue;
|
||||||
count += 1;
|
count += 1;
|
||||||
try themes.append(.{
|
|
||||||
.location = loc.location,
|
const path = try std.fs.path.join(alloc, &.{ loc.dir, entry.name });
|
||||||
.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
|
||||||
.theme = try alloc.dupe(u8, entry.name),
|
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 => {},
|
else => {},
|
||||||
}
|
}
|
||||||
|
|
@ -1594,3 +1619,13 @@ fn preview(allocator: std.mem.Allocator, themes: []ThemeListElement) !void {
|
||||||
defer app.deinit();
|
defer app.deinit();
|
||||||
try app.run();
|
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);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue