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

92 lines
1.9 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
set -o nounset
set -o pipefail
SCRIPTS=${SCRIPTS:-$HOME/.local/scripts}
THIS=${THIS:-$(basename ${BASH_SOURCE[0]})}
usage() {
cat <<EOF
Select from currently available windows, returning the selected window's address.
USAGE:
$ $THIS <flags> [FZF_OPTIONS]
FLAGS:
-h | --help: Show this help page.
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)')
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
################################################################################
while [[ $# -gt 0 ]]; do
if [[ $1 =~ ^--footer ]]; then
uses_supplied_footer="1"
fzf_opts+=("$1")
elif [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
usage && exit 0
else
fzf_opts+=("$1")
fi
shift
done
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/utils/fzf/preview-stats window {1}'
)
status=$?
[[ -z $sel ]] && exit 1
# revove everything but the address portion.
sel=${sel%%|*}
echo "$sel"
exit $status