Merge branch 'hyprlandLanguage' of https://github.com/vaxerski/Waybar into hyprlandLanguage

This commit is contained in:
NotAShelf 2022-08-18 20:09:34 +03:00
commit c5910ae19a
No known key found for this signature in database
GPG Key ID: B96DF05812633566
2 changed files with 39 additions and 1 deletions

View File

@ -18,6 +18,7 @@ private:
void onEvent(const std::string&); void onEvent(const std::string&);
void initLanguage(); void initLanguage();
std::string getShortFrom(const std::string&);
std::mutex mutex_; std::mutex mutex_;
const Bar& bar_; const Bar& bar_;

View File

@ -1,6 +1,8 @@
#include "modules/hyprland/language.hpp" #include "modules/hyprland/language.hpp"
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <xkbcommon/xkbcommon.h>
#include <xkbcommon/xkbregistry.h>
#include "modules/hyprland/backend.hpp" #include "modules/hyprland/backend.hpp"
@ -30,7 +32,7 @@ auto Language::update() -> void {
if (!format_.empty()) { if (!format_.empty()) {
label_.show(); label_.show();
label_.set_markup(fmt::format(format_, layoutName_)); label_.set_markup(layoutName_);
} else { } else {
label_.hide(); label_.hide();
} }
@ -57,6 +59,15 @@ void Language::onEvent(const std::string& ev) {
return str; return str;
}; };
const auto BRIEFNAME = getShortFrom(layoutName);
if (config_.isMember("format-" + BRIEFNAME)) {
const auto PROPNAME = "format-" + BRIEFNAME;
layoutName = fmt::format(format_, config_[PROPNAME].asString());
} else {
layoutName = fmt::format(format_, layoutName);
}
layoutName = replaceAll(layoutName, "&", "&amp;"); layoutName = replaceAll(layoutName, "&", "&amp;");
if (layoutName == layoutName_) return; if (layoutName == layoutName_) return;
@ -92,4 +103,30 @@ void Language::initLanguage() {
} }
} }
std::string Language::getShortFrom(const std::string& fullName) {
const auto CONTEXT = rxkb_context_new(RXKB_CONTEXT_LOAD_EXOTIC_RULES);
rxkb_context_parse_default_ruleset(CONTEXT);
std::string foundName = "";
rxkb_layout* layout = rxkb_layout_first(CONTEXT);
while (layout) {
std::string nameOfLayout = rxkb_layout_get_description(layout);
if (nameOfLayout != fullName) {
layout = rxkb_layout_next(layout);
continue;
}
std::string briefName = rxkb_layout_get_brief(layout);
rxkb_context_unref(CONTEXT);
return briefName;
}
rxkb_context_unref(CONTEXT);
return "";
}
} // namespace waybar::modules::hyprland } // namespace waybar::modules::hyprland