Compare commits

...

13 Commits
v2 ... main

Author SHA1 Message Date
JuanZoran
6a9d887db7 refactor: add warning message 2023-04-07 19:56:07 +08:00
JuanZoran
4d547a0397 docs: 注意: 此分支已经不再打算维护, 新版本目前在expermental分支, 稳定后会设置成默认分支, expermantal分支已经加入了百度和有道在线翻译 2023-03-17 10:31:12 +08:00
JuanZoran
b2851cffd8 feat: add health check and update README.md 2023-03-07 22:31:32 +08:00
JuanZoran
c6ad825dac fix: fix close window twice 2023-02-18 13:24:13 +08:00
JuanZoran
c6c5bf4f7c fix: fix window is not valid 2023-02-18 13:13:32 +08:00
DeepSource Bot
ce43dbd489 Add .deepsource.toml 2023-02-16 17:18:16 +00:00
JuanZoran
adfbe7f50c docs: 添加mac发音感谢说明 2023-02-08 13:23:25 +08:00
Zoran
5355d9c97e
Merge pull request #24 from happysmile12321/main
add mac specify commit
2023-02-08 13:19:52 +08:00
happysmile
2172d29f08 add mac specify commit 2023-02-08 12:17:36 +08:00
JuanZoran
c35cfbb0f5 chore: merge branch 'expermental' 2023-02-07 17:18:52 +08:00
JuanZoran
e723f4177f Merge branch 'experimental' 2023-02-07 17:15:54 +08:00
JuanZoran
b62478cf2d fix: 取消了回调的设计, 修复了部分bug
将回调的接口换成了run的函数接口, 修复了在线查询, 自动命令, 窗口显示的bug
2023-02-03 15:33:32 +08:00
JuanZoran
acfde8e4f5 Merge branch 'experimental' 2023-02-01 17:18:51 +08:00
9 changed files with 273 additions and 191 deletions

7
.deepsource.toml Normal file
View File

@ -0,0 +1,7 @@
version = 1
[[analyzers]]
name = "shell"
[[analyzers]]
name = "javascript"

View File

@ -17,6 +17,8 @@
- [待办 (画大饼)](#待办-画大饼)
<!--toc:end-->
## 注意: 此分支已经不再打算维护, 新版本目前在expermental分支, 稳定后会设置成默认分支
## 特点
- 使用纯 lua 编写, 速度极快
@ -28,36 +30,44 @@
> 见 wiki
- 大部分功能可以自定义:
- 高亮
- 悬浮大小
- 排版顺序
- 弹窗大小
- `舒服窗口动画`
- etc (更多可以查看[配置](#配置))
- **完全离线** 的单词翻译体验 (可能后面会支持在线翻译)
- 支持显示:
- 柯林斯星级
- 牛津 3000 词汇
- 中文翻译
- 英文翻译 (不是英译中, 而是用英文解释)
- 词根
- etc
- 舒服的排版和`动画`
- 支持 `normal``visual`模式
> <font color='#FF9900'>不支持 visual-block mode</font>
- 本地词库单词量: `430w`
## 屏幕截图
### 演示
https://user-images.githubusercontent.com/107862700/213752097-2eee026a-ddee-4531-bf80-ba2cbc8b44ef.mp4
> 视频演示的在线查询, 查询速度取决于你的网络状况
> 可以打开音量查看自动读音
https://user-images.githubusercontent.com/107862700/215941500-3293c571-20a1-44e2-b202-77079f158ce9.mp4
https://user-images.githubusercontent.com/107862700/213752097-2eee026a-ddee-4531-bf80-ba2cbc8b44ef.mp4
### 主题
> 如果你有更美观或者更适合的配色, 欢迎提 PR
@ -145,10 +155,14 @@ use {
<font color="#FF9900">**注意事项**: </font>
- **如果插件无法正常工作, 请运行**`:check Trans`, 查看插件是否安装正确并且处于正常工作环境
- `install.sh`
- 使用了 `wget`下载词库, 安装请确保你的环境变量中存在 wget
- install.sh 下载后会自动将词库解压, 并移动到 `$HOME/.vim/dict`文件夹下
- 目前仅在 `Ubuntu22.04`的环境下测试通过
> 如果上述条件不符合, 请删掉 `run = 'install.sh'`部分, 考虑手动安装词库
> 如果上述条件满足, 仍出现问题, 欢迎在 issue 里向我反馈,我会及时尝试解决
@ -156,17 +170,16 @@ use {
- 下载词典的过程中, 需要能够 `流畅的访问github下载`
> 词库文件压缩包大小为: **281M**
> 解压缩后的大小大概为: 1.2G
> 解压缩后的大小大概为: **1.2G**
- 安装后如果不能正常运行, 请尝试检查一下问题:
- 本机是否已经安装了 `sqlite3`
> Linux 下安装:
> `sudo pacman -S sqlite # Arch`
> `sudo apt-get install sqlite3 libsqlite3-dev # Ubuntu`
> 后续会增加 `healthcheck` 进行检查
- **`auto_play`** 使用步骤:
> linux 只需要安装`festival`
@ -174,8 +187,10 @@ use {
> **_如果你想要设置音色发音可以访问:_** [Festival 官方](https://www.cstr.ed.ac.uk/projects/festival/morevoices.html)
> 可以选择英音、美音、男声、女声
> 其他操作系统
> mac 系统使用`say` (感谢[@happysmile12321](https://github.com/happysmile12321) )
> 其他操作系统
- 需要确保安装了`nodejs`
- 进入插件的`tts`目录运行`npm install`
> 如果`install.sh`运行正常则自动安装,如果安装失败,请尝试手动安装

View File

@ -142,10 +142,8 @@ buffer.__index = function(self, key)
local res = buffer[key]
if res then
return res
elseif type(key) == 'number' then
return fn.getbufoneline(self.bufnr, key)
else
error('invalid key' .. key)
end

63
lua/Trans/health.lua Normal file
View File

@ -0,0 +1,63 @@
local M = {}
M.check = function()
local health = vim.health
local ok = health.report_ok
local warn = health.report_warn
local error = health.report_error
local has = vim.fn.has
local executable = vim.fn.executable
-- INFO :Check neovim version
if has('nvim-0.9') == 1 then
ok [[
[PASS]: you have Trans.nvim with full features in neovim-nightly
]]
else
warn [[
[WARN]: Trans Title requires Neovim 0.9 or newer
See neovim-nightly: https://github.com/neovim/neovim/releases/tag/nightly
]]
end
-- INFO :Check Sqlite
local has_sqlite = pcall(require, 'sqlite')
if has_sqlite then
ok [[
[PASS]: Dependency sqlite.lua is installed
]]
else
error [[
[ERROR]: Dependency sqlite.lua can't work correctly
Please Read the doc in github carefully
]]
end
if executable('sqlite3') then
ok [[
[PASS]: Dependency sqlite3 found
]]
else
error [[
[ERROR]: Dependency sqlite3 not found
]]
end
-- INFO :Check stardict
local db_path = vim.fn.expand(require('Trans').conf.db_path)
if vim.fn.filereadable(db_path) == 1 then
ok [[
[PASS]: Stardict database found
]]
else
error [[
[PASS]: Stardict database not found
Please check the doc in github
]]
end
end
return M

View File

@ -1,11 +1,11 @@
local M = {}
local api, fn = vim.api, vim.fn
if fn.executable('sqlite3') ~= 1 then
error('Please check out sqlite3')
if fn.executable 'sqlite3' ~= 1 then
error 'Please check out sqlite3'
end
local win_title = fn.has('nvim-0.9') == 1 and {
local win_title = fn.has 'nvim-0.9' == 1 and {
{ '', 'TransTitleRound' },
{ ' Trans', 'TransTitle' },
{ '', 'TransTitleRound' },
@ -24,23 +24,27 @@ string.width = api.nvim_strwidth
string.isEn = function(self)
local char = { self:byte(1, -1) }
for i = 1, #self do
if char[i] > 127 then
if char[i] > 128 then
return false
end
end
return true
end
string.play = fn.has('linux') == 1 and function(self)
string.play = fn.has 'linux' == 1 and function(self)
local cmd = ([[echo "%s" | festival --tts]]):format(self)
fn.jobstart(cmd)
end or fn.has 'mac' == 1 and function(self)
local cmd = ([[say "%s"]]):format(self)
fn.jobstart(cmd)
end or function(self)
local seperator = fn.has('unix') and '/' or '\\'
local file = debug.getinfo(1, "S").source:sub(2):match('(.*)lua') .. seperator .. 'tts' .. seperator .. 'say.js'
local seperator = fn.has 'unix' and '/' or '\\'
local file = debug.getinfo(1, 'S').source:sub(2):match '(.*)lua' .. seperator .. 'tts' .. seperator .. 'say.js'
fn.jobstart('node ' .. file .. ' ' .. self)
end
M.conf = {
warning = true,
view = {
i = 'float',
n = 'hover',
@ -132,16 +136,8 @@ M.conf = {
-- appPasswd = '',
-- },
},
-- TODO :
-- register word
-- history = {
-- -- TOOD
-- }
-- TODO :add online translate engine
}
local times = 0
M.setup = function(opts)
if opts then
M.conf = vim.tbl_deep_extend('force', M.conf, opts)
@ -162,34 +158,27 @@ M.setup = function(opts)
engines[i] = k
i = i + 1
end
conf.engines = engines
times = times + 1
if times == 1 then
---@format disable
local new_command = api.nvim_create_user_command
new_command('Translate' , function() M.translate() end, { desc = ' 单词翻译',})
new_command('TranslateInput' , function() M.translate('i') end, { desc = ' 搜索翻译',})
new_command('TransPlay' , function()
local word = M.get_word(api.nvim_get_mode().mode)
if word ~= '' and word:isEn() then
word:play()
end
end, { desc = ' 自动发音' })
local set_hl = api.nvim_set_hl
local hls = require('Trans.ui.theme')[conf.theme]
local hls = require 'Trans.ui.theme'[conf.theme]
for hl, opt in pairs(hls) do
set_hl(0, hl, opt)
end
---@format enable
if M.conf.warning then
vim.notify([[
v2已经发布, :
https://github.com/JuanZoran/Trans.nvim
,
]], vim.log.levels.WARN)
end
end
local function get_select()
local _start = fn.getpos("v")
local _end = fn.getpos('.')
local _start = fn.getpos 'v'
local _end = fn.getpos '.'
if _start[2] > _end[2] or (_start[3] > _end[3] and _start[2] == _end[2]) then
_start, _end = _end, _start
@ -206,7 +195,6 @@ local function get_select()
if s_row == e_row then
return line:sub(s_col, e_col)
else
local lines = fn.getline(s_row, e_row)
local i = #lines
@ -219,16 +207,14 @@ end
M.get_word = function(mode)
local word
if mode == 'n' then
word = fn.expand('<cword>')
word = fn.expand '<cword>'
elseif mode == 'v' then
api.nvim_input('<ESC>')
api.nvim_input '<ESC>'
word = get_select()
elseif mode == 'i' then
-- TODO Use Telescope with fuzzy finder
---@diagnostic disable-next-line: param-type-mismatch
word = fn.input('请输入需要查询的单词:')
word = fn.input '请输入需要查询的单词:'
else
error('invalid mode: ' .. mode)
end
@ -237,9 +223,17 @@ end
M.translate = function(mode, view)
if M.conf.warning then
vim.notify([[
, :
https://github.com/JuanZoran/Trans.nvim
,
]], vim.log.levels.WARN)
end
vim.validate {
mode = { mode, 's', true },
view = { view, 's', true }
view = { view, 's', true },
}
mode = mode or api.nvim_get_mode().mode
@ -253,6 +247,6 @@ M.translate = function(mode, view)
end
end
M.ns = api.nvim_create_namespace('Trans')
M.ns = api.nvim_create_namespace 'Trans'
return M

View File

@ -82,6 +82,7 @@ local function process()
end
return function(word)
buffer:init()
-- TODO :online query
-- local float = conf.float
vim.notify([[

View File

@ -1,9 +1,10 @@
local api = vim.api
local conf = require('Trans').conf
local hover = conf.hover
local buffer = require('Trans.buffer')()
local error_msg = conf.icon.notfound .. ' 没有找到相关的翻译'
local buffer = require('Trans.buffer')()
local node = require('Trans.node')
local it, t, f = node.item, node.text, node.format
@ -33,7 +34,6 @@ local function handle_result(result)
if not phonetic and not collins and not oxford then
buffer:addline(it(word, 'TransWord'))
else
buffer:addline(f {
width = hover.width,
@ -50,7 +50,6 @@ local function handle_result(result)
})
end
end,
tag = function(tag)
addtitle('标签')
local tag_map = {
@ -86,7 +85,6 @@ local function handle_result(result)
buffer:addline('')
end,
pos = function(pos)
addtitle('词性')
local pos_map = {
@ -114,7 +112,6 @@ local function handle_result(result)
buffer:addline('')
end,
exchange = function(exchange)
addtitle('词形变化')
local exchange_map = {
@ -138,7 +135,6 @@ local function handle_result(result)
buffer:addline('')
end,
translation = function(translation)
addtitle('中文翻译')
@ -150,7 +146,6 @@ local function handle_result(result)
buffer:addline('')
end,
definition = function(definition)
addtitle('英文注释')
@ -215,11 +210,9 @@ local function handle_keymap(win, word)
pageup = function()
buffer:normal('gg')
end,
pagedown = function()
buffer:normal('G')
end,
pin = function()
if lock then
error('请先关闭窗口')
@ -256,7 +249,6 @@ local function handle_keymap(win, word)
})
end)
end,
close = function()
pcall(api.nvim_del_autocmd, cmd_id)
local run = win:try_close()
@ -265,7 +257,6 @@ local function handle_keymap(win, word)
end)
try_del_keymap()
end,
toggle_entry = function()
if lock and win:is_valid() then
local prev = api.nvim_get_current_win()
@ -275,7 +266,6 @@ local function handle_keymap(win, word)
del('n', keymap.toggle_entry)
end
end,
play = function()
if word then
word:play()
@ -379,7 +369,6 @@ return function(word)
win:set('wrap', true)
handle_keymap(win, word)
end)
else
local win, run = open_window {
width = error_msg:width(),

View File

@ -27,8 +27,10 @@ end
---@param option string option name
---@param value any
function window:set(option, value)
if self:is_valid() then
api.nvim_win_set_option(self.winid, option, value)
end
end
---@param name string option name
---@return any
@ -99,6 +101,7 @@ function window:try_close()
field = field,
target = 1,
}
run(function()
api.nvim_win_close(self.winid, true)
end)

12
plugin/Trans.lua Normal file
View File

@ -0,0 +1,12 @@
local M = require("Trans")
local api = vim.api
local command = api.nvim_create_user_command
command('Translate', function() M.translate() end, { desc = ' 单词翻译', })
command('TranslateInput', function() M.translate('i') end, { desc = ' 搜索翻译', })
command('TransPlay', function()
local word = M.get_word(api.nvim_get_mode().mode)
if word ~= '' and word:isEn() then
word:play()
end
end, { desc = ' 自动发音' })