refactor: sort-by enum hyprland

This commit is contained in:
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 "AModule.hpp"
#include "bar.hpp" #include "bar.hpp"
#include "modules/hyprland/backend.hpp" #include "modules/hyprland/backend.hpp"
#include "util/enum.hpp"
namespace waybar::modules::hyprland { namespace waybar::modules::hyprland {
@ -86,7 +87,8 @@ 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;
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 fill_persistent_workspaces();
void create_persistent_workspaces(); void create_persistent_workspaces();

28
include/util/enum.hpp Normal file
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"]; auto config_sort_by = config_["sort-by"];
if (config_sort_by.isString()) { 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_name_less = a->name() < b->name();
auto is_number_less = std::stoi(a->name()) < std::stoi(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 { try {
return is_number_less; return is_number_less;
} catch (const std::invalid_argument &) { } catch (const std::invalid_argument &) {
// Handle the exception if necessary.
break;
} }
} else if (sort_by == "name") { case util::EnumParser::SORT_METHOD::DEFAULT:
return is_name_less; default:
} else if (sort_by == "id") { // Handle the default case here.
return is_id_less;
} else {
// normal -> named persistent -> named -> special -> named special // normal -> named persistent -> named -> special -> named special
// both normal (includes numbered persistent) => sort by ID // 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) // sort non-special named workspaces by name (ID <= -1377)
return is_name_less; 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) { for (size_t i = 0; i < workspaces_.size(); ++i) {