diff --git a/lua/Trans/core/buffer.lua b/lua/Trans/core/buffer.lua index fb4dbae..3ca8b6f 100644 --- a/lua/Trans/core/buffer.lua +++ b/lua/Trans/core/buffer.lua @@ -2,7 +2,7 @@ local api, fn = vim.api, vim.fn ---@class TransBuffer ---@field bufnr integer buffer handle ----@field [number] string buffer[line] content +---@field [number] string|TransNode? buffer[line] content local buffer = {} -- INFO : corountine can't invoke C function diff --git a/lua/Trans/core/frontend.lua b/lua/Trans/core/frontend.lua index f4c3396..4408825 100644 --- a/lua/Trans/core/frontend.lua +++ b/lua/Trans/core/frontend.lua @@ -7,7 +7,7 @@ local frontend_opts = conf.frontend ---@field opts TransFrontendOpts ---@field get_active_instance fun():TransFrontend? ---@field process fun(self: TransFrontend, data: TransData) ----@field wait fun(self: TransFrontend): fun() Update wait status +---@field wait fun(self: TransFrontend): fun(backend: TransBackend): boolean Update wait status ---@field execute fun(action: string) @Execute action for frontend instance ---@field fallback fun() @Fallback method when no result ---@field setup? fun() @Setup method for frontend [optional] **NOTE: This method will be called when frontend is first used** diff --git a/lua/Trans/core/translate.lua b/lua/Trans/core/translate.lua index bfb7d99..bf64453 100644 --- a/lua/Trans/core/translate.lua +++ b/lua/Trans/core/translate.lua @@ -60,7 +60,7 @@ local strategy = { local name = backend.name ---@cast backend TransBackend while result[name] == nil do - if not update() then break end + if not update(backend) then break end end if result[name] then return true end diff --git a/lua/Trans/frontend/hover/init.lua b/lua/Trans/frontend/hover/init.lua index 1ec4071..f77e5a8 100644 --- a/lua/Trans/frontend/hover/init.lua +++ b/lua/Trans/frontend/hover/init.lua @@ -91,22 +91,17 @@ function M:init_window(opts) local option = { buffer = self.buffer, animation = m_opts.animation, + win_opts = { + col = opts.col or 1, + row = opts.row or 1, + width = opts.width or m_opts.width, + height = opts.height or m_opts.height, + relative = opts.relative or 'cursor', + title = m_opts.title, + title_pos = m_opts.title and 'center' or nil, + }, } - local win_opts = { - col = opts.col or 1, - row = opts.row or 1, - title = m_opts.title, - width = opts.width or m_opts.width, - height = opts.height or m_opts.height, - relative = opts.relative or 'cursor', - } - - if win_opts.title then - win_opts.title_pos = 'center' - end - - option.win_opts = win_opts self.window = Trans.window.new(option) return self.window end @@ -120,29 +115,30 @@ function M:icon_format(format) end ---Get Check function for waiting ----@return function +---@return fun(backend: TransBackend): boolean function M:wait() - local cur = 0 + local util = Trans.util local opts = self.opts local buffer = self.buffer - local times = opts.width - local pause = Trans.util.pause + local pause = util.pause local cell = opts.icon.cell local spinner = Trans.style.spinner[opts.spinner] + local times = opts.width - spinner[1]:width() local size = #spinner local interval = math.floor(opts.timeout / opts.width) self:init_window { - height = 1, - width = times, + height = 2, + width = opts.width, } - - -- FIXME : add proper args - return function() + local cur = 0 + local pr = util.node.prompt + local it = util.node.item + return function(backend) cur = cur + 1 - buffer[1] = spinner[cur % size + 1] .. (cell):rep(cur) - buffer:add_highlight(1, 'TransWaitting') + buffer[1] = pr(backend.name_zh) + buffer[2] = it { spinner[cur % size + 1] .. (cell):rep(cur), 'TransWaitting' } pause(interval) return cur < times end @@ -150,22 +146,21 @@ end ---FallBack window for no result function M:fallback() + local opts = self.opts + local fallback_msg = self:icon_format(opts.fallback_message) + + + local buffer = self.buffer + buffer:wipe() + buffer[1] = Trans.util.center(fallback_msg, opts.width) + buffer:add_highlight(1, 'TransFailed') if not self.window then self:init_window { - height = 1, + height = buffer:line_count(), width = self.opts.width, } end - local buffer = self.buffer - buffer:wipe() - - local opts = self.opts - local fallback_msg = self:icon_format(opts.fallback_message) - - -- TODO :Center - buffer[1] = Trans.util.center(fallback_msg, opts.width) - buffer:add_highlight(1, 'TransFailed') self:defer() end @@ -211,11 +206,7 @@ function M:process(data) -- vim.pretty_print(result) util.main_loop(function() - if not buffer:is_valid() then - buffer:init() - else - buffer:wipe() - end + buffer[buffer:is_valid() and 'wipe' or 'init'](buffer) ---@cast name string self:load(result, name, opts.order[name]) diff --git a/lua/Trans/frontend/hover/load.lua b/lua/Trans/frontend/hover/load.lua index 41aa9ac..7ae07c0 100644 --- a/lua/Trans/frontend/hover/load.lua +++ b/lua/Trans/frontend/hover/load.lua @@ -1,5 +1,5 @@ local node = require 'Trans'.util.node -local it, conjunction = node.item, node.conjunction +local it, pr = node.item, node.prompt local interval = (' '):rep(4) local M = setmetatable({}, { @@ -27,7 +27,7 @@ local default = { if not translation then return end local buffer = hover.buffer - buffer:setline(conjunction(hover.opts.icon.translation .. ' 中文翻译')) + buffer:setline(pr(hover.opts.icon.translation .. ' 中文翻译')) for _, value in ipairs(translation) do buffer:setline( @@ -42,7 +42,7 @@ local default = { if not definition then return end local buffer = hover.buffer - buffer:setline(conjunction(hover.opts.icon.definition .. ' 英文注释')) + buffer:setline(pr(hover.opts.icon.definition .. ' 英文注释')) for _, value in ipairs(definition) do buffer:setline( diff --git a/lua/Trans/frontend/hover/offline.lua b/lua/Trans/frontend/hover/offline.lua index 6a93510..b3f8850 100644 --- a/lua/Trans/frontend/hover/offline.lua +++ b/lua/Trans/frontend/hover/offline.lua @@ -1,7 +1,11 @@ local node = require 'Trans'.util.node -local it, t, f, co = node.item, node.text, node.format, node.conjunction +local it, t, f, co = node.item, node.text, node.format, node.prompt local interval = (' '):rep(4) + +---@class TransHover +---@field offline TransHoverRenderer + ---@type TransHoverRenderer local M = {} diff --git a/lua/Trans/frontend/hover/youdao.lua b/lua/Trans/frontend/hover/youdao.lua index 9a3112f..09ccb18 100644 --- a/lua/Trans/frontend/hover/youdao.lua +++ b/lua/Trans/frontend/hover/youdao.lua @@ -1,5 +1,5 @@ -local node = require('Trans').util.node -local it, t, f, co = node.item, node.text, node.format, node.conjunction +local node = require 'Trans'.util.node +local it, t, f, pr = node.item, node.text, node.format, node.prompt ---@type TransHoverRenderer local M = {} @@ -8,7 +8,7 @@ local interval = (' '):rep(4) function M.web(hover, result) if not result.web then return end local buffer = hover.buffer - buffer:setline(co(hover.opts.icon.web .. ' 网络释义')) + buffer:setline(pr(hover.opts.icon.web .. ' 网络释义')) local function remove_duplicate(strs) local uniq_strs = {} @@ -42,14 +42,14 @@ function M.web(hover, result) }) end end - buffer:setline('') + buffer:setline '' end function M.explains(hover, result) local explains = result.explains if not explains then return end local buffer = hover.buffer - buffer:setline(co('基本释义')) + buffer:setline(pr '基本释义') for i = 1, #explains, 2 do @@ -59,9 +59,9 @@ function M.explains(hover, result) 'TransExplains' }) end - buffer:setline('') + buffer:setline '' end -M.title = require('Trans').frontend.hover.offline.title +M.title = require 'Trans'.frontend.hover.offline.title return M diff --git a/lua/Trans/util/node.lua b/lua/Trans/util/node.lua index db12531..d163eee 100644 --- a/lua/Trans/util/node.lua +++ b/lua/Trans/util/node.lua @@ -91,7 +91,7 @@ return { item = item, text = text, format = format, - conjunction = function(str) + prompt = function(str) return { item { '', 'TransTitleRound' }, item { str, 'TransTitle' },