#!/bin/sh
#
# for use in tmux.conf:
# bind -n DoubleClick1Pane run-shell ",tmux-popup-opener '#{mouse_word}' '#{pane_current_path}'"

set -e

if echo "$1" | grep -qE '^xhttps?\:\/\/[a-z0-9_\.\?\&\=#-]+'; then
  # neomutt separator
  url="$(echo "$1" | cut -c2-)"
  tmux popup -w90% -h90% -E -d "$2" "browser '$url'"
  # tmux popup -w90% -h90% -E -d "$2" "open '$url'"

elif echo "$1" | grep -qE '^https?\:\/\/[a-z0-9_\.\?\&\=#-]+'; then
  tmux popup -w90% -h90% -E -d "$2" "open '$1'"
  # tmux popup -w90% -h90% -E -d "$2" "browser '$1'"

elif echo "$1" | grep -qE '^E\d{4}`\.'; then
  err=$(echo "$1" | cut -c1-5)
  tmux popup -w50% -h80% -E "rustc --explain '${err}'"

elif git log --format=format:%H | grep -q "$1"; then
  tmux popup -w90% -h90% -E -d "$2" \
    "git log --format=format:%H | grep '$1' | xargs git show; read"
else
  printf '%s' "$1" | yank
  notify "Copied to clipboard" "$1"
fi
