Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rofi (dmenu) script #37

Closed
sjehuda opened this issue Jun 29, 2024 · 4 comments
Closed

rofi (dmenu) script #37

sjehuda opened this issue Jun 29, 2024 · 4 comments

Comments

@sjehuda
Copy link

sjehuda commented Jun 29, 2024

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
Copy link
Owner

Welcome to PR ;)

@sjehuda
Copy link
Author

sjehuda commented Jun 30, 2024

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

@manateelazycat
Copy link
Owner

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
Copy link
Author

sjehuda commented Jul 1, 2024 via email

@sjehuda sjehuda closed this as completed Jul 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants