manateelazycat / sdcv

Emacs interface for sdcv (Stardict console version)
73 stars 14 forks source link

rofi (dmenu) script #37

Closed sjehuda closed 2 months ago

sjehuda commented 2 months ago

Good evening,

I want to contribute a rofi script which makes use of sdcv.

#!/bin/sh

# Name    : sdcv-rofi
# About   : This is a rofi script to translate words and phrases with sdcv.
# Version : 2024-05-29
# License : MIT;
# Author  : Schimon Jehudah Zackary;

version="0.1"

if [ "$1" = "--help" ] ; then
  echo "Translate words or phrases and copy the results to clipboard."
  echo "rofi and sdcv and xclip or xsel or wl-paste must be installed."
  exit 0
elif [ "$1" = "--version" ] ; then
  echo "sdcv-dmenu version $version"
  exit 0
fi

case $XDG_SESSION_TYPE in
  x11 | tty | /dev/pts/1)
    if [ -x "$(command -v xsel)" ]; then
      clipboard=$(xsel)
    elif [ -x "$(command -v xclip)" ]; then
      clipboard=$(xclip -out)
    else
      echo "xclip is required to use clipboard."
    fi
    ;;
  wayland)
    if [ -x "$(command -v wl-paste)" ]; then
      clipboard=$(wl-paste --primary)
    else
      echo "wl-paste is required to use clipboard."
    fi
    ;;
  *)
    echo "Could not determine display server."
    echo "Defaulting to xclip."
    if [ -x "$(command -v xclip)" ]; then
      clipboard=$(xclip -out)
    else
      echo "wl-paste is required to use clipboard."
    fi
    ;;
esac

sdcv_translate() {
  chosen_option=$(echo -e "$clipboard\nClose" | rofi -dmenu -i -p "sdcv" -mesg "Enter a word or phrase to translate.");

  if [ -z "$chosen_option" ] || [ "$chosen_option" == "Close" ]; then
    exit 0
  else
    phrase=$chosen_option;
  fi

  #translation=$(sdcv --non-interactive --exact-search --use-dict="Babylon English-Hebrew" $phrase);
  #chosen_option=$(echo -e "Copy\nClose" | sed 's/<[^>]*>//g' | rofi -dmenu -i -p "Translation" -mesg $translation);
  message=$(sdcv --non-interactive $phrase | head -n 1);
  translation=$(sdcv --non-interactive $phrase | sed 1d);
  # 
  # Report to sdcv
  # $ sdcv --non-interactive [ðə ,ðɪ]
  # Nothing similar to [ðə, sorry :(
  # Nothing similar to ,ðɪ], sorry :(
  # 
  if [ -z "$translation" ]; then
    chosen_option=$(echo -e "Translate\nClose" | sed 's/<[^>]*>//g' | rofi -dmenu -i -p "sdcv" -mesg "$message");
  else
    chosen_option=$(echo -e "Close\n\n$translation\n\nCopy The Whole Entry" | sed 's/<[^>]*>//g' | rofi -dmenu -i -p "Translation" -mesg "$message Select a line to copy.");
  fi

  if [ "$chosen_option" == "Translate" ]; then
    sdcv_translate
  elif [ -z "$chosen_option" ] || [ "$chosen_option" == "Close" ]; then
    exit 0
  elif [ "$chosen_option" == "Copy The Whole Entry" ]; then
    # FIXME Result is copied into a single line
    echo $translation | xclip -in -selection clipboard;
  else
    echo $chosen_option | xclip -in -selection clipboard;
  fi
}

sdcv_translate

I have made this script after one of my favourite StarDict software package size has surpassed a 100MB, and which motivated me to find and alternative. I have found sdcv and then made a rofii script.

I suggest to add this script to this repository or to a new repository of manateelazycat/sdcv-rofi.

Kind regards, Schimon

manateelazycat commented 2 months ago

Welcome to PR ;)

sjehuda commented 2 months ago

Fine. I will add a fix for the clipboard managers, and then make a PR.

manateelazycat commented 2 months ago

I read the related issue, and I suggest you include these codes in your own warehouse. I usually don't use rofi, and adding the code here will increase my maintenance costs.

Thank you again for your help.

sjehuda commented 2 months ago

Great. I will host it at Codeberg and inform you when it is ready.