refactor: enum utility allow overriding

pull/2486/head
Austin Horstman 2023-09-09 11:18:12 -05:00
parent 8ce64ea784
commit 2b8c92e8fd
No known key found for this signature in database
3 changed files with 21 additions and 16 deletions

View File

@ -87,8 +87,14 @@ class Workspaces : public AModule, public EventHandler {
bool all_outputs_ = false;
bool show_special_ = false;
bool active_only_ = false;
util::EnumParser enum_parser_;
util::EnumParser::SORT_METHOD sort_by_ = util::EnumParser::SORT_METHOD::DEFAULT;
enum SORT_METHOD { ID, NAME, NUMBER, DEFAULT };
util::EnumParser<SORT_METHOD> enum_parser_;
SORT_METHOD sort_by_ = SORT_METHOD::DEFAULT;
std::map<std::string, SORT_METHOD> sort_map_ = {{"ID", SORT_METHOD::ID},
{"NAME", SORT_METHOD::NAME},
{"NUMBER", SORT_METHOD::NUMBER},
{"DEFAULT", SORT_METHOD::DEFAULT}};
void fill_persistent_workspaces();
void create_persistent_workspaces();

View File

@ -1,26 +1,25 @@
#pragma once
#include <cctype>
#include <iostream>
#include <map>
#include <stdexcept>
#include <string>
namespace waybar::util {
template <typename EnumType>
struct EnumParser {
EnumParser() {}
enum SORT_METHOD { ID, NAME, NUMBER, DEFAULT };
SORT_METHOD sortStringToEnum(const std::string& str) {
// Convert the input string to uppercase (make it lenient on config input)
EnumType sortStringToEnum(const std::string& str,
const std::map<std::string, EnumType>& enumMap) {
// Convert the input string to uppercase
std::string uppercaseStr;
for (char c : str) {
uppercaseStr += std::toupper(c);
}
static const std::map<std::string, SORT_METHOD> enumMap = {
{"ID", ID}, {"NAME", NAME}, {"NUMBER", NUMBER}, {"DEFAULT", DEFAULT}};
auto it = enumMap.find(uppercaseStr);
if (it != enumMap.end()) {
return it->second;

View File

@ -61,10 +61,10 @@ auto Workspaces::parse_config(const Json::Value &config) -> void {
if (config_sort_by.isString()) {
auto sort_by_str = config_sort_by.asString();
try {
sort_by_ = enum_parser_.sortStringToEnum(sort_by_str);
sort_by_ = enum_parser_.sortStringToEnum(sort_by_str, sort_map_);
} catch (const std::invalid_argument &e) {
// Handle the case where the string is not a valid enum representation.
sort_by_ = util::EnumParser::SORT_METHOD::DEFAULT;
sort_by_ = SORT_METHOD::DEFAULT;
g_warning("Invalid string representation for sort-by. Falling back to default sort method.");
}
}
@ -435,18 +435,18 @@ void Workspaces::sort_workspaces() {
auto is_number_less = std::stoi(a->name()) < std::stoi(b->name());
switch (sort_by_) {
case util::EnumParser::SORT_METHOD::ID:
case SORT_METHOD::ID:
return is_id_less;
case util::EnumParser::SORT_METHOD::NAME:
case SORT_METHOD::NAME:
return is_name_less;
case util::EnumParser::SORT_METHOD::NUMBER:
case SORT_METHOD::NUMBER:
try {
return is_number_less;
} catch (const std::invalid_argument &) {
// Handle the exception if necessary.
break;
}
case util::EnumParser::SORT_METHOD::DEFAULT:
case SORT_METHOD::DEFAULT:
default:
// Handle the default case here.
// normal -> named persistent -> named -> special -> named special