#!/usr/bin/env sh
#
# bs - bookmark edit/open/search with bmm and fzf
# dependencies: bmm | fzf
#
# ---
# source: https://github.com/dhth/dotfiles/blob/master/utils/exe/bs

# RELOAD='reload:bmm search {q} -f plain'
RELOAD='reload:(tail -n +2 <(bmm search {q} -f delimited))'
# OPEN='open -g {+1}'
OPEN='browser {+1}'
EDIT='bmm save {1} -e'
START=''

if [ "$#" -gt 0 ]; then
  START="reload:(tail -n +2 <(bmm search ${1} -f delimited))"
fi

fzf </dev/null \
  --reverse \
  --multi \
  --delimiter ',' \
  --with-nth=2,1 \
  --bind "start:${START}" \
  --bind "enter:${RELOAD}" \
  --bind "ctrl-r:${RELOAD}" \
  --bind "alt-enter:execute:${OPEN}" \
  --bind "ctrl-o:execute:${OPEN}" \
  --bind "ctrl-e:execute:${EDIT}" \
  --bind 'H:clear-query' \
  --bind='ctrl-y:execute-silent(printf '\''%s'\'' {1} | pbcopy)+bell' \
  --bind='ctrl-y:+transform-footer(echo Copied {} to clipboard)' \
  --bind='ctrl-y:+bg-cancel+bg-transform-footer(sleep 1)' \
  --preview 'bmm show {1}' \
  --preview-window='nohidden:down:40%' \
  --style=minimal
