#!/usr/bin/env sh
#
# desc: fzf tmux fifo with neovim

set -e

fifo="${TMPDIR:-/tmp}"/nvim-tmux-exit
mkfifo -m o+w "${fifo}" || return 1

trap 'command rm -f ${fifo}' EXIT INT TERM

# shellcheck disable=SC2312
escape() { printf "'%s'\\n" "$(printf '%s' "$1" | sed -e "s/'/'\\\\''/g")"; }

[ $# -eq 0 ] && args="" || args=$(escape "$@")
# (( $# == 0 )) && args="" || args="${(z)@}"

tmux split-window -bf zsh -c "nvim ${args}; echo $? > ${fifo}" || return 1
exit <"${fifo}"

# TODO(2025-06-14): this needs to be fixed
# exec {fd}<"${fifo}"
# exec {NVIM_FIFO}>&-
