#!/usr/bin/env bash
# bemenu-translate

# A very simple bemenu wrapper script for translate-shell.
#
# Displays a bemenu into which user can enter their phrase
# and any options they want to pass to translate-shell.
# Subsequently opens a second prompt with the results.
# If phrase is entered and then Alt+1 instead of Enter pressed
# will show the 'brief', often only one-word single-translation
# results instead.
#
# Some useful commandline options for translate shell are e.g.:
# `de: Wort` -> to translate 'from' German
# `:es horse` -> to translate 'to' Spanish
# and both can be combined as well `de:es Pferd`.
# But of course many more possibilities exist, see
# translate-shell help.

input=$(echo " " | bemenu -p "Alt+1 for brief mode| Translate>")
exit_code="$?"

if [ -z "$input" ]; then
    exit 0
fi

case "$exit_code" in
1) exit 0 ;;
0)
    output=$(echo "$input" | xargs trans -j -no-ansi)
    ;;
10)
    output=$(echo "$input" | xargs trans -j -no-ansi -b)
    ;;
esac

chosen=$(echo "$output" | bemenu -p "Translation:" -w -l 100)

if [ -n "$chosen" ] && exist clip; then
    clip "$chosen"
fi
