style(core.*, health.lua): better code style and backend strategy

This commit is contained in:
JuanZoran
2023-03-11 00:24:48 +08:00
parent 7fd7d1c309
commit 20fffe0ee5
12 changed files with 586 additions and 533 deletions

View File

@@ -1,20 +1,34 @@
local M = require('Trans')
local util = M.util
local Trans = require('Trans')
local util = Trans.util
local backends = {
'offline',
'baidu',
}
local process
process = function(opts)
local function new_data(opts)
opts = opts or {}
local mode = opts.mode or vim.api.nvim_get_mode().mode
local str = util.get_str(mode)
local method = opts.method or ({
n = 'normal',
v = 'visual',
})(vim.api.nvim_get_mode().mode)
local str = util.get_str(method)
if str == '' then return end
local strategy = Trans.conf.strategy[method] or Trans.conf.strategy
local data = {
str = str,
view = opts.view or M.conf.view[mode],
mode = mode,
method = method,
frontend = strategy.frontend,
}
local backend = strategy.backend
if type(backend) == 'string' then
backend = backend == '*' and backends or { backend }
end
data.backend = backend
if util.is_English(str) then
data.from = 'en'
data.to = 'zh'
@@ -22,8 +36,10 @@ process = function(opts)
data.from = 'zh'
data.to = 'en'
end
return data
end
local function set_result(data)
require('Trans.backend').baidu.query(data)
local thread = coroutine.running()
local resume = function()
@@ -38,8 +54,22 @@ process = function(opts)
coroutine.yield()
end
vim.pretty_print(data)
end
M.translate = coroutine.wrap(process)
local function render_window()
end
local function process(opts)
Trans.translate = coroutine.wrap(process)
local data = new_data(opts)
if not data then return end
set_result(data)
if data.result == false then return end
render_window()
end
return coroutine.wrap(process)