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

支持通过环境变量指定Python地址和PAGER翻页 #80

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ issue: <a href="https://github.com/ChestnutHeng/Wudao-dict/issues/new">创建新
sudo pip3 install bs4
sudo pip3 install lxml
```

#### OpenSUSE
```
sudo zypper install python3-pip
Expand Down Expand Up @@ -101,6 +100,21 @@ Youdao is wudao, a powerful dict.

查词时可以直接使用`wd 词语`查汉英词典,或`wd word`查英汉词典(可以自动检测)。

## 配置

支持通过环境变量配置Python环境地址和PAGER程序。将以下内容加入shell配置文件(`.bashrc`、`.zshrc`等),或在运行时指定(例如:`WD_PAGER=bat wd word` )

```bash
# 系统存在多个Python环境时,建议指定专用python地址,例如`/usr/bin/python3`,避免切换环境时需重新安装bs4等依赖。不指定此变量则直接执行当前环境的`python3`
export WD_PYTHON_PATH=/your/env/path/python
# 指定pager命令。输出结果较长时可以使用翻页程序(例如`less`、`more`、`bat`等)翻页滚动。默认为空
export WD_PAGER_COMMAND='less -F' # -F参数设置less只有一页时不进入翻页模式
```

如图为指定`export WD_PAGER_COMMAND=bat`时的效果,可上下滚动查看内容:

<!-- # ![Zh_En Demo](https://github.com/ChestnutHeng/Wudao-dict/raw/master/img/wudao_zh.png) -->
![Pager Demo](./img/wudao_pager_bat.png)

## 小贴士

Expand Down
Binary file added img/wudao_pager_bat.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 40 additions & 6 deletions wudao-dict/wdd
Original file line number Diff line number Diff line change
@@ -1,11 +1,45 @@
#!/bin/bash

count=`ps -ef | grep "python3 WudaoServer.py" | grep -v "grep" | wc -l`

py=${WD_PYTHON_PATH:-python3}
pager=${WD_PAGER_COMMAND}

has_pymodule() {
$py -c "import $1" >/dev/null 2>&1
}

get_available_pager() {
for p in 'bat' 'less' 'more'; do
command -V $p >/dev/null 2>&1 && pager=$p
break
done
case $pager in
less)
pager='less -F'
;;
esac
}

check_and_fix_pypath() {
[[ "$(which $py)" == /usr/bin/* ]] && return

if ! (has_pymodule 'lxml' && has_pymodule 'bs4'); then
# using system py
py=/usr/bin/python3
fi
}

[[ $py =~ python3? ]] && check_and_fix_pypath
[[ -z $pager ]] && get_available_pager

count=$(ps -ef | grep "python3 WudaoServer.py" | grep -v "grep" | wc -l)

if [ $count == 0 ]; then
nohup python3 WudaoServer.py > ./usr/server.log 2>&1 &
git pull origin master > ./usr/pull.log 2>&1 &
nohup $py WudaoServer.py >./usr/server.log 2>&1 &
git pull origin master >./usr/pull.log 2>&1 &
fi

python3 WudaoCommand.py $*

if [[ -n "${pager}" ]]; then
$py WudaoCommand.py $* | $pager
else
$py WudaoCommand.py $*
fi