Merge pull request #5216 from LukashonakV/enum-refactor
util/enum: make header-only and drop mandatory Hyprland dependency
This commit is contained in:
+2
-3
@@ -296,13 +296,12 @@ waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config)
|
||||
}
|
||||
#endif
|
||||
|
||||
waybar::util::EnumParser<util::KillSignalAction> m_signalActionEnumParser;
|
||||
const auto& configSigusr1 = config["on-sigusr1"];
|
||||
if (configSigusr1.isString()) {
|
||||
auto strSigusr1 = configSigusr1.asString();
|
||||
try {
|
||||
onSigusr1 =
|
||||
m_signalActionEnumParser.parseStringToEnum(strSigusr1, util::userKillSignalActions);
|
||||
util::parseStringToEnum<util::KillSignalAction>(strSigusr1, util::userKillSignalActions);
|
||||
} catch (const std::invalid_argument& e) {
|
||||
onSigusr1 = util::SIGNALACTION_DEFAULT_SIGUSR1;
|
||||
spdlog::warn(
|
||||
@@ -314,7 +313,7 @@ waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config)
|
||||
auto strSigusr2 = configSigusr2.asString();
|
||||
try {
|
||||
onSigusr2 =
|
||||
m_signalActionEnumParser.parseStringToEnum(strSigusr2, util::userKillSignalActions);
|
||||
util::parseStringToEnum<util::KillSignalAction>(strSigusr2, util::userKillSignalActions);
|
||||
} catch (const std::invalid_argument& e) {
|
||||
onSigusr2 = util::SIGNALACTION_DEFAULT_SIGUSR2;
|
||||
spdlog::warn(
|
||||
|
||||
@@ -682,7 +682,7 @@ auto Workspaces::populateSortByConfig(const Json::Value& config) -> void {
|
||||
if (configSortBy.isString()) {
|
||||
auto sortByStr = configSortBy.asString();
|
||||
try {
|
||||
m_sortBy = m_enumParser.parseStringToEnum(sortByStr, m_sortMap);
|
||||
m_sortBy = waybar::util::parseStringToEnum<SortMethod>(sortByStr, m_sortMap);
|
||||
} catch (const std::invalid_argument& e) {
|
||||
m_sortBy = SortMethod::DEFAULT;
|
||||
spdlog::warn(
|
||||
@@ -806,7 +806,7 @@ auto Workspaces::populateWorkspaceTaskbarConfig(const Json::Value& config) -> vo
|
||||
auto posStr = workspaceTaskbar["active-window-position"].asString();
|
||||
try {
|
||||
m_activeWindowPosition =
|
||||
m_activeWindowEnumParser.parseStringToEnum(posStr, m_activeWindowPositionMap);
|
||||
util::parseStringToEnum<ActiveWindowPosition>(posStr, m_activeWindowPositionMap);
|
||||
} catch (const std::invalid_argument& e) {
|
||||
spdlog::warn(
|
||||
"Invalid string representation for active-window-position. Falling back to 'none'.");
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
#include "util/enum.hpp"
|
||||
|
||||
#include <algorithm> // for std::transform
|
||||
#include <cctype> // for std::toupper
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
#include "modules/hyprland/workspaces.hpp"
|
||||
#include "util/string.hpp"
|
||||
|
||||
namespace waybar::util {
|
||||
|
||||
template <typename EnumType>
|
||||
EnumParser<EnumType>::EnumParser() = default;
|
||||
|
||||
template <typename EnumType>
|
||||
EnumParser<EnumType>::~EnumParser() = default;
|
||||
|
||||
template <typename EnumType>
|
||||
EnumType EnumParser<EnumType>::parseStringToEnum(const std::string& str,
|
||||
const std::map<std::string, EnumType>& enumMap) {
|
||||
// Convert the input string to uppercase
|
||||
std::string uppercaseStr = capitalize(str);
|
||||
|
||||
// Capitalize the map keys before searching
|
||||
std::map<std::string, EnumType> capitalizedEnumMap;
|
||||
std::transform(
|
||||
enumMap.begin(), enumMap.end(), std::inserter(capitalizedEnumMap, capitalizedEnumMap.end()),
|
||||
[](const auto& pair) { return std::make_pair(capitalize(pair.first), pair.second); });
|
||||
|
||||
// Return enum match of string
|
||||
auto it = capitalizedEnumMap.find(uppercaseStr);
|
||||
if (it != capitalizedEnumMap.end()) return it->second;
|
||||
|
||||
// Throw error if it doesn't return
|
||||
throw std::invalid_argument("Invalid string representation for enum");
|
||||
}
|
||||
|
||||
// Explicit instantiations for specific EnumType types you intend to use
|
||||
// Add explicit instantiations for all relevant EnumType types
|
||||
template struct EnumParser<modules::hyprland::Workspaces::SortMethod>;
|
||||
template struct EnumParser<modules::hyprland::Workspaces::ActiveWindowPosition>;
|
||||
template struct EnumParser<util::KillSignalAction>;
|
||||
|
||||
} // namespace waybar::util
|
||||
Reference in New Issue
Block a user