Files
dotfiles/env/.local/scripts/hypr/window-picker

85 lines
1.7 KiB
Bash
Executable File

#!/usr/bin/env bash
THIS=$(basename ${BASH_SOURCE[0]})
usage() {
cat <<EOF
Select from currently available windows.
USAGE:
$ $THIS [OPTIONS] [ARGS...]
NOTES:
By default, we show a footer and header unless specifically passed in as extra arguments / options.
Any other options or arguments are passed directly to 'fzf'.
EOF
}
uses_supplied_footer="0"
fzf_opts=()
rows=()
window_data=$(hyprctl clients -j | jq 'sort_by(.workspace.id)')
SCRIPTS=${SCRIPTS:-$HOME/.local/scripts}
while [[ $# -gt 0 ]]; do
if [[ $1 =~ ^--footer ]]; then
uses_supplied_footer="1"
fzf_opts+=("$1")
else
fzf_opts+=("$1")
fi
shift
done
footer() {
cat <<'EOF'
_ ___ __
| | /| / (_)__ ___/ /__ _ _____
| |/ |/ / / _ \/ _ / _ \ |/|/ (_-<
|__/|__/_/_//_/\_,_/\___/__,__/___/
EOF
}
generate_rows() {
readarray -t addresses <<<"$(echo "$window_data" | jq -r '.[] | .address')"
readarray -t titles <<<$(echo "$window_data" | jq -r '.[] | .title')
# Zip into rows.
for i in "${!addresses[@]}"; do
rows+=("${addresses[i]}|${titles[i]}")
done
}
################################################################################
# MAIN
################################################################################
[[ -z ${FZF_DEFAULT_OPTS} ]] &&
[[ -f $SCRIPTS/catppuccin-colors ]] &&
source $SCRIPTS/catppuccin-colors
if [[ $uses_supplied_footer == "0" ]]; then
fzf_opts+=("--footer=$(footer)")
fi
generate_rows
sel=$(
printf '%s\n' "${rows[@]}" |
fzf --style=full "${fzf_opts[@]}" \
--preview-label='[ Window Stats ]' --delimiter='|' --with-nth=2 \
--preview="$SCRIPTS/hypr/preview-stats window {1}"
)
[[ -z $sel ]] && exit 1
# revove everything but the address portion.
sel=${sel%%|*}
echo "$sel"