#!/usr/bin/env fish

set windows

# while read i; do
#   declare -a line_array="($i)"
#   window_ids+=(${line_array[0]})
#   window_titles+=("${line_array[1]} - ${line_array[2]} \0icon\x1f${line_array[1]}")
# done <<<$(niri msg --json windows | jq -r '.[] | [ .id, .app_id, .title] | "\""+join ("\" \"")+"\""')
#
set windows (niri msg --json windows | jq -r '.[] | [ .id, .app_id, .title] | join(" ")')

set ids (string split --max 2 --field 1 " " $windows)

for window in $windows
    set -a options (string join -- ' ' (string split --max 1 --fields 2 ' ' $window) "\0icon\x1f$(string split --max 2 --field 2 ' ' $window)" )
end

set input (menu --index $options)

niri msg action focus-window --id $ids[(math "$input+1")]
