aboutsummaryrefslogtreecommitdiff
path: root/local/bin/translate-prompt
blob: fb23b564d2b43116addf1364da47eda897355510 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/sh

set -e

command -v trans > /dev/null ||
	{ echo "translate-shell not installed" ; exit 1 ; }
command -v rofi > /dev/null &&
    menu_exec='rofi -dmenu' ||
    menu_exec='dmenu'

choice="$(
$menu_exec <<EOF
From Selection
From Clipboard
Open Editor
EOF
)"

# TODO: alacritty --class CustomFloating when I figure out how to center floating windows

text_file="$(mktemp /tmp/translate_prompt_text_XXXXXX)"

case "$choice" in
	'From Selection')
		xsel --output > "$text_file"
		;;
	'From Clipboard')
		xclip -out -selection clipboard > "$text_file"
		;;
	'Open Editor')
		text_file="$(mktemp /tmp/translate_prompt_text_XXXXXX)"
		"$TERMINAL" -e "$EDITOR" "$text_file"
		;;
	*)
        printf '%s' "$choice" > "$text_file"
		;;
esac

brief=-brief
if [ "$(head -c1 "$text_file")" = '#' ]
then
    brief=''
    sed -i '1 s/^#//' "$text_file"
fi

translation_file="$(mktemp /tmp/translate_prompt_translation_XXXXXX)"
trans $brief -input "$text_file" -output "$translation_file"
"$TERMINAL" -e less "$translation_file"

# -view gives one line at a time?
# "$TERMINAL" -e trans $brief -view -input "$text_file"