commit 1f2273518495b139ae5378088e1c7c6fe7e4b717
parent 50f4997ae3bf2345f54e41644124b5b08f2f7fbc
Author: awy <awy@awy.one>
Date: Fri, 30 May 2025 18:44:07 +0300
word definition
Diffstat:
2 files changed, 38 insertions(+), 0 deletions(-)
diff --git a/.config/sway/config b/.config/sway/config
@@ -107,6 +107,7 @@ bindsym $mod+Shift+space floating toggle
# bindsym $mod+Shift+Return exec 'swaymsg mark swapme && swaymsg focus left && swaymsg swap container with mark swapme && swaymsg unmark swapme'
bindsym $mod+Shift+Return exec $term -a termfloat
bindsym $mod+grave exec bookmarks
+bindsym $mod+Shift+grave exec define
bindsym XF86AudioPlay exec mpc toggle
bindsym XF86AudioNext exec mpc next
bindsym XF86AudioPrev exec mpc prev
diff --git a/.local/bin/define b/.local/bin/define
@@ -0,0 +1,37 @@
+#!/usr/bin/env bash
+
+word=${1:-$(xclip -o -selection primary 2>/dev/null || wl-paste 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 $?)
+[ $? -ne 0 ] && notify-send -h string:bgcolor:#bf616a -t 3000 "Connection error." && exit 1
+
+# Check for invalid word response
+[[ "$query" == *"No Definitions Found"* ]] && notify-send -h string:bgcolor:#bf616a -t 3000 "Invalid word." && exit 0
+
+# 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 installed
+notify-send -t 60000 "$word -" "$def"
+
+
+### MORE OPTIONS :)
+
+# Show first definition for each part of speech (thanks @morgengabe1 on youtube)
+# def=$(echo "$query" | jq -r '.[0].meanings[] | "\(.partOfSpeech): \(.definitions[0].definition)\n"')
+
+# Show all definitions
+# def=$(echo "$query" | jq -r '.[].meanings[] | "\n\(.partOfSpeech). \(.definitions[].definition)"')
+
+# Regex + grep for just definition, if anyone prefers that to jq
+# def=$(grep -Po '"definition":"\K(.*?)(?=")' <<< "$query")
+
+# bold=$(tput bold) # Print text bold with echo, for visual clarity
+# normal=$(tput sgr0) # Reset text to normal
+# echo "${bold}Definition of $word"
+# echo "${normal}$def"