// Label math for the keyboard layout widget, kept Qt-free so it can be unit // tested under node (test/shell.d/keyboard-layout-test.sh). // xkbcli list prints YAML, and every layout and variant block pairs a brief with // the description hyprctl reports as the active keymap: // // - layout: 'us' // variant: '' // brief: 'en' // description: English (US) // // The models and option groups it also prints carry no brief of their own, and // a brief never carries past the block it was printed in, so neither reaches // the table. function layoutBriefs(text) { var briefs = {} var brief = "" String(text || "").split("\n").forEach(function (line) { if (/^\s*- /.test(line)) brief = "" var field = line.match(/^ (brief|description): (.*)$/) if (!field) return if (field[1] === "brief") { brief = field[2].replace(/^'|'$/g, "") } else if (brief) { briefs[field[2]] = brief brief = "" } }) return briefs } // The brief is a short language code rather than a country one, which keeps the // label sensible for the layouts named after a language: Esperanto reads EO and // Arabic reads AR. It is the same code GNOME shows in its own indicator. // // Layouts missing from the table fall back to the first word of the description, // which reads as ENG/POR but at least says something. // // Nearly every brief is a bare two-letter code, but a few tack a script onto it // (Burmese (Zawgyi) is my-zwg) and the custom layout's is a word, so drop the // script and cap the result at the same three characters the fallback gets. // The widget sits between fixed neighbours on the bar and has no room to grow. function shortLabel(description, briefs) { if (!description) return "" // A description like "constructor" reaches an inherited member rather than a // brief, so take the lookup only when it hands back the string it promises. var brief = (briefs || {})[description] var label = typeof brief === "string" && brief ? brief.split("-")[0] : description.split(/\s+/)[0] return label.substring(0, 3).toUpperCase() } if (typeof module !== "undefined") { module.exports = { layoutBriefs: layoutBriefs, shortLabel: shortLabel } }