workspace/arch-user/bin/sway/search-mode.sh
2025-09-28 22:08:05 +00:00

55 lines
1.9 KiB
Bash
Executable File

get_windows_with_workspace() {
swaymsg -t get_tree | jq -r '
def calc_ws_name(ws_name):
if ws_name == "__i3_scratch" then "_"
elif (ws_name | test("^[0-9]+:")) then (ws_name | sub("^[0-9]+:"; ""))
else ws_name
end;
def get_windows(workspace_name):
if (.type == "con" or .type == "floating_con") and .pid != null and (.name // "") != "" then
{
id: .id,
name: .name | ascii_downcase,
app_id: (.app_id // .window_properties.class // "unknown") | ascii_downcase,
workspace: workspace_name,
focused: .focused
}
else empty end,
(.nodes[]? | get_windows(workspace_name)),
(.floating_nodes[]? | get_windows(workspace_name));
def traverse_all:
if .type == "workspace" then
. as $ws | (.name | calc_ws_name(.)) as $ws_name | get_windows($ws_name)
else empty end,
(.nodes[]? | traverse_all);
traverse_all |
"\(.id)|\(.app_id)|\(.name)|\(.workspace)|\(.focused)"
'
}
window_list=$(get_windows_with_workspace)
formatted_list=$(echo "$window_list" | while IFS='|' read -r id app_id name workspace focused; do
if [ "$focused" = "true" ]; then
indicator="> "
else
indicator=" "
fi
printf "%s%s%s %s %s\n" "$workspace" "$indicator" "$id" "$app_id" "$name"
done)
selection=$(echo "$formatted_list" | bemenu --auto-select --width-factor 100 --prompt "/" --index "$(echo "$formatted_list" | awk 'substr($0,2,1)==">" {print NR-1}')" )
[ -z "$selection" ] && exit 0
window_id=$(echo "$selection" | sed -E 's/^...([0-9]+).*/\1/')
if [ -n "$window_id" ]; then
swaymsg "[con_id=\"$window_id\"] focus"
else
echo "Failed to extract window ID."
exit 1
fi