From 04c3a938a8cd9fbc6922483d64d0b0a079d2e21c Mon Sep 17 00:00:00 2001 From: adeci Date: Mon, 10 Nov 2025 10:04:49 +0700 Subject: [PATCH] feat: add --themes-dir flag --- src/btop.cpp | 6 ++++++ src/btop_cli.cpp | 19 +++++++++++++++++++ src/btop_cli.hpp | 2 ++ src/btop_theme.cpp | 4 +++- src/btop_theme.hpp | 1 + 5 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/btop.cpp b/src/btop.cpp index 7826594..086681a 100644 --- a/src/btop.cpp +++ b/src/btop.cpp @@ -910,6 +910,12 @@ static auto configure_tty_mode(std::optional force_tty) { } } + //? Set custom themes directory from command line if provided + if (cli.themes_dir.has_value()) { + Theme::custom_theme_dir = cli.themes_dir.value(); + Logger::info("Using custom themes directory: " + Theme::custom_theme_dir.string()); + } + //? Config init init_config(cli.low_color, cli.filter); diff --git a/src/btop_cli.cpp b/src/btop_cli.cpp index 904467a..25f1b62 100644 --- a/src/btop_cli.cpp +++ b/src/btop_cli.cpp @@ -142,6 +142,24 @@ namespace Cli { } continue; } + if (arg == "--themes-dir") { + // This flag requires an argument. + if (++it == args.end()) { + error("Themes directory requires an argument"); + return std::unexpected { 1 }; + } + + auto arg = *it; + auto themes_dir = stdfs::path { arg }; + + if (not stdfs::is_directory(themes_dir)) { + error("Themes directory does not exist or is not a directory"); + return std::unexpected { 1 }; + } + + cli.themes_dir = std::make_optional(themes_dir); + continue; + } if (arg == "-u" || arg == "--update") { // This flag requires an argument. if (++it == args.end()) { @@ -183,6 +201,7 @@ namespace Cli { " {2}-l, --low-color{1} Disable true color, 256 colors only\n" " {2}-p, --preset{1} Start with a preset (0-9)\n" " {2}-t, --tty{1} Force tty mode with ANSI graph symbols and 16 colors only\n" + " {2} --themes-dir{1} Path to a custom themes directory\n" " {2} --no-tty{1} Force disable tty mode\n" " {2}-u, --update{1} Set an initial update rate in milliseconds\n" " {2}-h, --help{1} Show this help message and exit\n" diff --git a/src/btop_cli.hpp b/src/btop_cli.hpp index 50c64b7..92d4fb9 100644 --- a/src/btop_cli.hpp +++ b/src/btop_cli.hpp @@ -28,6 +28,8 @@ namespace Cli { bool low_color {}; // Start with one of the provided presets std::optional preset; + // Path to a custom themes directory + std::optional themes_dir; // The initial refresh rate std::optional updates; }; diff --git a/src/btop_theme.cpp b/src/btop_theme.cpp index a0fe056..2980578 100644 --- a/src/btop_theme.cpp +++ b/src/btop_theme.cpp @@ -41,6 +41,7 @@ namespace Theme { fs::path theme_dir; fs::path user_theme_dir; + fs::path custom_theme_dir; vector themes; std::unordered_map colors; std::unordered_map> rgbs; @@ -418,7 +419,8 @@ namespace Theme { themes.push_back("Default"); themes.push_back("TTY"); - for (const auto& path : { user_theme_dir, theme_dir } ) { + //? Priority: custom_theme_dir -> user_theme_dir -> theme_dir + for (const auto& path : { custom_theme_dir, user_theme_dir, theme_dir } ) { if (path.empty()) continue; for (auto& file : fs::directory_iterator(path)) { if (file.path().extension() == ".theme" and access(file.path().c_str(), R_OK) != -1 and not v_contains(themes, file.path().c_str())) { diff --git a/src/btop_theme.hpp b/src/btop_theme.hpp index 10a37ce..695fd3d 100644 --- a/src/btop_theme.hpp +++ b/src/btop_theme.hpp @@ -31,6 +31,7 @@ using std::vector; namespace Theme { extern std::filesystem::path theme_dir; extern std::filesystem::path user_theme_dir; + extern std::filesystem::path custom_theme_dir; //* Contains "Default" and "TTY" at indices 0 and 1, otherwise full paths to theme files extern vector themes;