Files
dotfiles/env/.local/scripts/hypr/utils/workspaces/workspace-picker

131 lines
3.3 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
set -o nounset
set -o pipefail
SCRIPTS=${SCRIPTS:-$HOME/.local/scripts}
THIS_FILE=${BASH_SOURCE[0]}
LOG_LABEL=$(basename "$THIS_FILE")
THIS=${THIS:-$LOG_LABEL}
LOG_FILE=${LOG_FILE:-"/tmp/$LOG_LABEL.log"}
FZF_DEFAULT_OPTS=${FZF_DEFAULT_OPTS:-""}
usage() {
cat <<EOF
Select from currently available workspaces. If no options are specified, then the default behavior is
to return the selected workspace id.
USAGE:
$ $THIS <flags> [FZF_OPTIONS]
FLAGS:
-n | --return-name: Return workspace name instead of id.
-s | --return-name-if-special: Return name if a special workspace, otherwise return the id.
This option is useful when moving a workspace using 'hyprctl' with
the selection.
-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
}
# Logging utility function, use in place of echo.
log() {
logging log --source "$THIS_FILE" "$@"
}
footer() {
cat <<'EOF'
_ __ __
| | /| / /__ ____/ /__ ___ ___ ___ ________ ___
| |/ |/ / _ \/ __/ '_/(_-</ _ \/ _ `/ __/ -_|_-<
|__/|__/\___/_/ /_/\_\/___/ .__/\_,_/\__/\__/___/
/_/
EOF
}
prompt_for_selection() {
local rows=()
local fzf_opts=$1
local sel=""
local workspaces=$(hyprctl workspaces -j | jq 'sort_by(.id)')
readarray -t names <<<"$(echo "$workspaces" | jq -r '.[] | .name')"
readarray -t ids <<<"$(echo "$workspaces" | jq -r '.[] | .id')"
for i in "${!names[@]}"; do
rows+=("${ids[i]}|${names[i]}")
done
log "Showing workspace picker / stats..."
sel=$(
printf "%s\n" "${rows[@]}" |
fzf --style=full "${fzf_opts[@]}" --delimiter='|' --with-nth=2 \
--preview-label='[ Workspace Stats ]' \
--preview='$SCRIPTS/hypr/utils/fzf/preview-stats workspace {1}'
)
echo "$sel"
}
################################################################################
# MAIN
################################################################################
# Setup logging file and label.
source "$SCRIPTS/hypr/logging"
setup-logging "$LOG_FILE" "$LOG_LABEL"
return_name_if_special_flag="0"
return_name_flag="0"
uses_supplied_footer="0"
fzf_opts=()
while [[ $# -gt 0 ]]; do
if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
usage && exit 0
elif [[ $1 == "-n" ]] || [[ $1 == "--return-name" ]]; then
return_name_flag="1"
elif [[ $1 == "-s" ]] || [[ $1 == "--return-name-if-special" ]]; then
return_name_if_special_flag="1"
elif [[ $1 =~ ^--footer ]]; then
uses_supplied_footer="1"
fzf_opts+=("$1")
else
fzf_opts+=("$1")
fi
shift
done
# Load fzf color scheme if it's not been setup yet.
[[ -z ${FZF_DEFAULT_OPTS} ]] &&
[[ -f $SCRIPTS/catppuccin-colors ]] &&
source $SCRIPTS/catppuccin-colors
if [[ $uses_supplied_footer == "0" ]]; then
fzf_opts+=("--footer=$(footer)")
fi
sel=$(prompt_for_selection "${fzf_opts[@]}")
name=${sel##*\|}
id=${sel%%\|*}
if [[ $return_name_flag == "1" ]] ||
([[ $return_name_if_special_flag == "1" ]] && [[ $name =~ ^special ]]); then
echo "$name" && exit 0
fi
echo "$id"