feat: add prompt for waitting window

This commit is contained in:
JuanZoran 2023-03-26 20:07:04 +08:00
parent 529a1639c6
commit c5216e2b37
8 changed files with 50 additions and 55 deletions

View File

@ -2,7 +2,7 @@ local api, fn = vim.api, vim.fn
---@class TransBuffer ---@class TransBuffer
---@field bufnr integer buffer handle ---@field bufnr integer buffer handle
---@field [number] string buffer[line] content ---@field [number] string|TransNode? buffer[line] content
local buffer = {} local buffer = {}
-- INFO : corountine can't invoke C function -- INFO : corountine can't invoke C function

View File

@ -7,7 +7,7 @@ local frontend_opts = conf.frontend
---@field opts TransFrontendOpts ---@field opts TransFrontendOpts
---@field get_active_instance fun():TransFrontend? ---@field get_active_instance fun():TransFrontend?
---@field process fun(self: TransFrontend, data: TransData) ---@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 execute fun(action: string) @Execute action for frontend instance
---@field fallback fun() @Fallback method when no result ---@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** ---@field setup? fun() @Setup method for frontend [optional] **NOTE: This method will be called when frontend is first used**

View File

@ -60,7 +60,7 @@ local strategy = {
local name = backend.name local name = backend.name
---@cast backend TransBackend ---@cast backend TransBackend
while result[name] == nil do while result[name] == nil do
if not update() then break end if not update(backend) then break end
end end
if result[name] then return true end if result[name] then return true end

View File

@ -91,22 +91,17 @@ function M:init_window(opts)
local option = { local option = {
buffer = self.buffer, buffer = self.buffer,
animation = m_opts.animation, animation = m_opts.animation,
} win_opts = {
local win_opts = {
col = opts.col or 1, col = opts.col or 1,
row = opts.row or 1, row = opts.row or 1,
title = m_opts.title,
width = opts.width or m_opts.width, width = opts.width or m_opts.width,
height = opts.height or m_opts.height, height = opts.height or m_opts.height,
relative = opts.relative or 'cursor', relative = opts.relative or 'cursor',
title = m_opts.title,
title_pos = m_opts.title and 'center' or nil,
},
} }
if win_opts.title then
win_opts.title_pos = 'center'
end
option.win_opts = win_opts
self.window = Trans.window.new(option) self.window = Trans.window.new(option)
return self.window return self.window
end end
@ -120,29 +115,30 @@ function M:icon_format(format)
end end
---Get Check function for waiting ---Get Check function for waiting
---@return function ---@return fun(backend: TransBackend): boolean
function M:wait() function M:wait()
local cur = 0 local util = Trans.util
local opts = self.opts local opts = self.opts
local buffer = self.buffer local buffer = self.buffer
local times = opts.width local pause = util.pause
local pause = Trans.util.pause
local cell = opts.icon.cell local cell = opts.icon.cell
local spinner = Trans.style.spinner[opts.spinner] local spinner = Trans.style.spinner[opts.spinner]
local times = opts.width - spinner[1]:width()
local size = #spinner local size = #spinner
local interval = math.floor(opts.timeout / opts.width) local interval = math.floor(opts.timeout / opts.width)
self:init_window { self:init_window {
height = 1, height = 2,
width = times, width = opts.width,
} }
local cur = 0
-- FIXME : add proper args local pr = util.node.prompt
return function() local it = util.node.item
return function(backend)
cur = cur + 1 cur = cur + 1
buffer[1] = spinner[cur % size + 1] .. (cell):rep(cur) buffer[1] = pr(backend.name_zh)
buffer:add_highlight(1, 'TransWaitting') buffer[2] = it { spinner[cur % size + 1] .. (cell):rep(cur), 'TransWaitting' }
pause(interval) pause(interval)
return cur < times return cur < times
end end
@ -150,22 +146,21 @@ end
---FallBack window for no result ---FallBack window for no result
function M:fallback() 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 if not self.window then
self:init_window { self:init_window {
height = 1, height = buffer:line_count(),
width = self.opts.width, width = self.opts.width,
} }
end 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() self:defer()
end end
@ -211,11 +206,7 @@ function M:process(data)
-- vim.pretty_print(result) -- vim.pretty_print(result)
util.main_loop(function() util.main_loop(function()
if not buffer:is_valid() then buffer[buffer:is_valid() and 'wipe' or 'init'](buffer)
buffer:init()
else
buffer:wipe()
end
---@cast name string ---@cast name string
self:load(result, name, opts.order[name]) self:load(result, name, opts.order[name])

View File

@ -1,5 +1,5 @@
local node = require 'Trans'.util.node 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 interval = (' '):rep(4)
local M = setmetatable({}, { local M = setmetatable({}, {
@ -27,7 +27,7 @@ local default = {
if not translation then return end if not translation then return end
local buffer = hover.buffer local buffer = hover.buffer
buffer:setline(conjunction(hover.opts.icon.translation .. ' 中文翻译')) buffer:setline(pr(hover.opts.icon.translation .. ' 中文翻译'))
for _, value in ipairs(translation) do for _, value in ipairs(translation) do
buffer:setline( buffer:setline(
@ -42,7 +42,7 @@ local default = {
if not definition then return end if not definition then return end
local buffer = hover.buffer local buffer = hover.buffer
buffer:setline(conjunction(hover.opts.icon.definition .. ' 英文注释')) buffer:setline(pr(hover.opts.icon.definition .. ' 英文注释'))
for _, value in ipairs(definition) do for _, value in ipairs(definition) do
buffer:setline( buffer:setline(

View File

@ -1,7 +1,11 @@
local node = require 'Trans'.util.node 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) local interval = (' '):rep(4)
---@class TransHover
---@field offline TransHoverRenderer
---@type TransHoverRenderer ---@type TransHoverRenderer
local M = {} local M = {}

View File

@ -1,5 +1,5 @@
local node = require('Trans').util.node local node = require 'Trans'.util.node
local it, t, f, co = node.item, node.text, node.format, node.conjunction local it, t, f, pr = node.item, node.text, node.format, node.prompt
---@type TransHoverRenderer ---@type TransHoverRenderer
local M = {} local M = {}
@ -8,7 +8,7 @@ local interval = (' '):rep(4)
function M.web(hover, result) function M.web(hover, result)
if not result.web then return end if not result.web then return end
local buffer = hover.buffer local buffer = hover.buffer
buffer:setline(co(hover.opts.icon.web .. ' 网络释义')) buffer:setline(pr(hover.opts.icon.web .. ' 网络释义'))
local function remove_duplicate(strs) local function remove_duplicate(strs)
local uniq_strs = {} local uniq_strs = {}
@ -42,14 +42,14 @@ function M.web(hover, result)
}) })
end end
end end
buffer:setline('') buffer:setline ''
end end
function M.explains(hover, result) function M.explains(hover, result)
local explains = result.explains local explains = result.explains
if not explains then return end if not explains then return end
local buffer = hover.buffer local buffer = hover.buffer
buffer:setline(co('基本释义')) buffer:setline(pr '基本释义')
for i = 1, #explains, 2 do for i = 1, #explains, 2 do
@ -59,9 +59,9 @@ function M.explains(hover, result)
'TransExplains' 'TransExplains'
}) })
end end
buffer:setline('') buffer:setline ''
end end
M.title = require('Trans').frontend.hover.offline.title M.title = require 'Trans'.frontend.hover.offline.title
return M return M

View File

@ -91,7 +91,7 @@ return {
item = item, item = item,
text = text, text = text,
format = format, format = format,
conjunction = function(str) prompt = function(str)
return { return {
item { '', 'TransTitleRound' }, item { '', 'TransTitleRound' },
item { str, 'TransTitle' }, item { str, 'TransTitle' },