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,97 +1,88 @@
local title
if vim.fn.has('nvim-0.9') == 1 then
title = {
{ '', 'TransTitleRound' },
{ ' Trans', 'TransTitle' },
{ '', 'TransTitleRound' },
}
end
return {
view = {
i = 'float',
n = 'hover',
v = 'hover',
},
hover = {
width = 37,
height = 27,
border = 'rounded',
title = vim.fn.has('nvim-0.9') == 1 and {
{ '', 'TransTitleRound' },
{ ' Trans', 'TransTitle' },
{ '', 'TransTitleRound' },
} or nil,
keymap = {
pageup = '[[',
pagedown = ']]',
pin = '<leader>[',
close = '<leader>]',
toggle_entry = '<leader>;',
play = '_',
},
animation = {
-- open = 'fold',
-- close = 'fold',
open = 'slid',
close = 'slid',
interval = 12,
},
auto_close_events = {
'InsertEnter',
'CursorMoved',
'BufLeave',
},
auto_play = true,
timeout = 2000,
spinner = 'dots', -- 查看所有样式: /lua/Trans/util/spinner
-- spinner = 'moon'
},
order = { -- only work on hover mode
'title',
'tag',
'pos',
'exchange',
'translation',
'definition',
},
icon = {
star = '',
notfound = '',
yes = '',
no = '',
-- --- char: ■ | □ | ▇ | ▏ ▎ ▍ ▌ ▋ ▊ ▉ █
-- --- ◖■■■■■■■◗▫◻ ▆ ▆ ▇⃞ ▉⃞
cell = '',
-- star = '⭐',
-- notfound = '❔',
-- yes = '✔️',
-- no = '❌'
},
theme = 'default',
theme = 'default', -- see lua/Trans/style/theme.lua
auto_play = true,
dir = vim.fn.expand('$HOME/.vim/dict'),
-- float = {
-- width = 0.8,
-- height = 0.8,
-- border = 'rounded',
-- keymap = {
-- quit = 'q',
-- },
-- animation = {
-- open = 'fold',
-- close = 'fold',
-- interval = 10,
-- },
-- tag = {
-- wait = '#519aba',
-- fail = '#e46876',
-- success = '#10b981',
-- },
-- },
strategy = {
frontend = 'hover',
backend = '*',
},
backend = {
timeout = 2000,
},
frontend = {
hover = {
title = title, -- need nvim-0.9
width = 37,
height = 27,
border = 'rounded',
keymap = {
pageup = '[[',
pagedown = ']]',
pin = '<leader>[',
close = '<leader>]',
toggle_entry = '<leader>;',
play = '_',
},
animation = {
open = 'slid', -- 'fold', 'slid'
close = 'slid',
interval = 12,
},
auto_close_events = {
'InsertEnter',
'CursorMoved',
'BufLeave',
},
order = {
'title',
'tag',
'pos',
'exchange',
'translation',
'definition',
},
spinner = 'dots', -- see: /lua/Trans/style/spinner
},
},
-- or use emoji
icon = {
star = '', -- ⭐
notfound = '', -- ❔
yes = '', -- ✔️
no = '', -- ❌
cell = '', -- ■ | □ | ▇ | ▏ ▎ ▍ ▌ ▋ ▊ ▉ █
},
}
-- ---Pasue Handler for {ms} milliseconds
-- ---@param ms number @milliseconds
-- M.pause = function(ms)
-- local co = coroutine.running()
-- vim.defer_fn(function()
-- coroutine.resume(co)
-- end, ms)
-- coroutine.yield()
-- end
-- TODO :
-- float = {
-- width = 0.8,
-- height = 0.8,
-- border = 'rounded',
-- keymap = {
-- quit = 'q',
-- },
-- animation = {
-- open = 'fold',
-- close = 'fold',
-- interval = 10,
-- },
-- tag = {
-- wait = '#519aba',
-- fail = '#e46876',
-- success = '#10b981',
-- },
-- },
-- local title = {
-- "████████╗██████╗ █████╗ ███╗ ██╗███████╗",
@ -101,5 +92,3 @@ return {
-- " ██║ ██║ ██║██║ ██║██║ ╚████║███████║",
-- " ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚══════╝",
--}
-- string.width = api.nvim_strwidth

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)

View File

@ -1,8 +1,7 @@
local M = {}
local fn, api = vim.fn, vim.api
M.get_select = function()
function M.get_select()
local _start = fn.getpos("v")
local _end = fn.getpos('.')
@ -33,24 +32,24 @@ M.get_select = function()
end
---Get Text which need to be translated
---@param mode string 'n' | 'v' | 'i'
---@param method string 'n' | 'v' | 'i'
---@return string
M.get_str = function(mode)
if mode == 'n' then
return fn.expand('<cword>')
elseif mode == 'v' then
api.nvim_input('<ESC>')
return M.get_select()
elseif mode == 'i' then
-- TODO Use Telescope with fuzzy finder
---@diagnostic disable-next-line: param-type-mismatch
return fn.input('请输入需要查询的单词:')
else
error('invalid mode: ' .. mode)
end
function M.get_str(method)
return ({
normal = function()
return fn.expand('<cword>')
end,
visual = function()
api.nvim_input('<ESC>')
return M.get_select()
end,
input = function()
return fn.input('请输入需要查询的单词:')
end,
})(method)()
end
M.is_English = function(str)
function M.is_English(str)
local char = { str:byte(1, -1) }
for i = 1, #str do
if char[i] > 128 then