[misc] move dict.sh to a new project
This commit is contained in:
parent
3d6ccf3f08
commit
a980991fdc
85
bin/d
85
bin/d
|
@ -1,85 +0,0 @@
|
|||
#!/bin/sh
|
||||
|
||||
# shellcheck disable=2086,2059
|
||||
|
||||
# dmenu optional patches: preselect / center
|
||||
# fzf selector: 'fzf --reverse --height=30%%'
|
||||
set -e
|
||||
DIR="$(readlink -f "$(dirname "$0")")"
|
||||
|
||||
# compose selector command if not specified
|
||||
if [ -z "$SELECTOR" ]; then
|
||||
if command -v dmenu >/dev/null; then
|
||||
SELECTOR='dmenu -i -fn monospace:13 -nb #626262 -nf #bbbbbb -sb #5b97f7 -sf #eeeeee -l 20'
|
||||
dmenu -h 2>&1 | grep -qF ' [-n number]' && SELECTOR="$SELECTOR -n %i"
|
||||
dmenu -h 2>&1 | grep -qP '\[-\w*c\w*\]' && SELECTOR="$SELECTOR -c"
|
||||
elif command -v fzf >/dev/null; then
|
||||
SELECTOR='fzf --reverse --height=30%%'
|
||||
else
|
||||
echo 'require either dmenu or fzf'
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
EMPTY_RESULT=' 查无结果'
|
||||
GREETING=' 请输入单词并回车'
|
||||
|
||||
|
||||
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() {
|
||||
R="$RESULT"
|
||||
if [ -z "$R" ]; then
|
||||
if [ -n "$SENTENCE" ]; then
|
||||
R="$EMPTY_RESULT : $SENTENCE"
|
||||
else
|
||||
R="$GREETING"
|
||||
fi
|
||||
fi
|
||||
SELECT=$(printf "$SELECTOR" "$LINE_NUM")
|
||||
echo "$R" \
|
||||
| $SELECT \
|
||||
| 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
|
||||
echo awk '"'$INPUT'" { print NR }'
|
||||
LINE_NUM=$(echo "$RESULT" | awk -v INPUT="$INPUT" '$0==INPUT { print NR - 1 }')
|
||||
display $LINE_NUM
|
||||
elif echo "$INPUT" | grep -qF "$EMPTY_RESULT" || echo "$INPUT" | grep -qF "$GREETING"; then
|
||||
return
|
||||
elif echo "$INPUT" | grep -qP '^ '; then
|
||||
printf "%s" "$INPUT" | sed 's/^\s+//' | xsel -b
|
||||
else
|
||||
SENTENCE="$INPUT"
|
||||
request
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
SENTENCE="$*"
|
||||
if [ -n "$SENTENCE" ]; then
|
||||
request
|
||||
else
|
||||
display
|
||||
fi
|
||||
#pronounce "$@"
|
||||
|
41
bin/dict.awk
41
bin/dict.awk
|
@ -1,41 +0,0 @@
|
|||
#!/bin/awk
|
||||
|
||||
BEGIN {
|
||||
if (color) {
|
||||
HEAD = " \033[33m\033[1m%s\033[0m\n"
|
||||
TRAN = " \033[32m%s\033[0m\n"
|
||||
} else {
|
||||
HEAD = " %s\n"
|
||||
TRAN = " %s\n"
|
||||
}
|
||||
stop = 0
|
||||
}
|
||||
|
||||
!stop {
|
||||
if (match($0, /<meta name="keywords" content="(.+)"\/>/, m)) {
|
||||
sentence = m[1]
|
||||
} else if (match($0, /<span class="pronounce">(\w)/, m)) {
|
||||
pronounce = m[1]
|
||||
} else if (match($0, /<span class="phonetic">(.+)<\/span>/, m)) {
|
||||
phonetic = m[1]
|
||||
} else if (match($0, /<li>([a-z]+\..+)<\/li>/, m)) {
|
||||
if (sentence) {
|
||||
printf HEAD, sentence
|
||||
sentence = ""
|
||||
}
|
||||
printf TRAN, m[1]
|
||||
} else if (match($0, /<div id="synonyms"/)) {
|
||||
synonyms = 1
|
||||
printf HEAD, "同近义词"
|
||||
} else if (synonyms && match($0, /<a .+>(.+)<\/a>/, m)) {
|
||||
printf " %s\n", m[1]
|
||||
} else if (synonyms && match($0, /<\/div>/)) {
|
||||
stop = 1
|
||||
}
|
||||
|
||||
if (pronounce && phonetic) {
|
||||
printf " %s %s %s\n", PI, pronounce, phonetic
|
||||
pronounce = ""
|
||||
phonetic = ""
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user