Skip to content

getting word definition on terminal if cli is used #32

Description

@amirreza-sf80

hi 👋
i altered the define script a bit so when invoked from the command line, it shows the result in the command line instead of notification (gpt is used in modifying this, as i am not a shell scripter)

#!/usr/bin/env bash
cli_mode=false
if [[ -n"$1" ]];then
cli_mode=true
fi
word=${1:-$(xclip -o -selection primary 2>/dev/null || wl-paste -p 2>/dev/null)}# Check for empty word or special characters
[[ -z"$word"||"$word"=~ [\/] ]] && notify-send -h string:bgcolor:#bf616a -t 3000 "Invalid input." && exit 0
query=$(curl -s --connect-timeout 5 --max-time 10 "https://api.dictionaryapi.dev/api/v2/entries/en_US/$word")# Check for connection error (curl exit status stored in $?)if [[ $?-ne 0 ]];thenif$cli_mode;thenecho"Connection error."else
notify-send -h string:bgcolor:#bf616a -t 3000 "Connection error."fiexit 1
fi# Check for invalid word responseif [[ "$query"==*"No Definitions Found"* ]];thenif$cli_mode;thenecho"Invalid word."else
notify-send -h string:bgcolor:#bf616a -t 3000 "Invalid word."fiexit 0
fi# Show only first 3 definitions
def=$(echo "$query"| jq -r '[.[].meanings[] | {pos: .partOfSpeech, def: .definitions[].definition}] | .[:3].[] | "\n\(.pos). \(.def)"')# Requires a notification daemon to be installedif$cli_mode;thenecho"$word"echo"---------------"echo"$def"else
notify-send -t 60000 "$word -""$def"fi

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions