Skip to content

Instantly share code, notes, and snippets.

@kawas44
Last active September 25, 2019 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kawas44/69747ebe07b64f3c1dc7056d5dfeb69c to your computer and use it in GitHub Desktop.
Save kawas44/69747ebe07b64f3c1dc7056d5dfeb69c to your computer and use it in GitHub Desktop.
Show current keyboard layout in i3status bar
#!/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