fix: use backend local strategy instead of global strategy

This commit is contained in:
JuanZoran
2023-03-16 11:49:26 +08:00
parent 978677696e
commit 9a2d3b4e0a
7 changed files with 210 additions and 124 deletions

View File

@@ -116,12 +116,20 @@ function M:wait()
buffer[1] = spinner[cur % size + 1] .. (cell):rep(cur)
buffer:add_highlight(1, 'TransWaitting')
pause(interval)
return cur == times
return cur < times
end
end
---FallBack window for no result
function M:fallback()
if not self.window then
self:init_window {
height = 1,
width = self.opts.width,
}
end
local buffer = self.buffer
local opts = self.opts
buffer:wipe()
@@ -153,34 +161,35 @@ end
---Display Result in hover window
---@param data TransData
---@param result TransResult
---@overload fun(result:TransResult)
function M:process(data, result)
function M:process(data)
if self.pin then return end
local result, name = data:get_available_result()
if not result then
self:fallback()
return
end
local opts = self.opts
if opts.auto_play then
(data.from == 'en' and data.str or result.definition[1]):play()
end
-- local node = Trans.util.node
-- local it, t, f = node.item, node.text, node.format
-- self.buffer:setline(it('hello', 'MoreMsg'))
local opts = self.opts
if self.pin then return end
local buffer = self.buffer
if not buffer:is_valid() then buffer:init() end
buffer:deleteline(1)
if opts.auto_play then
(data.from == 'en' and data.str or result.definition[1]):play()
if not buffer:is_valid() then
buffer:init()
else
buffer:wipe()
end
for _, field in ipairs(opts.order) do
if result[field] then
self:load(result, field)
end
end
---@cast name string
self:load(result, name, opts.order[name])
local window = self.window
local display_size = Trans.util.display_size(buffer:lines(), opts.width)
local window = self.window
if window and window:is_valid() then
if opts.auto_resize then
display_size.width = math.min(opts.width, display_size.width + opts.padding)

View File

@@ -1,5 +1,6 @@
local node = require('Trans').util.node
local it, t, f = node.item, node.text, node.format
local interval = (' '):rep(4)
local function conjunction(text)
return {
@@ -9,44 +10,89 @@ local function conjunction(text)
}
end
local interval = (' '):rep(4)
---@type table<string, fun(hover:TransHover, result: TransResult)>
local default = {
str = function(hover, result)
-- TODO :
hover.buffer:setline(it(result.str, 'TransWord'))
end,
translation = function(hover, result)
local translation = result.translation
if not translation then return end
local strategy = {
title = function(hover, title)
if type(title) == 'string' then
hover.buffer:setline(it(title, 'TransWord'))
return
local buffer = hover.buffer
buffer:setline(conjunction('中文翻译'))
for _, value in ipairs(translation) do
buffer:setline(
it(interval .. value, 'TransTranslation')
)
end
local icon = hover.opts.icon
buffer:setline('')
end,
definition = function(hover, result)
local definition = result.definition
if not definition then return end
local buffer = hover.buffer
buffer:setline(conjunction('英文注释'))
for _, value in ipairs(definition) do
buffer:setline(
it(interval .. value, 'TransDefinition')
)
end
buffer:setline('')
end,
}
---@diagnostic disable-next-line: assign-type-mismatch
default.__index = default
local strategy = setmetatable({}, {
__index = function(tbl, key)
tbl[key] = default
return tbl[key]
end,
__newindex = function(tbl, key, value)
rawset(tbl, key, setmetatable(value, default))
end
})
strategy.offline = {
title = function(hover, result)
local title = result.title
if not title then return end
local icon = hover.opts.icon
local word = title.word
local oxford = title.oxford
local collins = title.collins
local phonetic = title.phonetic
if not phonetic and not collins and not oxford then
hover.buffer:setline(it(word, 'TransWord'))
else
hover.buffer:setline(f {
width = hover.opts.width,
text = t {
it(word, 'TransWord'),
t {
it('['),
it((phonetic and phonetic ~= '') and phonetic or icon.notfound, 'TransPhonetic'),
it(']')
},
it(collins and icon.star:rep(collins) or icon.notfound, 'TransCollins'),
it(oxford == 1 and icon.yes or icon.no)
hover.buffer:setline(f {
width = hover.opts.width,
text = t {
it(word, 'TransWord'),
t {
it('['),
it((phonetic and phonetic ~= '') and phonetic or icon.notfound, 'TransPhonetic'),
it(']')
},
})
end
it(collins and icon.star:rep(collins) or icon.notfound, 'TransCollins'),
it(oxford == 1 and icon.yes or icon.no)
},
})
end,
tag = function(hover, tag)
tag = function(hover, result)
local tag = result.tag
if not tag then return end
local buffer = hover.buffer
buffer:setline(conjunction('标签'))
@@ -65,7 +111,10 @@ local strategy = {
buffer:setline('')
end,
exchange = function(hover, exchange)
exchange = function(hover, result)
local exchange = result.exchange
if not exchange then return end
local buffer = hover.buffer
buffer:setline(conjunction('词形变化'))
@@ -77,7 +126,10 @@ local strategy = {
buffer:setline('')
end,
pos = function(hover, pos)
pos = function(hover, result)
local pos = result.pos
if not pos then return end
local buffer = hover.buffer
buffer:setline(conjunction('词性'))
@@ -87,39 +139,20 @@ local strategy = {
)
end
buffer:setline('')
end,
translation = function(hover, translation)
local buffer = hover.buffer
buffer:setline(conjunction('中文翻译'))
for _, value in ipairs(translation) do
buffer:setline(
it(interval .. value, 'TransTranslation')
)
end
buffer:setline('')
end,
definition = function(hover, definition)
local buffer = hover.buffer
buffer:setline(conjunction('英文注释'))
for _, value in ipairs(definition) do
buffer:setline(
it(interval .. value, 'TransDefinition')
)
end
buffer:setline('')
end,
}
-- FIXME :
---@class TransHover
---@field load fun(hover: TransHover, result: TransResult, field: string)
return function(hover, result, field)
strategy[field](hover, result[field])
---@field load fun(hover: TransHover, result: TransResult, name: string, order: string[])
return function(hover, result, name, order)
order = order or { 'str', 'translation', 'definition' }
local method = strategy[name]
for _, field in ipairs(order) do
method[field](hover, result)
end
end