feat: add auto_play for hover mode

This commit is contained in:
JuanZoran 2023-01-21 21:13:51 +08:00
parent eb10830eb5
commit 294ffe1b9f
8 changed files with 70 additions and 23 deletions

3
.gitignore vendored
View File

@ -1,6 +1,7 @@
lua/Trans/util/
lua/Trans/test/
note/
go/
demo.mp4
screenshot.gif
tts/node_modules/
tts/package-lock.json

View File

@ -103,6 +103,12 @@ use {
> 后续会增加 `healthcheck` 进行检查
- **`auto_play`** 使用步骤:
- 需要确保安装了`nodejs`
- 进入插件的`tts`目录运行`npm install`
> 如果`install.sh`运行正常则自动安装,如果安装失败,请尝试手动安装
- linux 用户需要额外安装以下依赖:
> sudo apt-get install festival festvox-kallpc16k
## 配置
```lua
@ -125,14 +131,24 @@ require'Trans'.setup {
-- TODO :
pageup = '[[',
pagedown = ']]',
pin = '_', -- 将窗口固定在右上角, 参考demo
close = '+',
pin = '<leader>[',
close = '<leader>]',
toggle_entry = '<leader>;',
play = '_',
},
animation = {
open = 'slid', -- 可选的样式: slid , fold
-- open = 'fold',
-- close = 'fold',
open = 'slid',
close = 'slid',
interval = 12, -- 动画的帧间隔
}
interval = 12,
},
auto_close_events = {
'InsertEnter',
'CursorMoved',
'BufLeave',
},
auto_play = true, -- WARN : 请阅读说明
},
float = {
width = 0.8,
@ -167,14 +183,15 @@ require'Trans'.setup {
},
icon = {
star = '',
notfound = '❔',
yes = '✔️',
no = '❌'
-- notfound = '❔',
notfound = ' ',
yes = ' ',
no = ''
-- yes = '✔️',
-- no = '❌'
-- star = '⭐',
-- notfound = '',
-- yes = '',
-- no = ''
},
db_path = '$HOME/.vim/dict/ultimate.db',
-- TODO :

View File

@ -15,3 +15,4 @@ wget https://github.com/skywind3000/ECDICT-ultimate/releases/download/1.0.0/ecdi
unzip /tmp/dict.zip -d $HOME/.vim/dict
rm -rf /tmp/dict.zip
cd ./tts/ && npm install

View File

@ -22,6 +22,7 @@ M.conf = {
pin = '<leader>[',
close = '<leader>]',
toggle_entry = '<leader>;',
play = '_',
},
animation = {
-- open = 'fold',
@ -35,6 +36,7 @@ M.conf = {
'CursorMoved',
'BufLeave',
},
auto_play = true,
},
float = {
width = 0.8,
@ -69,13 +71,13 @@ M.conf = {
},
icon = {
star = '',
notfound = '',
yes = '✔️',
no = ''
-- notfound = '❔',
notfound = '',
yes = '',
no = ''
-- yes = '✔️',
-- no = '❌'
-- star = '⭐',
-- notfound = '',
-- yes = '',
-- no = ''
},
db_path = '$HOME/.vim/dict/ultimate.db',

View File

@ -2,6 +2,7 @@ if vim.fn.executable('sqlite3') ~= 1 then
error('Please check out sqlite3')
end
vim.api.nvim_create_user_command('Translate', function()
require("Trans").translate()
end, { desc = ' 单词翻译', })

View File

@ -186,6 +186,8 @@ end
local action
local next
local _word
action = {
pageup = function()
m_window:normal('gg')
@ -251,7 +253,12 @@ action = {
else
vim.keymap.del('n', conf.hover.keymap.toggle_entry, { buffer = true })
end
end
end,
play = function()
local file = debug.getinfo(1, "S").source:sub(2):match('(.*)lua/') .. 'tts/say.js'
vim.fn.jobstart('node ' .. file .. ' ' .. _word)
end,
}
@ -259,11 +266,15 @@ return function(word)
vim.validate {
word = { word, 's' },
}
_word = word
-- 目前只处理了本地数据库的查询
m_result = require('Trans.query.offline')(word)
local hover = conf.hover
local opt = {
if hover.auto_play then
action.play()
end
local opt = {
relative = 'cursor',
width = hover.width,
height = hover.height,
@ -298,6 +309,7 @@ return function(word)
end)
-- Auto Close
cmd_id = api.nvim_create_autocmd(
hover.auto_close_events, {
@ -310,7 +322,9 @@ return function(word)
end,
})
for act, key in pairs(hover.keymap) do
vim.keymap.set('n', key, action[act], { buffer = true, silent = true })
if m_result then
for act, key in pairs(hover.keymap) do
vim.keymap.set('n', key, action[act], { buffer = true, silent = true })
end
end
end

5
tts/package.json Normal file
View File

@ -0,0 +1,5 @@
{
"dependencies": {
"say": "^0.16.0"
}
}

6
tts/say.js Normal file
View File

@ -0,0 +1,6 @@
const say = require('say')
word = process.argv
// console.log(word)
say.speak(word.slice(2))