river: Add translation widget mapping

Added quick way to translate from anywhere with the Mod+Shift+T mapping.
Will open a bemenu window into which text to be translated can be input
(as well as any translation options) and will return in another bemenu
window. From there, selected text can be put on the clipboard for easy
pasting.

Internally uses a simple wrapper script for translate-shell on which the
translation is based.

Closes #12.
This commit is contained in:
Marty Oehme 2022-05-02 16:08:30 +02:00
parent 4abfc41ca2
commit a08f2daafe
Signed by: Marty
GPG key ID: B7538B8F50A1C800
3 changed files with 74 additions and 29 deletions

View file

@ -0,0 +1,41 @@
#!/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