#!/bin/bash
echo "Script is running" >> /tmp/tmux_search_debug.log

selection=$(fd -t f -t d . | fzf --preview "if [ -d {} ]; then tree {}; else bat --color=always {}; fi" --preview-window down:50%:wrap --height=80% --border --prompt="Search: " --no-reverse --ansi --bind="q:execute(bash -c \"exit\")")

if [ -n "$selection" ]; then
  if [ -d "$selection" ]; then
    tmux send-keys -t "#{pane_id}" "cd '$selection' && clear" Enter
  else
    tmux send-keys -t "#{pane_id}" "cd '\$(dirname \"$selection\")' && \$EDITOR \"$selection\" && clear" Enter
  fi
fi

