chore: add misc file

This commit is contained in:
JuanZoran 2023-03-07 21:52:29 +08:00
parent 7dbf3b17be
commit e9f5f7f160
4 changed files with 104 additions and 55 deletions

View File

@ -0,0 +1,13 @@
local M = {}
M.__index = function(tbl, k)
local res, engine = pcall(require, 'Trans.backend.' .. k)
assert(res, [[Can't Found Engine:]] .. k)
tbl[k] = engine
return engine
end
return setmetatable(M, M)

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

@ -118,31 +118,9 @@ M.conf = {
-- no = '❌'
},
theme = 'default',
-- theme = 'dracula',
-- theme = 'tokyonight',
db_path = '$HOME/.vim/dict/ultimate.db',
engine = {
youdao = {},
-- baidu = {
-- appid = '',
-- appPasswd = '',
-- },
-- -- youdao = {
-- appkey = '',
-- 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)
@ -165,27 +143,12 @@ M.setup = function(opts)
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]
for hl, opt in pairs(hls) do
set_hl(0, hl, opt)
end
---@format enable
end
end
local function get_select()
@ -207,7 +170,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
@ -221,11 +183,9 @@ M.get_word = function(mode)
local word
if mode == 'n' then
word = fn.expand('<cword>')
elseif mode == 'v' then
api.nvim_input('<ESC>')
word = get_select()
elseif mode == 'i' then
-- TODO Use Telescope with fuzzy finder
---@diagnostic disable-next-line: param-type-mismatch

13
plugin/Trans.lua Normal file
View File

@ -0,0 +1,13 @@
local api = vim.api
local M = require('Trans')
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 = ' 自动发音' })