# https://xplr.dev/
# The reason to use xplr instead of fzf here is sometimes we want to edit a file
# not in the current working directory.

define-command xplr-open -params 1.. %{
    evaluate-commands %sh{
        for xplr_file in $@; do
            if [ -f "$xplr_file" ]; then
                printf "%s\n" "edit $xplr_file"
            fi
        done
    }
} -hidden

define-command xplr -params ..1 -docstring "Use xplr to open file" %{
    wezterm-terminal-tab sh -c %{
        PATH=$PATH:~/.fzf/bin/
        kak_buffile=$1 kak_session=$2 kak_client=$3
        shift 3
        kak_pwd="${@:-$(dirname "${kak_buffile}")}"
        output=$(xplr "${kak_pwd}")
        kak_cmd="evaluate-commands -client $kak_client xplr-open $output"
        echo $kak_cmd | kak -p $kak_session
    } -- %val{buffile} %val{session} %val{client} %arg{@}
}

# Utility command to edit a kak config file with xplr
define-command edit-config %{
    xplr %val{config}
}
