Even though the same functionality can be implemented with rofi and the window mode, I wanted to write my own version with the following additions:
- Displays the Niri workspace on which the window is open
- The currently focused window is prefixed with an asterisk
- If no other window is open, an alert is displayed and the script exits
- The rofi prompt displays the total number of open windows
#!/bin/bash
WINS="$(niri msg -j windows | jq "[.[] | select(.is_urgent == false|true )] | length")"
R_PROMPT="Window List ($WINS open) "
R_CONFIG="/home/karmst/.config/rofi/themes/fzf_finder.rasi"
R_LINES=12
if [[ $WINS -lt 2 ]]; then
dunstify "There are no other windows to display" -t 2000
exit 0
fi
options=()
for ((index=0; index<WINS; index++)); do
ID=$(niri msg -j windows | jq ".[$index]" | jq ".id")
TITLE=$(niri msg -j windows | jq ".[$index]" | jq ".title")
WS=$(niri msg -j windows | jq ".[$index]" | jq ".workspace_id")
IS_FOCUSED=$(niri msg -j windows | jq ".[$index]" | jq ".is_focused")
FOCUSED=" "
if [[ $IS_FOCUSED == true ]]; then
FOCUSED="* "
fi
CONCAT="$FOCUSED[workspace $WS] - $TITLE:$ID"
options+=("$CONCAT")
done
get_options() {
for opt in "${options[@]}"; do
echo "${opt%%:*}"
done
}
selection=$(get_options | rofi -dmenu -i -l "$R_LINES" -p "$R_PROMPT" -config "$R_CONFIG" -theme-str 'window {width: 80%;} listview { scrollbar: false; }' )
if [[ -z "$selection" ]]; then
exit 0
fi
action=""
for opt in "${options[@]}"; do
display_name="${opt%%:*}"
if [[ "$selection" == "$display_name" ]]; then
action="${opt##*:}"
break
fi
done
if [[ -n "$action" ]]; then
cmd=$(niri msg action focus-window --id "$action")
fi
exit 0