Last active
September 25, 2019 14:01
-
-
Save kawas44/69747ebe07b64f3c1dc7056d5dfeb69c to your computer and use it in GitHub Desktop.
Show current keyboard layout in i3status bar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script to show current keyboard layout in i3status bar | |
# Requires https://github.com/nonpop/xkblayout-state | |
# Inspiration from https://github.com/vzaliva/scripts_and_configs/blob/master/scripts/i3status.sh | |
# set your keymap as you wish | |
# ex: setxkbmap -layout 'us,us,fr' -variant 'mac,colemak,oss' -option grp:ctrls_toggle -option caps:escape | |
i3status | while : | |
do | |
read line | |
xkb_layout=$(xkblayout-state print "%s %v" | tr -d '\n\r') | |
awk_program=' | |
/^layout:/ { | |
n = split($2, l, ",") | |
} | |
/^variant:/ { | |
split($2, v, ",") | |
} | |
END { | |
s = ""; | |
for (i = 1; i < n+1; i++) { | |
if (i==1) { | |
s = sprintf("%s %s", l[i], v[i]) | |
} else { | |
s = sprintf("%s,%s %s", s, l[i], v[i]) | |
} | |
} | |
print s | |
}' | |
IFS=',' read -r -a layouts <<< $(setxkbmap -query | awk "$awk_program") | |
lastidx=$( expr ${#layouts[@]} - 1 ) | |
res="[{ \"full_text\": \"⌨\", \"separator\":false}" | |
for index in "${!layouts[@]}" | |
do | |
v="${layouts[index]}" | |
if [ "$v" == "$xkb_layout" ] | |
then | |
c=", \"color\":\"#FFFFFF\"" | |
else | |
c=", \"color\":\"#444444\"" | |
fi | |
if [[ $index -eq $lastidx ]]; then | |
e=", \"separator_block_width\": 15" | |
else | |
e=", \"separator\":false" | |
fi | |
res="$res, { \"full_text\": \"${v}\"$c$e}" | |
done | |
echo "${line/[/$res,}" || exit 1 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment