refactor: rewrite TransNode and use main_loop in process instead of buffer function

This commit is contained in:
JuanZoran
2023-03-23 09:52:44 +08:00
parent 8e9ccfc8f7
commit 84e06a268e
8 changed files with 135 additions and 262 deletions

View File

@ -185,24 +185,25 @@ function M:process(data)
self:fallback()
return
end
-- vim.pretty_print(result)
local opts = self.opts
local buffer = self.buffer
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 buffer = self.buffer
if not buffer:is_valid() then
buffer:init()
else
buffer:wipe()
end
-- vim.pretty_print(result)
Trans.util.main_loop(function()
if not buffer:is_valid() then
buffer:init()
else
buffer:wipe()
end
---@cast name string
self:load(result, name, opts.order[name])
---@cast name string
self:load(result, name, opts.order[name])
end)
local display_size = Trans.util.display_size(buffer:lines(), opts.width)
local window = self.window

View File

@ -20,7 +20,7 @@ local M = setmetatable({}, {
---@type TransHoverRenderer
local default = {
str = function(hover, result)
hover.buffer:setline(it(result.str, 'TransWord'))
hover.buffer:setline(it { result.str, 'TransWord' })
end,
translation = function(hover, result)
local translation = result.translation
@ -31,7 +31,7 @@ local default = {
for _, value in ipairs(translation) do
buffer:setline(
it(interval .. value, 'TransTranslation')
it { interval .. value, 'TransTranslation' }
)
end
@ -46,7 +46,7 @@ local default = {
for _, value in ipairs(definition) do
buffer:setline(
it(interval .. value, 'TransDefinition')
it { interval .. value, 'TransDefinition' }
)
end

View File

@ -9,7 +9,7 @@ function M.title(hover, result)
local title = result.title
if not title then return end
if type(title) == 'string' then
hover.buffer:setline(it(title, 'TransWord'))
hover.buffer:setline(it { title, 'TransWord' })
return
end
@ -22,18 +22,17 @@ function M.title(hover, result)
local phonetic = title.phonetic
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)
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 },
width = hover.opts.width,
})
end
@ -47,12 +46,12 @@ function M.tag(hover, result)
local size = #tag
for i = 1, size, 3 do
buffer:setline(it(
buffer:setline(it {
interval .. tag[i] ..
(tag[i + 1] and interval .. tag[i + 1] ..
(tag[i + 2] and interval .. tag[i + 2] or '') or ''),
'TransTag'
))
})
end
buffer:setline('')
@ -67,7 +66,7 @@ function M.exchange(hover, result)
for description, value in pairs(exchange) do
buffer:setline(
it(interval .. description .. interval .. value, 'TransExchange')
it { interval .. description .. interval .. value, 'TransExchange' }
)
end
@ -83,7 +82,7 @@ function M.pos(hover, result)
for description, value in pairs(pos) do
buffer:setline(
it(interval .. description .. interval .. value, 'TransPos')
it { interval .. description .. interval .. value, 'TransPos' }
)
end

View File

@ -30,16 +30,16 @@ function M.web(hover, result)
local indent = interval .. ' ' .. hover.opts.icon.list .. ' '
for _, w in ipairs(result.web) do
buffer:setline(it(
buffer:setline(it {
interval .. w.key,
'TransWeb'
))
})
for _, v in ipairs(remove_duplicate(w.value)) do
buffer:setline(it(
buffer:setline(it {
indent .. v,
'TransWeb'
))
})
end
end
buffer:setline('')
@ -53,11 +53,11 @@ function M.explains(hover, result)
for i = 1, #explains, 2 do
buffer:setline(it(
buffer:setline(it {
interval .. explains[i] ..
(explains[i + 1] and interval .. explains[i + 1] or ''),
'TransExplains'
))
})
end
buffer:setline('')
end