2020-11-04 14:54:58 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
2020-11-04 15:31:25 +00:00
|
|
|
# shellcheck disable=2086
|
|
|
|
|
2020-11-04 14:54:58 +00:00
|
|
|
set -e
|
|
|
|
DIR="$(readlink -f "$(dirname "$0")")"
|
|
|
|
DMENU_OPTS='-i -c -fn monospace:13 -nb #626262 -nf #bbbbbb -sb #5b97f7 -sf #eeeeee -l 20'
|
|
|
|
|
|
|
|
|
|
|
|
request() {
|
|
|
|
RESULT="$(
|
|
|
|
curl -SsLG "http://dict.youdao.com/w/eng/$SENTENCE/" \
|
|
|
|
| awk -v PI=' ' -f "$DIR/dict.awk"
|
|
|
|
)"
|
|
|
|
display
|
|
|
|
}
|
|
|
|
|
|
|
|
pronounce() {
|
|
|
|
PLAY=afplay
|
|
|
|
! command -v "$PLAY" 2>/dev/null && PLAY="ffplay -nodisp -autoexit"
|
|
|
|
FP="/tmp/dict/$*.mp3"
|
|
|
|
mkdir -p /tmp/dict
|
|
|
|
[ ! -f "$FP" ] && curl -o "$FP" "http://dict.youdao.com/dictvoice?audio=$1&type=$2" 1>/dev/null 2>&1
|
|
|
|
$PLAY "$FP" 1>/dev/null 2>&1
|
|
|
|
}
|
|
|
|
|
|
|
|
display() {
|
|
|
|
echo "$RESULT" \
|
|
|
|
| dmenu $DMENU_OPTS \
|
|
|
|
| if IFS= read -r INPUT; then
|
|
|
|
if echo "$INPUT" | grep -qP ' '; then
|
|
|
|
if echo "$INPUT" | grep -q '美'; then
|
|
|
|
pronounce "$SENTENCE" 2 &
|
|
|
|
else
|
|
|
|
pronounce "$SENTENCE" 1 &
|
|
|
|
fi
|
|
|
|
display
|
|
|
|
elif echo "$INPUT" | grep -P '^ '; then
|
2020-11-04 15:31:25 +00:00
|
|
|
printf "%s" "$INPUT" | sed 's/^\s+//' | xsel -b
|
2020-11-04 14:54:58 +00:00
|
|
|
else
|
|
|
|
SENTENCE="$INPUT"
|
|
|
|
request
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SENTENCE="$*"
|
2020-11-04 15:31:25 +00:00
|
|
|
if [ -n "$SENTENCE" ]; then
|
|
|
|
request
|
|
|
|
else
|
|
|
|
display
|
|
|
|
fi
|
2020-11-04 14:54:58 +00:00
|
|
|
#pronounce "$@"
|
|
|
|
|