refactor: enum utility allow overriding

This commit is contained in:
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 all_outputs_ = false;
bool show_special_ = false; bool show_special_ = false;
bool active_only_ = 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 fill_persistent_workspaces();
void create_persistent_workspaces(); void create_persistent_workspaces();

View File

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

View File

@ -61,10 +61,10 @@ auto Workspaces::parse_config(const Json::Value &config) -> void {
if (config_sort_by.isString()) { if (config_sort_by.isString()) {
auto sort_by_str = config_sort_by.asString(); auto sort_by_str = config_sort_by.asString();
try { 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) { } catch (const std::invalid_argument &e) {
// Handle the case where the string is not a valid enum representation. // 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."); 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()); auto is_number_less = std::stoi(a->name()) < std::stoi(b->name());
switch (sort_by_) { switch (sort_by_) {
case util::EnumParser::SORT_METHOD::ID: case SORT_METHOD::ID:
return is_id_less; return is_id_less;
case util::EnumParser::SORT_METHOD::NAME: case SORT_METHOD::NAME:
return is_name_less; return is_name_less;
case util::EnumParser::SORT_METHOD::NUMBER: case SORT_METHOD::NUMBER:
try { try {
return is_number_less; return is_number_less;
} catch (const std::invalid_argument &) { } catch (const std::invalid_argument &) {
// Handle the exception if necessary. // Handle the exception if necessary.
break; break;
} }
case util::EnumParser::SORT_METHOD::DEFAULT: case SORT_METHOD::DEFAULT:
default: default:
// Handle the default case here. // Handle the default case here.
// normal -> named persistent -> named -> special -> named special // normal -> named persistent -> named -> special -> named special