fix: change global config to default option

This commit is contained in:
JuanZoran
2023-03-12 20:09:08 +08:00
parent 493fad6a3f
commit bc8c673ee0
10 changed files with 287 additions and 184 deletions

View File

@@ -1,8 +1,9 @@
local Trans = require('Trans')
local M = Trans.metatable('backend')
local conf = Trans.conf
--- INFO :Parse online engine keys config file
local path = Trans.conf.dir .. '/Trans.json'
local path = conf.dir .. '/Trans.json'
local file = io.open(path, "r")
if file then
@@ -14,19 +15,12 @@ if file then
end
local default_opts = vim.deepcopy(Trans.conf.backend)
for name, private_opts in pairs(result or {}) do
local opts = vim.tbl_extend('keep', Trans.conf.backend[name] or {}, default_opts, private_opts)
local backend = M[name]
local opts = vim.tbl_extend('keep', conf.backend[name] or {}, conf.backend.default, private_opts)
for k, v in pairs(opts) do
if not backend[k] then
backend[k] = v
end
M[name][k] = v
end
end
end
return M

View File

@@ -10,21 +10,27 @@ end
return {
dir = os.getenv('HOME') .. '/.vim/dict',
strategy = {
frontend = 'hover',
backend = '*',
default = {
frontend = 'hover',
backend = '*',
},
},
backend = {
timeout = 2000,
default = {
timeout = 2000,
},
},
frontend = {
auto_play = true,
border = 'rounded',
animation = {
open = 'slid', -- 'fold', 'slid'
close = 'slid',
interval = 12,
default = {
auto_play = true,
border = 'rounded',
animation = {
open = 'slid', -- 'fold', 'slid'
close = 'slid',
interval = 12,
},
title = title, -- need nvim-0.9
},
title = title, -- need nvim-0.9
hover = {
width = 37,
height = 27,

View File

@@ -0,0 +1,17 @@
local Trans = require('Trans')
local M = Trans.metatable('frontend')
-- local default_opts = vim.deepcopy(Trans.conf.frontend)
-- for name, private_opts in pairs(result or {}) do
-- local opts = vim.tbl_extend('keep', Trans.conf.backend[name] or {}, default_opts, private_opts)
-- local backend = M[name]
-- for k, v in pairs(opts) do
-- if not backend[k] then
-- backend[k] = v
-- end
-- end
-- end
return M

View File

@@ -13,27 +13,24 @@ local function set_strategy_opts(conf)
return backend
end
local global_strategy = conf.strategy
global_strategy.backend = parse_backend(global_strategy.backend)
local default_strategy = conf.strategy.default
default_strategy.backend = parse_backend(default_strategy.backend)
local meta = {
__index = function(tbl, key)
tbl[key] = global_strategy[key]
tbl[key] = default_strategy[key]
return tbl[key]
end
}
local strategy = conf.strategy
for _, mode in ipairs(all_modes) do
if not global_strategy[mode] then
global_strategy[mode] = setmetatable({}, meta)
else
if mode.backend then
mode.backend = parse_backend(mode.backend)
end
setmetatable(mode, meta)
strategy[mode] = setmetatable(strategy[mode] or {}, meta)
if type(strategy[mode].backend) == 'string' then
strategy[mode].backend = parse_backend(strategy[mode].backend)
end
end
end
@@ -102,4 +99,74 @@ return function(opts)
set_frontend_opts(conf)
define_keymaps(conf)
define_highlights(conf)
end
-- {
-- backend = {
-- default = {
-- timeout = 2000
-- }
-- },
-- dir = "/home/zoran/.vim/dict",
-- frontend = {
-- default = {
-- animation = {
-- close = "slid",
-- interval = 12,
-- open = "slid"
-- },
-- auto_play = true,
-- border = "rounded",
-- title = { { "", "TransTitleRound" }, { " Trans", "TransTitle" }, { "", "TransTitleRound" } }
-- },
-- hover = {
-- auto_close_events = { "InsertEnter", "CursorMoved", "BufLeave" },
-- height = 27,
-- keymap = {
-- close = "<leader>]",
-- pagedown = "]]",
-- pageup = "[[",
-- pin = "<leader>[",
-- play = "_",
-- toggle_entry = "<leader>;"
-- },
-- order = { "title", "tag", "pos", "exchange", "translation", "definition" },
-- spinner = "dots",
-- width = 37,
-- <metatable> = {
-- __index = <function 1>
-- }
-- }
-- },
-- strategy = {
-- default = {
-- backend = <1>{ "offline", "baidu" },
-- frontend = "hover"
-- },
-- input = {
-- backend = <table 1>,
-- <metatable> = <2>{
-- __index = <function 2>
-- }
-- },
-- normal = {
-- backend = <table 1>,
-- <metatable> = <table 2>
-- },
-- visual = {
-- backend = <table 1>,
-- <metatable> = <table 2>
-- }
-- },
-- style = {
-- icon = {
-- cell = "■",
-- no = "",
-- notfound = " ",
-- star = "",
-- yes = "✔"
-- },
-- theme = "default"
-- }
-- }

View File

@@ -48,6 +48,15 @@ function M.get_str(mode)
end
end
function M.pause(ms)
local co = coroutine.running()
vim.defer_fn(function()
coroutine.resume(co)
end, ms)
coroutine.yield()
end
function M.is_English(str)
local char = { str:byte(1, -1) }
for i = 1, #str do