diff options
| author | filip <“filip.rabiega@gmail.com”> | 2026-01-18 11:44:26 +0100 |
|---|---|---|
| committer | filip <“filip.rabiega@gmail.com”> | 2026-01-18 11:44:26 +0100 |
| commit | 83954961af8b6337da100e038561f2fd975ac1fe (patch) | |
| tree | 33a45dbf91cc4c0b8975287b9537db8aa272505f /dtrans | |
| parent | afd02bfcce54e3252d356f34c25a155c855f3612 (diff) | |
| download | chadscripts-83954961af8b6337da100e038561f2fd975ac1fe.tar.gz chadscripts-83954961af8b6337da100e038561f2fd975ac1fe.tar.bz2 chadscripts-83954961af8b6337da100e038561f2fd975ac1fe.zip | |
Diffstat (limited to 'dtrans')
| -rwxr-xr-x | dtrans | 146 |
1 files changed, 73 insertions, 73 deletions
@@ -1,16 +1,16 @@ #!/bin/sh echo_err() { - notify-send -u critical "$0 error" "$1" + notify-send -u critical "$0 error" "$1" } err() { - echo_err "$1" - exit 1 + echo_err "$1" + exit 1 } check_dep() { - command -v "$1" > /dev/null || err "$1 is required: $2" + command -v "$1" > /dev/null || err "$1 is required: $2" } # We don't check if dmenu is installed because someone @@ -19,96 +19,96 @@ check_dep trans https://github.com/soimort/translate-shell check_dep notify-send load_config() { - config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/dmenu-translate" - [ -d "$config_dir" ] || mkdir -p "$config_dir" - - config_file="$config_dir/config.sh" - [ -f "$config_file" ] || touch "$config_file" - - old_config_file="$config_dir/config.conf" - [ -f "$old_config_file" ] && notify-send "dmenu-translate warning" "Found old configuration file at <code>$old_config_file</code>, which is no longer supported. Please move your settings to a new file at <code>$config_file</code> and delete the old one." - - . "$config_file" - - # Check others - if [ -n "$WAYLAND_DISPLAY" ]; then - : "${CLIP_COPY:=wl-copy -n}" - : "${CLIP_PASTE_CLIP:=wl-paste}" - : "${CLIP_PASTE_PRIM:=wl-paste -p}" - : "${DMENU:=wmenu}" - : "${TERMINAL=foot}" - else - : "${CLIP_COPY:=xclip -i -r -selection clipboard}" - : "${CLIP_PASTE_CLIP:=xclip -o -selection clipboard}" - : "${CLIP_PASTE_PRIM:=xclip -o -selection primary}" - : "${DMENU:=dmenu}" - : "${TERMINAL=xterm}" - fi - - # Default values - : "${TRANS_LANGS:=:ru :en}" - : "${DMENU_TEXT:=${DMENU} -i -p 'Translate: Text'}" - : "${DMENU_LANG:=${DMENU} -i -p 'Translate: Into'}" - : "${DMENU_NEXT:=${DMENU} -i -p 'Translate: Next?'}" + config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/dmenu-translate" + [ -d "$config_dir" ] || mkdir -p "$config_dir" + + config_file="$config_dir/config.sh" + [ -f "$config_file" ] || touch "$config_file" + + old_config_file="$config_dir/config.conf" + [ -f "$old_config_file" ] && notify-send "dmenu-translate warning" "Found old configuration file at <code>$old_config_file</code>, which is no longer supported. Please move your settings to a new file at <code>$config_file</code> and delete the old one." + + . "$config_file" + + # Check others + if [ -n "$WAYLAND_DISPLAY" ]; then + : "${CLIP_COPY:=wl-copy -n}" + : "${CLIP_PASTE_CLIP:=wl-paste}" + : "${CLIP_PASTE_PRIM:=wl-paste -p}" + : "${DMENU:=wmenu}" + : "${TERMINAL=foot}" + else + : "${CLIP_COPY:=xclip -i -r -selection clipboard}" + : "${CLIP_PASTE_CLIP:=xclip -o -selection clipboard}" + : "${CLIP_PASTE_PRIM:=xclip -o -selection primary}" + : "${DMENU:=dmenu}" + : "${TERMINAL=xterm}" + fi + + # Default values + : "${TRANS_LANGS:=:ru :en}" + : "${DMENU_TEXT:=${DMENU} -i -p 'Translate: Text'}" + : "${DMENU_LANG:=${DMENU} -i -p 'Translate: Into'}" + : "${DMENU_NEXT:=${DMENU} -i -p 'Translate: Next?'}" } formatmenu() { - echo "$1" | tr '\n' ' ' | sed 's/\s\{3,\}//g; s/^\(.\{30\}\).\+/\1.../; s/$/\n/' + echo "$1" | tr '\n' ' ' | sed 's/\s\{3,\}//g; s/^\(.\{30\}\).\+/\1.../; s/$/\n/' } get_selection() { - [ -n "$1" ] && eval "$CLIP_PASTE_CLIP" || eval "$CLIP_PASTE_PRIM" + [ -n "$1" ] && eval "$CLIP_PASTE_CLIP" || eval "$CLIP_PASTE_PRIM" } clip_menu() { - { formatmenu "$1"; formatmenu "$2"; } | - sed 's/^\s*$//; 1s/^./Primary: &/; 2s/^./Clipboard: &/' | - sed '/^$/d' + { formatmenu "$1"; formatmenu "$2"; } | + sed 's/^\s*$//; 1s/^./Primary: &/; 2s/^./Clipboard: &/' | + sed '/^$/d' } lang_menu() { - echo "$TRANS_LANGS" | sed 's/\s\+/\n/g' | sed '/:/!s/^/:/' - echo '[Define]' + echo "$TRANS_LANGS" | sed 's/\s\+/\n/g' | sed '/:/!s/^/:/' + echo '[Define]' } choose_next() { - t="$1" - shift - if [ -n "$ALWAYS_COPY" ]; then - echo 'Copy' - else - eval "$DMENU_NEXT" "$@" <<-EOF + t="$1" + shift + if [ -n "$ALWAYS_COPY" ]; then + echo 'Copy' + else + eval "$DMENU_NEXT" "$@" <<-EOF Copy: $(formatmenu "$t") Copy temp file name View EOF - fi + fi } save_file() ( - tmp="$(mktemp --tmpdir 'dmenu-translate.XXXXXX')" - echo "$1" > "$tmp" - echo "$tmp" + tmp="$(mktemp --tmpdir 'dmenu-translate.XXXXXX')" + echo "$1" > "$tmp" + echo "$tmp" ) open_term() { - ${TERMINAL} -e ${PAGER:-'less'} "$(save_file "$1")" + ${TERMINAL} -e ${PAGER:-'less'} "$(save_file "$1")" } get_text() { - echo "$2" | while IFS= read -r clip; do - [ "$1" = "$clip" ] && { - type="$(echo "$clip" | sed 's/^\(\w\+\):.*/\1/')" - case "$type" in - Primary) echo "$primary" ;; - Clipboard) echo "$clipboard" ;; - esac - exit 10 - } - done - - # If not found, just echo text - [ $? -ne 10 ] && echo "$1" + echo "$2" | while IFS= read -r clip; do + [ "$1" = "$clip" ] && { + type="$(echo "$clip" | sed 's/^\(\w\+\):.*/\1/')" + case "$type" in + Primary) echo "$primary" ;; + Clipboard) echo "$clipboard" ;; + esac + exit 10 + } + done + + # If not found, just echo text + [ $? -ne 10 ] && echo "$1" } # Config @@ -129,9 +129,9 @@ target="$(lang_menu | eval "$DMENU_LANG" "$@")" || exit 0 # If Define chosen, define term and exit [ "$target" = '[Define]' ] && { - dict="$(trans -dictionary "$text")" - open_term "$(trans -dictionary "$text")" - exit 0 + dict="$(trans -dictionary "$text")" + open_term "$(trans -dictionary "$text")" + exit 0 } # Translate text @@ -139,10 +139,10 @@ translation="$(trans -b "$target" "$text")" [ -z "$translation" ] && err 'Failed to translate' case "$(choose_next "$translation" "$@")" in - 'View') open_term "$translation"; exit 0 ;; - 'Copy temp file name') output="$(save_file "$translation")" ;; - 'Copy'*) output="$translation" ;; - *) exit 0 ;; + 'View') open_term "$translation"; exit 0 ;; +'Copy temp file name') output="$(save_file "$translation")" ;; +'Copy'*) output="$translation" ;; +*) exit 0 ;; esac echo "$output" | eval "$CLIP_COPY" || err 'Failed to copy! Is CLIP_COPY setting set properly?' |
