From f26cb84ebd8e82a62d618df07b17357bd9594c85 Mon Sep 17 00:00:00 2001 From: cnbattle Date: Mon, 29 Jun 2020 12:27:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=A4=9A=E6=AC=A1=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E4=B8=BA=E7=A9=BA=E6=97=B6=EF=BC=8C=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E5=AE=89=E8=A3=85=E4=BE=9D=E8=B5=96=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 ++++++++++++ aitranslate.go | 4 ++-- aitranslate.service | 13 +++++++++++++ go.mod | 1 + go.sum | 3 +++ translate.go | 7 +++++++ 6 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 aitranslate.service diff --git a/README.md b/README.md index d32cd14..cf1fb24 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,18 @@ - [ ] 增加测试代码 ## 使用 +### 依赖 +- Linux `xsel`或`xclip` +``` +// Manjaro/Arch: +sudo pacman -S xsel xclip +// Debian/Ubuntu +sudo apt-get install xsel xclip +// Fedora +sudo dnf install xsel xclip +``` +- Windows 10 `PowerShell` + ### Linux or Mac ``` nohup ./aitranslate -c [Google|YouDao] & diff --git a/aitranslate.go b/aitranslate.go index 89082c9..1a97abe 100644 --- a/aitranslate.go +++ b/aitranslate.go @@ -11,8 +11,8 @@ var ( text string // 通知logo path string - // 监听时间间隔 - monitoringInterval int = 100 + // 监听时间间隔 毫秒 + monitoringInterval = 100 ) func init() { diff --git a/aitranslate.service b/aitranslate.service new file mode 100644 index 0000000..5747e06 --- /dev/null +++ b/aitranslate.service @@ -0,0 +1,13 @@ +[Unit] +Description=aitranslate +After=network.target + +[Service] +Type=simple +Restart=always +RestartSec=1s +ExecStart=/usr/bin/aitranslate +WorkingDirectory=/usr/bin/ + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/go.mod b/go.mod index 36ae215..dc06cd3 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/abadojack/whatlanggo v1.0.1 github.com/atotto/clipboard v0.1.2 github.com/gen2brain/beeep v0.0.0-20200420150314-13046a26d502 + github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect github.com/stretchr/testify v1.5.1 // indirect github.com/tidwall/gjson v1.6.0 ) diff --git a/go.sum b/go.sum index 1618a9a..94a7370 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,9 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gen2brain/beeep v0.0.0-20200420150314-13046a26d502 h1:e+btj9i/zyL7a2WrgOeKl2rTMMYkzGJIuVzziFu3fi0= github.com/gen2brain/beeep v0.0.0-20200420150314-13046a26d502/go.mod h1:KvEoJUYimgK9VGK9RZu1RvlPHfdwtGw1+m6rnfPK+lI= +github.com/go-bindata/go-bindata v1.0.0 h1:DZ34txDXWn1DyWa+vQf7V9ANc2ILTtrEjtlsdJRF26M= +github.com/go-bindata/go-bindata v3.1.2+incompatible h1:5vjJMVhowQdPzjE1LdxyFF7YFTXg5IgGVW4gBr5IbvE= +github.com/go-bindata/go-bindata v3.1.2+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo= github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4 h1:qZNfIGkIANxGv/OqtnntR4DfOY2+BgwR60cAcu/i3SE= github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4/go.mod h1:kW3HQ4UdaAyrUCSSDR4xUzBKW6O2iA4uHhk7AtyYp10= github.com/godbus/dbus v4.1.0+incompatible h1:WqqLRTsQic3apZUK9qC5sGNfXthmPXzUZ7nQPrNITa4= diff --git a/translate.go b/translate.go index d808dec..e7690c5 100644 --- a/translate.go +++ b/translate.go @@ -9,11 +9,15 @@ import ( // translates Translate and Notify func translates() { + emptyTimes := 0 beeep.Alert("", "自动翻译运行中...", "") text = getClipboardString() for { time.Sleep(time.Millisecond * time.Duration(monitoringInterval)) newText := getClipboardString() + if strings.EqualFold("", newText) { + emptyTimes++ + } if !strings.EqualFold(text, newText) { text = newText translateText, err := translate.Trans(channel, text) @@ -23,5 +27,8 @@ func translates() { } beeep.Alert(channel+" Translate", translateText, getImagePath("logo")) } + if emptyTimes == 30 { + beeep.Alert("Error", "多次未检查到内容,请确定依赖包已经安装", getImagePath("warning")) + } } }