在 zsh 中为 gsettings 键绑定自动化分配数组值

在 zsh 中为 gsettings 键绑定自动化分配数组值

我认为这个脚本应该可以设置按键绑定,但它目前非常无聊——更新:它已经被取消了,现在可以工作了。虽然它很丑陋,但我正在寻求让它变得更优雅的方法,因为每次我想要一个新的按键绑定时,我都需要复制粘贴四行。

我声称由于 zsh 不支持二维数组,所以我可能最好用 python 重写它......更新后,python 脚本仍然存在这里

如何正确设置键绑定的问题这里

#!/bin/zsh
# How many keybinds?
N=16
# Put keybindings here
k0=("keyboard dvorak nocaps" "Menu" "setxbmap dvorak -option nocaps")
k1=("launch code" "<Super>c" "code")
k2=("dropdown firefox" "<Shift><Super>f" "tdrop -ma -h \"100%\" -w \"50%\" firefox")

mkeys="org.gnome.settings-daemon.plugins.media-keys"
_locstr="/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom"
_ck="$mkeys.custom-keybinding:$_locstr"
M="$(expr $N - 1)"

# Create spaces for $N keybindings, emulating the command:
# gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "['/org/gnome/settings-daemon/plugins/ media-keys/custom-keybindings/custom0/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ ']"
locations=""
for i in $(seq 0 $M); do
  locations+="'$_locstr$i/', "
done
locations+="'$_locstr$N/' "
gsettings set $mkeys custom-keybindings "[$locations]"


# issue commands. Have to paste 4 lines each time, kinda gross.
ck="${_ck}0/"
gsettings set $ck name    $k0[1]
gsettings set $ck binding $k0[2]
gsettings set $ck command $k0[3]
ck="${_ck}1/"
gsettings set $ck name    $k1[1]
gsettings set $ck binding $k1[2]
gsettings set $ck command $k1[3]
ck="${_ck}2/"
gsettings set $ck name    $k2[1]
gsettings set $ck binding $k2[2]
gsettings set $ck command $k2[3]

# test with:
# i=0
# ck="${_ck}$i/"
# gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings
# gsettings list-recursively $ck

答案1

一些改进途径:

#! /bin/zsh -

bindings=(
  'keyboard dvorak nocaps' Menu              'setxbmap dvorak -option nocaps'
  'launch code'            '<Super>c'        code
  'dropdown firefox'       '<Shift><Super>f' 'tdrop -ma -h 100% -w 50% firefox'
)

mkeys=org.gnome.settings-daemon.plugins.media-keys
locstr=/${mkeys//./\/}/custom-keybindings/custom
ck=$mkeys.custom-keybinding:$locstr

locations=() i=0
for name binding command ($bindings) {
  for type (name binding command) gsettings set $ck$i $type ${(P)type}
  locations+=( "'$locstr$i'" )
  (( i++ ))
}

gsettings set $mkeys custom-keybindings "[${(j[, ])locations}]"

相关内容