refactor: begin to try to refactor frontend part

This commit is contained in:
JuanZoran 2023-03-12 22:12:33 +08:00
parent f86ff7b615
commit 3c28af5314
6 changed files with 176 additions and 155 deletions

View File

@ -56,6 +56,7 @@ return {
'definition',
},
spinner = 'dots', -- see: /lua/Trans/style/spinner
fallback_message = '翻译超时或没有找到相关的翻译' -- TODO :support replace with {{special word}}
},
},
style = {

View File

@ -65,14 +65,6 @@ local function set_result(data)
end
local function render_window(data)
-- TODO :
vim.pretty_print(data)
print('begin to render window')
end
-- HACK : Core process logic
local function process(opts)
Trans.translate = coroutine.wrap(process)
@ -82,8 +74,8 @@ local function process(opts)
-- Find in cache
if Trans.cache[str] then
local data = Trans.cache[opts.str]
render_window(data)
local data = Trans.cache[str]
data.frontend:process(data)
return
end
@ -101,7 +93,7 @@ local function process(opts)
if success == false then return end
Trans.cache[data.str] = data
render_window(data)
data.frontend:process(data)
end
return coroutine.wrap(process)

View File

@ -1,120 +1,120 @@
local api = vim.api
local conf = require('Trans').conf
local buffer = require('Trans.buffer')()
-- local api = vim.api
-- local conf = require('Trans').conf
-- local buffer = require('Trans.buffer')()
local node = require("Trans.node")
local t = node.text
local it = node.item
local f = node.format
-- local node = require("Trans.node")
-- local t = node.text
-- local it = node.item
-- local f = node.format
local engine_map = {
baidu = '百度',
youdao = '有道',
iciba = 'iciba',
offline = '本地',
}
-- local engine_map = {
-- baidu = '百度',
-- youdao = '有道',
-- iciba = 'iciba',
-- offline = '本地',
-- }
local function set_tag_hl(name, status)
-- local hl = conf.float.tag[status]
-- m_window:set_hl(name, {
-- fg = '#000000',
-- bg = hl,
-- })
-- local function set_tag_hl(name, status)
-- local hl = conf.float.tag[status]
-- m_window:set_hl(name, {
-- fg = '#000000',
-- bg = hl,
-- })
-- m_window:set_hl(name .. 'round', {
-- fg = hl,
-- })
end
-- m_window:set_hl(name .. 'round', {
-- fg = hl,
-- })
-- end
local function set_title()
-- local title = m_window:new_content()
-- local github = ' https://github.com/JuanZoran/Trans.nvim'
-- local function set_title()
-- local title = m_window:new_content()
-- local github = ' https://github.com/JuanZoran/Trans.nvim'
-- title:addline(
-- title:center(it(github, '@text.uri'))
-- )
-- title:addline(
-- title:center(it(github, '@text.uri'))
-- )
-- local f = '%s(%d)'
-- local f = '%s(%d)'
-- local tags = {}
-- local load_tag = function(engine, index)
-- set_tag_hl(engine, 'wait')
-- local round = engine .. 'round'
-- table.insert(tags, t(
-- it('', round),
-- it(f:format(engine_map[engine], index), engine),
-- it('', round)
-- ))
-- end
-- load_tag('offline', 1)
-- title:addline(unpack(tags))
-- title:newline('')
end
-- local tags = {}
-- local load_tag = function(engine, index)
-- set_tag_hl(engine, 'wait')
-- local round = engine .. 'round'
-- table.insert(tags, t(
-- it('', round),
-- it(f:format(engine_map[engine], index), engine),
-- it('', round)
-- ))
-- end
-- load_tag('offline', 1)
-- title:addline(unpack(tags))
-- title:newline('')
-- end
local action = {
quit = function()
-- m_window:try_close()
end,
}
-- local action = {
-- quit = function()
-- -- m_window:try_close()
-- end,
-- }
local exist = function(str)
return str and str ~= ''
end
-- local exist = function(str)
-- return str and str ~= ''
-- end
local function process()
-- TODO :
-- local icon = conf.icon
-- m_content:addline(m_content:format {
-- nodes = {
-- it(m_result.word, 'TransWord'),
-- t(
-- it('['),
-- it(exist(m_result.phonetic) and m_result.phonetic or icon.notfound, 'TransPhonetic'),
-- it(']')
-- ),
-- 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)
-- },
-- width = math.floor(m_window.width * 0.5)
-- })
-- m_content:addline(it('该窗口还属于实验性功能 .... '))
end
-- local function process()
-- TODO :
-- local icon = conf.icon
-- m_content:addline(m_content:format {
-- nodes = {
-- it(m_result.word, 'TransWord'),
-- t(
-- it('['),
-- it(exist(m_result.phonetic) and m_result.phonetic or icon.notfound, 'TransPhonetic'),
-- it(']')
-- ),
-- 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)
-- },
-- width = math.floor(m_window.width * 0.5)
-- })
-- m_content:addline(it('该窗口还属于实验性功能 .... '))
-- end
return function(word)
buffer:init()
-- TODO :online query
-- local float = conf.float
vim.notify([[
[]:
float窗口目前还待开发
input查询功能, hover]])
-- local opt = {
-- 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 = 20,
-- }
-- m_window = require('Trans.window')(true, opt)
-- set_title()
-- m_content = m_window:new_content()
-- m_result = require('Trans.query.offline')(word)
-- if m_result then
-- set_tag_hl('offline', 'success')
-- process()
-- else
-- set_tag_hl('offline', 'fail')
-- end
-- return function(word)
-- buffer:init()
-- -- TODO :online query
-- -- local float = conf.float
-- vim.notify([[
-- [注意]:
-- float窗口目前还待开发
-- 如果需要input查询功能, 请将窗口改成hover]])
-- local opt = {
-- 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 = 20,
-- }
-- m_window = require('Trans.window')(true, opt)
-- set_title()
-- m_content = m_window:new_content()
-- m_result = require('Trans.query.offline')(word)
-- if m_result then
-- set_tag_hl('offline', 'success')
-- process()
-- else
-- set_tag_hl('offline', 'fail')
-- end
-- m_window:open()
-- m_window:bufset('bufhidden', 'wipe')
-- m_window:open()
-- m_window:bufset('bufhidden', 'wipe')
-- for act, key in pairs(float.keymap) do
-- m_window:map(key, action[act])
-- end
end
-- for act, key in pairs(float.keymap) do
-- m_window:map(key, action[act])
-- end
-- end

View File

@ -0,0 +1,27 @@
local strategy = {
play = function(self)
print('TODO: play')
end,
pageup = function()
print('TODO: pageup')
end,
pagedown = function()
print('TODO: pagedown')
end,
pin = function()
print('TODO: pin')
end,
close = function()
print('TODO: close')
end,
toggle_entry = function()
print('TODO: toggle_entry')
end,
}
return function(self, action)
-- TODO :
strategy[action](self)
end

View File

@ -1,29 +1,54 @@
local M = {}
local Trans = require('Trans')
local style = Trans.conf.style
local M = Trans.metatable('frontend.hover')
M.queue = {}
M.__index = M
function M.new(data)
return setmetatable({
function M.new()
local new_instance = {
buffer = Trans.wrapper.buffer.new(),
}, M)
}
M.queue[#M.queue + 1] = new_instance
return setmetatable(new_instance, M)
end
function M.get_active_instance()
-- TODO :
M.clear_dead_instance()
return M.queue[1]
end
function M.clear_dead_instance()
-- TODO :
for i = #M.queue, 1, -1 do
if not M.queue[i]:is_available() then
table.remove(M.queue, i)
end
end
end
function M.new_window()
end
function M:wait(tbl, name, timeout)
local error_message = 'Faild'
local interval = math.floor(timeout / #error_message)
for i = 1, #error_message do
local msg = self.opts.fallback_message
local wid = msg:width()
local spinner = Trans.style.spinner[self.opts.spinner]
local size = #spinner
local function update_text(times)
return spinner[times % size + 1] .. ('.'):rep(times)
end
local interval = math.floor(timeout / wid)
for i = 1, wid do
if tbl[name] ~= nil then break end
print('waitting' .. ('.'):rep(i))
print(update_text(i))
Trans.util.pause(interval)
end
@ -31,42 +56,17 @@ function M:wait(tbl, name, timeout)
end
function M:process(data)
vim.pretty_print(data.result)
print('TODO: process data')
end
function M:is_available()
return true
end
function M:execute(action)
-- M.actions = {
-- play = function()
-- print('TODO: play')
-- end,
-- pageup = function()
-- print('TODO: pageup')
-- end,
-- pagedown = function()
-- print('TODO: pagedown')
-- end,
-- pin = function()
-- print('TODO: pin')
-- end,
-- close = function()
-- print('TODO: close')
-- end,
-- toggle_entry = function()
-- print('TODO: toggle_entry')
-- end,
-- }
return self.buffer:is_valid() and self.window:is_valid()
end
return M
-- local hover = conf.hover
-- local error_msg = conf.icon.notfound .. ' 没有找到相关的翻译'
-- local buffer = require('Trans.buffer')()
-- local node = require('Trans.node')

View File

@ -1,5 +1,6 @@
local api, fn = vim.api, vim.fn
string.width = api.nvim_strwidth
--- INFO :Define string play method
if fn.has('linux') == 1 then
string.play = function(self)