add module enum and module registry

This commit is contained in:
2026-06-02 00:43:55 +03:00
parent cdd4afd105
commit 4cb2b2726d
+46 -31
View File
@@ -9,6 +9,7 @@
#include <iomanip> #include <iomanip>
#include <unordered_map> #include <unordered_map>
#include <cctype> #include <cctype>
#include <string_view>
#include <sys/utsname.h> #include <sys/utsname.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <ctime> #include <ctime>
@@ -88,40 +89,54 @@ struct Config {
std::unordered_map<Module, Color, EnumHash> colors; std::unordered_map<Module, Color, EnumHash> colors;
}; };
struct ModuleSpec {
Module id;
std::string_view name;
string defaultLabel;
Color defaultColor;
string (*getter)();
};
static const std::array<ModuleSpec, 8>& moduleSpecs() {
static const std::array<ModuleSpec, 8> specs = {
ModuleSpec{Module::Distro, "distro", " distro:", Color::Green, &getDistro},
ModuleSpec{Module::Kernel, "kernel", " kernel:", Color::Magenta, &getKernel},
ModuleSpec{Module::Uptime, "uptime", " uptime:", Color::Blue, &getUptime},
ModuleSpec{Module::Shell, "shell", " shell:", Color::Magenta, &getShell},
ModuleSpec{Module::CPU, "cpu", "󰍛 CPU:", Color::Yellow, &getCPU},
ModuleSpec{Module::GPU, "gpu", "󰾲 GPU:", Color::Yellow, &getGPU},
ModuleSpec{Module::RAM, "ram", " RAM:", Color::Red, &getRAM},
ModuleSpec{Module::OsDate, "os_date", " OS Date:", Color::Blue, &getOsDate},
};
return specs;
}
static const ModuleSpec* findModuleSpec(std::string_view name) {
for (const auto& spec : moduleSpecs()) {
if (spec.name == name) return &spec;
}
return nullptr;
}
static const ModuleSpec* findModuleSpec(Module m) {
for (const auto& spec : moduleSpecs()) {
if (spec.id == m) return &spec;
}
return nullptr;
}
Config defaultConfig() { Config defaultConfig() {
Config cfg; Config cfg;
cfg.modules = {
Module::Distro,
Module::Kernel,
Module::Uptime,
Module::Shell,
Module::CPU,
Module::GPU,
Module::RAM,
Module::OsDate,
};
cfg.labels = { cfg.modules.clear();
{Module::Distro, " distro:"}, cfg.labels.clear();
{Module::Kernel, " kernel:"}, cfg.colors.clear();
{Module::Uptime, " uptime:"}, cfg.modules.reserve(moduleSpecs().size());
{Module::Shell, " shell:"}, for (const auto& spec : moduleSpecs()) {
{Module::CPU, "󰍛 CPU:"}, cfg.modules.push_back(spec.id);
{Module::GPU, "󰾲 GPU:"}, cfg.labels[spec.id] = spec.defaultLabel;
{Module::RAM, " RAM:"}, cfg.colors[spec.id] = spec.defaultColor;
{Module::OsDate, " OS Date:"}, }
};
cfg.colors = {
{Module::Distro, Color::Green},
{Module::Kernel, Color::Magenta},
{Module::Uptime, Color::Blue},
{Module::Shell, Color::Magenta},
{Module::CPU, Color::Yellow},
{Module::GPU, Color::Yellow},
{Module::RAM, Color::Red},
{Module::OsDate, Color::Blue},
};
cfg.logoEnabled = true; cfg.logoEnabled = true;
return cfg; return cfg;