🎨

fuzzelを使ってカスタムなコマンドパレットを作る(Bash or Python)

に公開

Hyprlandなど向けの話です。

fuzzelをdmenuモードとして使う

commandsでタイトルと実行コマンドをマップする。${!commands[@]}で配列のキー一覧を展開できるそう。

#!/bin/bash

declare -A commands=(
  ["reload hyprconf"]="hyprctl reload"
  ["toggle visual"]="sh ~/dotfiles/script/toggle_visual.sh"
  ["toggle slideshow of wallpapers"]='sh ~/dotfiles/script/cycle_wallpaper.sh "pse"'
  ["show status wallpapers slideshow"]="sh ~/dotfiles/script/status_of_slide.sh"
)

choice=$(printf '%s\n' "${!commands[@]}" | sort | fuzzel --dmenu --prompt="  ")

[[ -n "$choice" ]] && eval "${commands[$choice]}"

デモ

使いどころ

使いどころとしてはキーバインドに登録するほどじゃない操作を定義しておくことです。

Python version

Bashだとコマンドリストの順が保持できなかったのでPython版も

#!/usr/bin/env python3
import subprocess
import os

home = os.path.expanduser("~")
script = home + "/dotfiles/script/"

cmds = {
    "toggle slideshow of wallpapers": ["sh", script + "cycle_wallpaper.sh", "pse"],
    "toggle light-dark":              ["sh", script + "toggle_theme.sh"],
    "show status wallpapers slideshow": ["sh", script + "status_of_slide.sh"],
    "toggle visual":                  ["sh", script + "toggle_visual.sh"],
    "reload hyprconf":                ["hyprctl", "reload"],
}

res = "\n".join(cmds.keys())
cmd_res = subprocess.run(
    ["fuzzel", "--dmenu", "--prompt=  "],
    input=res, text=True, capture_output=True
)

if cmd_res.returncode != 0:
    exit(0)

choice = cmds.get(cmd_res.stdout.strip())
if choice is None:
    print("not chosen")
else:
    subprocess.run(choice)

Discussion