refactor: change default dict dir to plugin dir/extra, change Trans.json style

This commit is contained in:
JuanZoran 2023-03-23 14:56:48 +08:00
parent 84e06a268e
commit 6def81f2c6
9 changed files with 58 additions and 54 deletions

View File

@ -2,5 +2,4 @@
./util/md5.lua
./util/base64.lua
./test
./util/bing_node.lua
./style

View File

@ -1,17 +1,21 @@
local Trans = require("Trans")
local db = require("sqlite.db")
local path = Trans.conf.dir .. Trans.separator .. "ultimate.db"
local dict = db:open(path)
local db_name = "stardict"
vim.api.nvim_create_autocmd("VimLeavePre", {
callback = function()
if db:isopen() then db:close() end
end,
})
---@class TransOfflineBackend
local M = {
name = "offline",
no_wait = true,
}
local db = require("sqlite.db")
vim.api.nvim_create_autocmd("VimLeavePre", {
once = true,
callback = function()
if db:isopen() then db:close() end
end,
})
---@param data any
---@return any
---@overload fun(TransData): TransResult
@ -20,10 +24,6 @@ function M.query(data)
return
end
local path = require("Trans").conf.dir .. "/ultimate.db"
local dict = db:open(path)
local db_name = data.db_name or "stardict"
local res = dict:select(db_name, {
where = { word = data.str },
keys = M.query_field,

View File

@ -30,10 +30,10 @@ end
local all_name = {}
local backend_order = conf.backend_order or vim.tbl_keys(user_conf)
for _, name in ipairs(backend_order) do
if not user_conf[name].disable and user_conf[name] then
all_name[#all_name + 1] = name
for _, config in ipairs(user_conf) do
if not config.disable then
all_name[#all_name + 1] = config.name
user_conf[config.name] = config
end
end

View File

@ -5,13 +5,12 @@
---@class TransConf
return {
---@type string the directory for database file and password file
dir = os.getenv('HOME') .. '/.vim/dict',
query = 'fallback',
debug = true,
dir = require('Trans').relative_path({ 'extra' }, true),
query = 'fallback',
debug = true,
---@type 'default' | 'dracula' | 'tokyonight' global Trans theme [see lua/Trans/style/theme.lua]
theme = 'default', -- default | tokyonight | dracula
backend_order = nil,
strategy = {
theme = 'default', -- default | tokyonight | dracula
strategy = {
---@type { frontend:string, backend:string | string[] } fallback strategy for mode
default = {
frontend = 'hover',
@ -19,7 +18,7 @@ return {
},
},
---@type table frontend options
frontend = {
frontend = {
---@class TransFrontendOpts
---@field keymaps table<string, string>
default = {
@ -128,6 +127,7 @@ return {
-- },
-- },
-- local title = {
-- "████████╗██████╗ █████╗ ███╗ ██╗███████╗",
-- "╚══██╔══╝██╔══██╗██╔══██╗████╗ ██║██╔════╝",

View File

@ -4,7 +4,7 @@ return function()
local Trans = require('Trans')
-- INFO :Check ultimate.db exists
local dir = Trans.conf.dir
local path = dir .. '/ultimate.db'
local path = dir .. 'ultimate.db'
local fn = vim.fn
if fn.isdirectory(dir) == 0 then
@ -20,7 +20,8 @@ return function()
-- INFO :Download ultimate.db
local uri = 'https://github.com/skywind3000/ECDICT-ultimate/releases/download/1.0.0/ecdict-ultimate-sqlite.zip'
local zip = dir .. '/ultimate.zip'
local zip = dir .. 'ultimate.zip'
if fn.filereadable(zip) then os.remove(zip) end
local handle = function(output)
@ -43,7 +44,6 @@ return function()
vim.notify(debug_message, vim.log.ERROR)
end
Trans.curl.get(uri, {
output = zip,
callback = handle,
@ -51,6 +51,6 @@ return function()
-- INFO : Install tts dependencies
if fn.has('linux') == 0 and fn.has('mac') == 0 then
os.execute('cd ./tts/ && npm install')
os.execute('cd ./tts && npm install')
end
end

View File

@ -207,7 +207,6 @@ function M.list_concat(list, step)
return ret
end
---Get the field of the list
---@param list any[]
---@param field any
@ -220,6 +219,7 @@ function M.list_fields(list, field)
return ret
end
---@class Trans
---@field util TransUtil
return M

View File

@ -1,3 +1,4 @@
local Trans = require('Trans')
local health, fn = vim.health, vim.fn
local ok = health.report_ok
@ -55,7 +56,7 @@ local function check_binary_dependencies()
end
local function check_database()
local db_path = require("Trans").conf.dir .. "/ultimate.db"
local db_path = Trans.conf.dir .. Trans.separator .. "ultimate.db"
if fn.filereadable(db_path) == 1 then
ok([[ultimate database found ]])
else
@ -67,7 +68,7 @@ local function check_database()
end
local function check_configure_file()
local path = fn.expand(require("Trans").conf.dir .. "/Trans.json")
local path = fn.expand(Trans.conf.dir .. Trans.separator .. "Trans.json")
if not fn.filereadable(path) then
warn("Backend configuration file[%s] not found")
end

View File

@ -19,16 +19,28 @@ end
---@field play function @Use tts to play string
local separator = vim.loop.os_uname().sysname == "Windows" and "\\" or "/"
---@class Trans
---@field style table @Style module
---@field cache table<string, TransData> @Cache for translated data object
---@field modes string[] @all modes name
---@field plugin_dir string @Plugin directory
---@field separator string @Path separator
local M = metatable("core", {
cache = {},
style = metatable("style"),
augroup = vim.api.nvim_create_augroup("Trans", { clear = true }),
cache = {},
style = metatable("style"),
plugin_dir = debug.getinfo(1, "S").source:sub(2):match("(.-)lua" .. separator .. "Trans"),
separator = separator,
})
M.metatable = metatable
---Get abs_path of file
---@param path string[]
---@param is_dir boolean?
---@return string
function M.relative_path(path, is_dir)
return M.plugin_dir .. table.concat(path, separator) .. (is_dir and separator or "")
end
return M

View File

@ -1,24 +1,5 @@
local api, fn = vim.api, vim.fn
string.width = api.nvim_strwidth
--- INFO :Define string play method
if fn.has("linux") == 1 then
string.play = function(self)
local cmd = ([[echo %q | festival --tts]]):format(self)
fn.jobstart(cmd)
end
elseif fn.has("mac") == 1 then
string.play = function(self)
local cmd = ([[say %q]]):format(self)
fn.jobstart(cmd)
end
else
string.play = function(self)
local separator = fn.has("unix") and "/" or "\\"
local file = debug.getinfo(1, "S").source:sub(2):match("(.*)lua") .. separator .. "tts" .. separator .. "say.js"
fn.jobstart("node " .. file .. " " .. self)
end
end
--- INFO :Define plugin command
local Trans = require("Trans")
@ -40,3 +21,14 @@ command("TransPlay", function()
str:play()
end
end, { desc = " Auto play" })
string.width = api.nvim_strwidth
local f =
fn.has('linux') == 1 and ([[echo %q | festival --tts]])
or fn.has('mac') == 1 and ([[say %q]])
or 'node' .. Trans.relative_path { 'tts', 'say.js' } .. ' %q'
string.play = function(self)
fn.jobstart(f:format(self))
end