feat: add online query support
This commit is contained in:
@@ -72,18 +72,18 @@ return function(word)
|
||||
m_result = require('Trans.query.' .. engine_us)(word)
|
||||
|
||||
local opt = {
|
||||
relative = 'editor',
|
||||
width = float.width,
|
||||
height = float.height,
|
||||
border = float.border,
|
||||
title = float.title,
|
||||
row = bit.rshift((vim.o.lines - float.height), 1),
|
||||
col = bit.rshift((vim.o.columns - float.width), 1),
|
||||
zindex = 50,
|
||||
relative = 'editor',
|
||||
width = float.width,
|
||||
height = float.height,
|
||||
border = float.border,
|
||||
title = float.title,
|
||||
animation = float.animation,
|
||||
row = bit.rshift((vim.o.lines - float.height), 1),
|
||||
col = bit.rshift((vim.o.columns - float.width), 1),
|
||||
zindex = 50,
|
||||
}
|
||||
|
||||
m_window = require('Trans.window')(true, opt)
|
||||
m_window.animation = float.animation
|
||||
|
||||
set_title()
|
||||
m_content = m_window.contents[2]
|
||||
@@ -95,7 +95,6 @@ return function(word)
|
||||
set_tag_hl(engine_us, 'fail')
|
||||
end
|
||||
|
||||
m_window:draw()
|
||||
m_window:open()
|
||||
m_window:bufset('bufhidden', 'wipe')
|
||||
|
||||
|
@@ -1,5 +1,6 @@
|
||||
local api = vim.api
|
||||
local conf = require('Trans').conf
|
||||
local new_window = require('Trans.window')
|
||||
|
||||
local m_window
|
||||
local m_result
|
||||
@@ -41,7 +42,6 @@ local process = {
|
||||
it(m_result.collins and icon.star:rep(m_result.collins) or icon.notfound, 'TransCollins'),
|
||||
it(m_result.oxford == 1 and icon.yes or icon.no)
|
||||
)
|
||||
|
||||
end
|
||||
m_content:addline(line)
|
||||
end,
|
||||
@@ -166,14 +166,6 @@ local process = {
|
||||
m_content:newline('')
|
||||
end
|
||||
end,
|
||||
|
||||
failed = function()
|
||||
m_content:addline(
|
||||
it(conf.icon.notfound .. m_indent .. '没有找到相关的翻译', 'TransFailed')
|
||||
)
|
||||
|
||||
m_window:set_width(m_content.lines[1]:width())
|
||||
end,
|
||||
}
|
||||
|
||||
|
||||
@@ -264,64 +256,134 @@ action = {
|
||||
end,
|
||||
}
|
||||
|
||||
local function handle()
|
||||
local hover = conf.hover
|
||||
if hover.auto_play then
|
||||
local ok = pcall(action.play)
|
||||
if not ok then
|
||||
vim.notify('自动发音失败, 请检查README发音部分', vim.log.WARN)
|
||||
end
|
||||
end
|
||||
|
||||
for _, field in ipairs(conf.order) do
|
||||
process[field]()
|
||||
end
|
||||
|
||||
for act, key in pairs(hover.keymap) do
|
||||
vim.keymap.set('n', key, action[act], { buffer = true, silent = true })
|
||||
end
|
||||
end
|
||||
|
||||
local function online_query(word)
|
||||
-- TODO :Progress Bar
|
||||
local wait = {}
|
||||
local size = 0
|
||||
for k, _ in pairs(conf.engine) do
|
||||
size = size + 1
|
||||
wait[size] = require('Trans.query.' .. k)(word)
|
||||
end
|
||||
local error_msg = conf.icon.notfound .. ' 没有找到相关的翻译'
|
||||
|
||||
m_window:set_height(1)
|
||||
local width = m_window.width
|
||||
m_window:set_width(error_msg:width())
|
||||
if size == 0 then
|
||||
m_content:addline(it(error_msg, 'TransFailed'))
|
||||
m_window:open()
|
||||
return
|
||||
end
|
||||
|
||||
m_window:open()
|
||||
|
||||
local timeout = conf.hover.timeout
|
||||
local interval = math.floor(timeout / m_window.width)
|
||||
|
||||
-- --- char: ■ | □ | ▇ | ▏ ▎ ▍ ▌ ▋ ▊ ▉ █
|
||||
-- --- ◖■■■■■■■◗▫◻ ▆ ▆ ▇⃞ ▉⃞
|
||||
local cell = '▇'
|
||||
|
||||
local i = 1
|
||||
local do_progress
|
||||
do_progress = function()
|
||||
m_content:wipe()
|
||||
for j = 1, size do
|
||||
local res = wait[j]()
|
||||
if res then
|
||||
m_result = res
|
||||
m_window:set_width(width)
|
||||
handle()
|
||||
m_content:attach()
|
||||
|
||||
-- TODO :Animation
|
||||
m_window.height = m_content:actual_height(true)
|
||||
m_window:open {
|
||||
animation = 'fold',
|
||||
}
|
||||
return
|
||||
|
||||
elseif res == false then
|
||||
table.remove(wait, j)
|
||||
size = size - 1
|
||||
end
|
||||
end
|
||||
|
||||
if i == m_window.width or size == 0 then
|
||||
--- HACK : change framework
|
||||
m_content:addline(
|
||||
it(error_msg, 'TransFailed')
|
||||
)
|
||||
|
||||
m_content:attach()
|
||||
|
||||
else
|
||||
m_content:addline(
|
||||
it(cell:rep(i), 'MoreMsg')
|
||||
)
|
||||
i = i + 1
|
||||
m_content:attach()
|
||||
vim.defer_fn(do_progress, interval)
|
||||
end
|
||||
end
|
||||
|
||||
do_progress()
|
||||
end
|
||||
|
||||
return function(word)
|
||||
vim.validate {
|
||||
word = { word, 's' },
|
||||
}
|
||||
|
||||
m_result = require('Trans.query.offline')(word)
|
||||
local hover = conf.hover
|
||||
|
||||
m_window = require("Trans.window")(false, {
|
||||
relative = 'cursor',
|
||||
width = hover.width,
|
||||
height = hover.height,
|
||||
title = hover.title,
|
||||
border = hover.border,
|
||||
col = 1,
|
||||
row = 1,
|
||||
m_window = new_window(false, {
|
||||
relative = 'cursor',
|
||||
width = hover.width,
|
||||
height = hover.height,
|
||||
title = hover.title,
|
||||
border = hover.border,
|
||||
animation = hover.animation,
|
||||
col = 1,
|
||||
row = 1,
|
||||
})
|
||||
|
||||
m_window.animation = hover.animation
|
||||
m_content = m_window.contents[1]
|
||||
|
||||
m_result = require('Trans.query.offline')(word)
|
||||
if m_result then
|
||||
handle()
|
||||
m_window:open({
|
||||
callback = function()
|
||||
m_window:set('wrap', true)
|
||||
end,
|
||||
})
|
||||
|
||||
-- TODO :Progress Bar
|
||||
if not m_result then
|
||||
m_result = require('Trans.query.baidu')(word)
|
||||
end
|
||||
|
||||
if m_result and m_result.translation then
|
||||
if hover.auto_play then
|
||||
local ok = pcall(action.play)
|
||||
if not ok then
|
||||
vim.notify('自动发音失败, 请检查README发音部分', vim.log.WARN)
|
||||
end
|
||||
end
|
||||
|
||||
for _, field in ipairs(conf.order) do
|
||||
process[field]()
|
||||
end
|
||||
|
||||
for act, key in pairs(hover.keymap) do
|
||||
vim.keymap.set('n', key, action[act], { buffer = true, silent = true })
|
||||
local height = m_content:actual_height(true)
|
||||
if height < m_window.height then
|
||||
m_window:set_height(height)
|
||||
end
|
||||
else
|
||||
process.failed()
|
||||
online_query(word)
|
||||
end
|
||||
|
||||
m_window:draw()
|
||||
|
||||
local height = m_content:actual_height(true)
|
||||
if height < m_window.height then
|
||||
m_window:set_height(height)
|
||||
end
|
||||
|
||||
m_window:open(function()
|
||||
m_window:set('wrap', true)
|
||||
end)
|
||||
|
||||
-- Auto Close
|
||||
if hover.auto_close_events then
|
||||
cmd_id = api.nvim_create_autocmd(
|
||||
|
Reference in New Issue
Block a user