From e732866368a9e0b0bfa088d10ccc6a84d0cc1e24 Mon Sep 17 00:00:00 2001 From: scbj Date: Fri, 23 Jan 2026 12:17:33 +0100 Subject: [PATCH] (scripts/i3) keyboard layout layout switcher for i3 mostly working --- bashrc/.bashrc | 1 - i3/.config/i3/config | 3 ++ scripts/i3-kb-layout-switcher.sh | 68 +++++++++++++++++--------------- 3 files changed, 40 insertions(+), 32 deletions(-) mode change 100644 => 100755 scripts/i3-kb-layout-switcher.sh diff --git a/bashrc/.bashrc b/bashrc/.bashrc index 6760a61..2ffadc8 100644 --- a/bashrc/.bashrc +++ b/bashrc/.bashrc @@ -106,7 +106,6 @@ source_list+=("${personal_scripts}/navigate_bash_setup.sh") source_list+=("${personal_scripts}/fzf-bash-history.sh") source_list+=("${personal_scripts}/colors.sh") source_list+=("${personal_scripts}/dunst.sh") -source_list+=("${personal_scripts}/i3-kb-layout-switcher.sh") source_list+=("$HOME/.cargo/env") for script in ${source_list[@]}; do diff --git a/i3/.config/i3/config b/i3/.config/i3/config index 478cef0..aabe98b 100644 --- a/i3/.config/i3/config +++ b/i3/.config/i3/config @@ -77,6 +77,9 @@ bindsym $mod+Shift+v exec pkill picom bindsym $mod+d exec --no-startup-id rofi -show combi bindsym $mod+Shift+d exec --no-startup-id rofi -show run +# start keyboard layout selector +bindsym $mod+m exec --no-startup-id i3-kb-layout-switcher.sh + # start a terminal bindsym $mod+Return exec ghostty bindsym $mod+t exec ghostty -e tmux attach diff --git a/scripts/i3-kb-layout-switcher.sh b/scripts/i3-kb-layout-switcher.sh old mode 100644 new mode 100755 index 9e00a36..3d90f3b --- a/scripts/i3-kb-layout-switcher.sh +++ b/scripts/i3-kb-layout-switcher.sh @@ -1,49 +1,55 @@ #!/usr/bin/env bash function switch_kb_layout { - layouts=("us", "us") - variants=("default, intl") - languages="$(echo "${devices}" | grep -ioP '(?<="layout": ").*(?=")' -m 1)" - variants="$(echo "${devices}" | grep -ioP '(?<="variant": ").*(?=")' -m 1)" + layouts=("us" "us" "ch") + variants=("" "intl" "") + lines=${#layouts[@]} + number=0 + selection=-1 - ## TODO: use `setxkbmap -print` to get status - current="$(echo "${devices}" | grep -ioP '(?<="active_layout_index": ).*(?=,)' -m 1)" + if [[ ${#layouts[@]} -ne ${#variants[@]} ]]; then + notify-send "layout-switcher" "#layouts != #variants" + return 1 + fi - languages=(${languages}) - variants=(${variants}) - - number=$((${#languages[@]} - 1)) - for n in $(seq 0 $number); do - layouts+=("${languages[$n]//,/} - ${variants[$n]//,/}") + unset names + for n in ${!layouts[@]}; do + if [[ -n "${variants[$n]}" ]]; then + names+=("${layouts[$n]}:${variants[$n]}") + else + names+=("${layouts[$n]}") + fi done + # ## TODO: use `setxkbmap -print` to get status + # current="$(echo "${devices}" | grep -ioP '(?<="active_layout_index": ).*(?=,)' -m 1)" + if $(which rofi &>/dev/null); then - # selection="$(printf "%s\n" "${layouts[@]}" | fuzzel --lines ${#layouts[@]} --dmenu --select-index ${current})" - return 1 + selection="$(printf "%s\n" "${names[@]}" | rofi -dmenu -format 'i' -fixed-num-lines ${lines} -selected-row ${number} -case-smart)" else notify-send "layout-switcher" "requires [rofi]" return 1 fi - for n in $(seq 0 $number); do - if [[ "${layouts[$n]}" == "${selection}" ]]; then - index=$n - break - fi - done - if [[ -z "${index}" ]]; then + if [[ -z "$selection" ]]; then + return 0 + fi + + if [[ $selection -lt 0 || $selection -ge $lines ]]; then + notify-send "layout-switcher" "invalid selection" return 1 fi - hyprctl switchxkblayout all "${index}" >/dev/null + # if [[ $selection -ge $lines ]]; then + # notify-send "layout-switcher" "invalid selection" + # return 1 + # fi + # + if [[ -n "${variants[$selection]}" ]]; then + setxkbmap -layout "${layouts[$selection]}" -variant "${variants[$selection]}" + else + setxkbmap -layout "${layouts[$selection]}" + fi } -# switch_kb_layout - -function choose_layout_us { - setxkbmap -layout us -} - -function choose_layout_int { - setxkbmap -layout us -variant intl -} +switch_kb_layout