feat: Adds monitor-picker, utility script for generating fzf preview data for pickers.

This commit is contained in:
2025-10-05 21:17:04 -04:00
parent 90c97263cb
commit a7e6bc893c
6 changed files with 165 additions and 12 deletions

View File

@@ -111,7 +111,7 @@ logging() {
# Also log errors and warning messages to the console.
if [[ $error_flag == "1" ]] || [[ $warn_flag == "1" ]]; then
echo -e "$msg"
echo -e "[id: $LOG_INVOCATION_ID]: $msg"
fi
else
# Dry run mode, so just log to the console

101
env/.local/scripts/hypr/monitor-picker vendored Executable file
View File

@@ -0,0 +1,101 @@
#!/usr/bin/env bash
SCRIPTS=${SCRIPTS:-$HOME/.local/scripts}
THIS_FILE=${BASH_SOURCE[0]}
THIS=$(basename $THIS_FILE)
usage() {
cat <<EOF
Displays a monitor picker and stats preview. This script will accept any fzf options that you'd
like to pass. The id of the monitor is returned from this script upon selection.
USAGE:
$ $THIS [OPTIONS] [FZF_OPTIONS...]
OPTIONS:
--no-default-footer: Disable the 'Monitors' footer (supplying your own footer disables default as well).
--no-preview: Disables the monitor stats preview window.
-h | --help: Show this help page.
EOF
}
uses_supplied_footer="0"
no_preview_flag="0"
fzf_opts=("--style=full" "--delimiter=|" "--with-nth=2" "--preview-label=[ Monitor Stats ]")
rows=()
monitor_data=$(hyprctl monitors -j | jq 'sort_by(.id)')
while [[ $# -gt 0 ]]; do
if [[ $1 =~ ^--footer ]]; then
uses_supplied_footer="1"
fzf_opts+=("$1")
elif [[ $1 == "--no-preview" ]]; then
no_preview_flag="1"
elif [[ $1 == "--no-default-footer" ]]; then
uses_supplied_footer="1"
elif [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
usage && exit 0
else
fzf_opts+=("$1")
fi
shift
done
# Logging utility function, use in place of echo.
log() {
logging log --source "$THIS_FILE" "$@"
}
footer() {
cat <<'EOF'
__ ___ _ __
/ |/ /__ ___ (_) /____ _______
/ /|_/ / _ \/ _ \/ / __/ _ \/ __(_-<
/_/ /_/\___/_//_/_/\__/\___/_/ /___/
EOF
}
generate_rows() {
readarray -t ids <<<"$(echo "$monitor_data" | jq -r '.[] | .id')"
readarray -t names <<<"$(echo "$monitor_data" | jq -r '.[] | .name')"
# Zip into rows.
for i in "${!ids[@]}"; do
rows+=("${ids[i]}|${names[i]}")
done
}
################################################################################
# MAIN
################################################################################
# Setup logging file and label.
source "$SCRIPTS/hypr/logging"
setup-logging "$THIS"
[[ -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=""
# For some reason can't get the preview to work when setting in the fzf_opts array.
if [[ $no_preview_flag == "0" ]]; then
sel=$(printf '%s\n' "${rows[@]}" | fzf "${fzf_opts[@]}" --preview="$SCRIPTS/hypr/preview-stats monitor {1}")
else
sel=$(printf '%s\n' "${rows[@]}" | fzf "${fzf_opts[@]}")
fi
# revove everything but the id portion.
sel=${sel%%|*}
echo "$sel"

56
env/.local/scripts/hypr/preview-stats vendored Executable file
View File

@@ -0,0 +1,56 @@
#!/usr/bin/env bash
SCRIPTS=${SCRIPTS:-$HOME/.local/scripts}
THIS_FILE=${BASH_SOURCE[0]}
THIS=$(basename $THIS_FILE)
usage() {
cat <<EOF
Utility for getting fzf preview data.
USAGE:
$ $THIS <mode> <arg>
MODES:
monitor: Get monitor data, arg is the monitor id.
window: Get window data, arg is the window address.
workspace: Get workspace data, arg is the workspace id.
EOF
}
# Logging utility function, use in place of echo.
log() {
logging log --source "$THIS_FILE" "$@"
}
################################################################################
# MAIN
################################################################################
# Setup logging file and label.
source "$SCRIPTS/hypr/logging"
setup-logging "$THIS"
if [[ ! "${#@}" == "2" ]]; then
log --error "Unexpected argument count: ${#@}, expected: 2"
usage && exit 1
fi
mode="$1"
# Remove single quotes from the arg.
arg="${2//\'/}"
if [[ $mode == "monitor" ]]; then
hyprctl monitors -j | jq -C ".[] | select(.id == $arg)"
elif [[ $mode == "window" ]]; then
hyprctl clients -j | jq -C ".[] | select(.address == \"$arg\")"
elif [[ $mode == "workspace" ]]; then
hyprctl workspaces -j | jq -C ".[] | select(.id == $arg)"
else
log --error "Unexpected mode: $mode"
usage && exit 1
fi

View File

@@ -48,13 +48,11 @@ EOF
generate_rows() {
readarray -t addresses <<<"$(echo "$window_data" | jq -r '.[] | .address')"
readarray -t classes <<<$(echo "$window_data" | jq -r '.[] | .class')
readarray -t titles <<<$(echo "$window_data" | jq -r '.[] | .title')
readarray -t workspaces <<<$(echo "$window_data" | jq -r '.[] | .workspace.name')
# Zip into rows.
for i in "${!addresses[@]}"; do
rows+=("${addresses[i]}|${workspaces[i]}|${classes[i]}|${titles[i]}")
rows+=("${addresses[i]}|${titles[i]}")
done
}
@@ -75,8 +73,8 @@ generate_rows
sel=$(
printf '%s\n' "${rows[@]}" |
fzf --style=full "${fzf_opts[@]}" \
--preview-label='[ Window Stats ]' --delimiter='|' --with-nth=4 \
--preview='echo -e "Title: {4}\nClass: {3}\nWorkspace: {2}\nAddress: {1}"'
--preview-label='[ Window Stats ]' --delimiter='|' --with-nth=2 \
--preview="$SCRIPTS/hypr/preview-stats window {1}"
)
[[ -z $sel ]] && exit 1

View File

@@ -69,11 +69,9 @@ EOF
generate_rows() {
readarray -t names <<<"$(echo "$workspaces" | jq -r '.[] | .name')"
readarray -t ids <<<"$(echo "$workspaces" | jq -r '.[] | .id')"
readarray -t monitors <<<"$(echo "$workspaces" | jq -r '.[] | .monitor')"
readarray -t windows <<<"$(echo "$workspaces" | jq -r '.[] | .windows')"
for i in "${!names[@]}"; do
rows+=("${ids[i]}|${monitors[i]}|${windows[i]}|${names[i]}")
rows+=("${ids[i]}|${names[i]}")
done
}
@@ -93,9 +91,9 @@ fi
sel=$(
printf "%s\n" "${rows[@]}" |
fzf --style=full "${fzf_opts[@]}" --delimiter='|' --with-nth=4 \
fzf --style=full "${fzf_opts[@]}" --delimiter='|' --with-nth=2 \
--preview-label='[ Workspace Stats ]' \
--preview='printf "Name: {4}\nID: {1}\nWindows: {3}\nMonitor: {2}"'
--preview="$SCRIPTS/hypr/preview-stats workspace {1}"
)
[[ -z $sel ]] && exit 1