From 4bb8e6ba9e52ec5b1168c5a92faf632e1ffbf3c7 Mon Sep 17 00:00:00 2001 From: dabruh <11458706-dabruh@users.noreply.gitlab.com> Date: Sun, 5 Jun 2022 00:05:57 +0200 Subject: [PATCH] Show previously selected languages in dmenu. --- .local/bin/dmenugtranslate | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/.local/bin/dmenugtranslate b/.local/bin/dmenugtranslate index 9e1c764..eb31b20 100755 --- a/.local/bin/dmenugtranslate +++ b/.local/bin/dmenugtranslate @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # Translate the current clipboard with the help of dmenu # @@ -8,16 +8,29 @@ text=$(xclip -o -selection clipboard) 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. -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 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 to_code="$(grep "^$to=" "$map" | cut -d '=' -f2)" +echo "$from=$from_code,$to=$to_code" >"$FILE" + translation="$(curl --silent --get \ --data-urlencode "from=$from_code" \ --data-urlencode "to=$to_code" \