Show previously selected languages in dmenu.

This commit is contained in:
dabruh 2022-06-05 00:05:57 +02:00
parent 40ecdfd202
commit 4bb8e6ba9e
1 changed files with 16 additions and 3 deletions

View File

@ -1,4 +1,4 @@
#!/bin/sh #!/bin/bash
# Translate the current clipboard with the help of dmenu # Translate the current clipboard with the help of dmenu
# #
@ -8,16 +8,29 @@
text=$(xclip -o -selection clipboard) text=$(xclip -o -selection clipboard)
map=~/.local/share/dabruh/gtranslate-lang-map map=~/.local/share/dabruh/gtranslate-lang-map
FILE="/tmp/dmenugtranslate"
from="$(cut -d, -f1 2>/dev/null <"$FILE")"
to="$(cut -d, -f2 2>/dev/null <"$FILE")"
[ -n "$from" ] && LAST_FROM="$from"
[ -n "$to" ] && LAST_TO="$to"
# Inject the last used value on top. Remove the auto option for "Translate to".
FROM_MAP="$(echo "$LAST_FROM" | cat - "$map" | grep -v "^$" | cut -d= -f1 | uniq)"
TO_MAP="$(echo "$LAST_TO" | cat - "$map" | grep -Ev "^(Automatic=|$)" | cut -d= -f1 | uniq)"
# Get user selection via dmenu from language map file. # Get user selection via dmenu from language map file.
from=$(cut -d '=' -f1 "$map" | dmenu -i -l 30 -p "Translate from") from=$(dmenu -i -l 30 -p "Translate from" <<<"$FROM_MAP")
[ -z "$from" ] && exit 1 [ -z "$from" ] && exit 1
from_code="$(grep "^$from=" "$map" | cut -d '=' -f2)" from_code="$(grep "^$from=" "$map" | cut -d '=' -f2)"
to=$(cut -d '=' -f1 "$map" | grep -v Automatic | dmenu -i -l 30 -p "Translate to") to=$(dmenu -i -l 30 -p "Translate to" <<<"$TO_MAP")
[ -z "$to" ] && exit 1 [ -z "$to" ] && exit 1
to_code="$(grep "^$to=" "$map" | cut -d '=' -f2)" to_code="$(grep "^$to=" "$map" | cut -d '=' -f2)"
echo "$from=$from_code,$to=$to_code" >"$FILE"
translation="$(curl --silent --get \ translation="$(curl --silent --get \
--data-urlencode "from=$from_code" \ --data-urlencode "from=$from_code" \
--data-urlencode "to=$to_code" \ --data-urlencode "to=$to_code" \