refactor: sort-by enum hyprland

pull/2486/head
Austin Horstman 2023-09-09 09:32:55 -05:00
parent 65ba449460
commit 8ea2626de8
No known key found for this signature in database
3 changed files with 85 additions and 39 deletions

View File

@ -11,6 +11,7 @@
#include "AModule.hpp"
#include "bar.hpp"
#include "modules/hyprland/backend.hpp"
#include "util/enum.hpp"
namespace waybar::modules::hyprland {
@ -86,7 +87,8 @@ class Workspaces : public AModule, public EventHandler {
bool all_outputs_ = false;
bool show_special_ = false;
bool active_only_ = false;
std::string sort_by = "default";
util::EnumParser enum_parser_;
util::EnumParser::SORT_METHOD sort_by_ = util::EnumParser::SORT_METHOD::DEFAULT;
void fill_persistent_workspaces();
void create_persistent_workspaces();

View File

@ -0,0 +1,28 @@
#pragma once
#include <iostream>
#include <map>
#include <string>
namespace waybar::util {
struct EnumParser {
EnumParser() {}
enum SORT_METHOD { ID, NAME, NUMBER, DEFAULT };
SORT_METHOD sortStringToEnum(const std::string& str) {
static const std::map<std::string, SORT_METHOD> enumMap = {
{"ID", ID}, {"NAME", NAME}, {"NUMBER", NUMBER}, {"DEFAULT", DEFAULT}};
auto it = enumMap.find(str);
if (it != enumMap.end()) {
return it->second;
} else {
throw std::invalid_argument("Invalid string representation for enum");
}
}
~EnumParser() = default;
};
} // namespace waybar::util

View File

@ -59,7 +59,14 @@ auto Workspaces::parse_config(const Json::Value &config) -> void {
auto config_sort_by = config_["sort-by"];
if (config_sort_by.isString()) {
sort_by = config_sort_by.asString();
auto sort_by_str = config_sort_by.asString();
try {
sort_by_ = enum_parser_.sortStringToEnum(sort_by_str);
} 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;
g_warning("Invalid string representation for sort-by. Falling back to default sort method.");
}
}
}
@ -427,16 +434,21 @@ void Workspaces::sort_workspaces() {
auto is_name_less = a->name() < b->name();
auto is_number_less = std::stoi(a->name()) < std::stoi(b->name());
if (sort_by == "number") {
switch (sort_by_) {
case util::EnumParser::SORT_METHOD::ID:
return is_id_less;
case util::EnumParser::SORT_METHOD::NAME:
return is_name_less;
case util::EnumParser::SORT_METHOD::NUMBER:
try {
return is_number_less;
} catch (const std::invalid_argument &) {
// Handle the exception if necessary.
break;
}
} else if (sort_by == "name") {
return is_name_less;
} else if (sort_by == "id") {
return is_id_less;
} else {
case util::EnumParser::SORT_METHOD::DEFAULT:
default:
// Handle the default case here.
// normal -> named persistent -> named -> special -> named special
// both normal (includes numbered persistent) => sort by ID
@ -467,7 +479,11 @@ void Workspaces::sort_workspaces() {
// sort non-special named workspaces by name (ID <= -1377)
return is_name_less;
break;
}
// Return a default value if none of the cases match.
return is_name_less; // You can adjust this to your specific needs.
});
for (size_t i = 0; i < workspaces_.size(); ++i) {