Merge branch 'vaxerski:hyprlandLanguage' into hyprlandLanguage

This commit is contained in:
NotAShelf 2022-08-18 20:10:31 +03:00 committed by GitHub
commit d786f9a0e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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