From 0b5160e9f01b6d939a1c213478cbcbec2982e3aa Mon Sep 17 00:00:00 2001 From: Maciej Bartczak <39600846+maciekbartczak@users.noreply.github.com> Date: Thu, 1 May 2025 18:51:24 +0200 Subject: [PATCH 1/2] implement dark/light theme filtering in theme preview --- src/cli/list_themes.zig | 45 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/src/cli/list_themes.zig b/src/cli/list_themes.zig index 8ebac4487..54e71b96f 100644 --- a/src/cli/list_themes.zig +++ b/src/cli/list_themes.zig @@ -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); +} From 418c46538c740c202ae7c35e5756b90d9fcd91a9 Mon Sep 17 00:00:00 2001 From: Maciej Bartczak <39600846+maciekbartczak@users.noreply.github.com> Date: Thu, 1 May 2025 19:41:02 +0200 Subject: [PATCH 2/2] use enum for the color scheme args --- src/cli/list_themes.zig | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/cli/list_themes.zig b/src/cli/list_themes.zig index 54e71b96f..54f4c0969 100644 --- a/src/cli/list_themes.zig +++ b/src/cli/list_themes.zig @@ -24,11 +24,8 @@ 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, + /// Specifies the color scheme of the themes to include in the list. + color: enum { all, dark, light } = .all, pub fn deinit(self: Options) void { _ = self; @@ -99,6 +96,9 @@ const ThemeListElement = struct { /// * `--path`: Show the full path to the theme. /// /// * `--plain`: Force a plain listing of themes. +/// +/// * `--color`: Specify the color scheme of the themes included in the list. +/// This can be `dark`, `light`, or `all`. The default is `all`. pub fn run(gpa_alloc: std.mem.Allocator) !u8 { var opts: Options = .{}; defer opts.deinit(); @@ -146,7 +146,7 @@ pub fn run(gpa_alloc: std.mem.Allocator) !u8 { 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) { + if (opts.color == .all) { try themes.append(.{ .path = path, .location = loc.location, @@ -1627,5 +1627,5 @@ fn shouldIncludeTheme(opts: Options, theme_config: Config) bool { 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); + return (opts.color == .dark and is_dark) or (opts.color == .light and !is_dark); }