refactor: remove wrapper folder

This commit is contained in:
JuanZoran 2023-03-13 19:50:28 +08:00
parent 1fe20004ec
commit 7731c6c8cb
11 changed files with 74 additions and 79 deletions

View File

@ -5,9 +5,9 @@
- [TODO](#todo)
<!--toc:end-->
- [ ] Refactor query engine to 'Backend' and 'Frontend'
- [x] Refactor query engine to 'Backend' and 'Frontend'
- [x] Use `Trans.install` instead of `install.sh`
- [ ] Check if str is a word
- [ ] init frontend window
- [x] waitting animation
- [x] init frontend window
- [ ] build frontend window format logic
- [ ] waitting animation

View File

@ -56,7 +56,7 @@ function M.query(data)
end
Trans.wrapper.curl.get(M.uri, {
Trans.curl.get(M.uri, {
query = M.get_content(data),
callback = handle,
})

View File

@ -91,13 +91,14 @@ end
---Add highlight to buffer
---@param linenr number line number should be set[one index]
---@param col_start number column start
---@param col_start number column start [zero index]
---@param col_end number column end
---@param hl_group string highlight group
---@param ns number? highlight namespace
function buffer:add_highlight(linenr, col_start, col_end, hl_group, ns)
function buffer:add_highlight(linenr, hl_group, col_start, col_end, ns)
linenr = linenr and linenr - 1 or -1
api.nvim_buf_add_highlight(self.bufnr, ns or -1, hl_group, linenr, col_start, col_end)
col_start = col_start or 0
api.nvim_buf_add_highlight(self.bufnr, ns or -1, hl_group, linenr, col_start, col_end or -1)
end
---Calculate buffer content display height

42
lua/Trans/core/data.lua Normal file
View File

@ -0,0 +1,42 @@
local Trans = require('Trans')
local M = {}
M.__index = M
function M.new(opts)
local mode = opts.mode
local str = opts.str
local strategy = Trans.conf.strategy[mode]
local data = {
str = str,
mode = mode,
result = {},
}
data.frontend = Trans.frontend[strategy.frontend].new()
data.backend = {}
for i, name in ipairs(strategy.backend) do
data.backend[i] = Trans.backend[name]
end
if Trans.util.is_English(str) then
data.from = 'en'
data.to = 'zh'
else
data.from = 'zh'
data.to = 'en'
end
-- FIXME : Check if the str is a word
data.is_word = true
return setmetatable(data, M)
end
return M

View File

@ -36,7 +36,7 @@ return function()
end
Trans.wrapper.curl.get(uri, {
Trans.curl.get(uri, {
output = loc,
callback = handle,
})

View File

@ -13,41 +13,6 @@ local function init_opts(opts)
return opts
end
local function new_data(opts)
local mode = opts.mode
local str = opts.str
local strategy = Trans.conf.strategy[mode]
local data = {
str = str,
mode = mode,
result = {},
}
data.frontend = Trans.frontend[strategy.frontend].new()
data.backend = {}
for i, name in ipairs(strategy.backend) do
data.backend[i] = Trans.backend[name]
end
if util.is_English(str) then
data.from = 'en'
data.to = 'zh'
else
data.from = 'zh'
data.to = 'en'
end
-- FIXME : Check if the str is a word
data.is_word = true
return data
end
local function set_result(data)
-- HACK :Rewrite this function to support multi requests
local frontend = data.frontend
@ -80,9 +45,10 @@ local function process(opts)
end
local data = new_data(opts)
local data = Trans.data.new(opts)
set_result(data)
local success = false
for _, v in pairs(data.result) do
if type(v) == "table" then

View File

@ -54,6 +54,11 @@ function window:width()
return api.nvim_win_get_width(self.winid)
end
function window:highlight_line(linenr, highlight)
self.buffer:highlight_line(linenr, highlight, self.ns)
end
---Get window height
function window:height()
return api.nvim_win_get_height(self.winid)
@ -149,7 +154,6 @@ end
window.__index = window
local default_opts = {
ns = api.nvim_create_namespace('TransHoverWin'),
enter = false,
winid = -1,
win_opts = {
@ -174,20 +178,6 @@ end
return window
--@class win_opts
--@field buf buf buffer for attached
--@field height integer
--@field width integer
--@field col integer
--@field row integer
--@field border string
--@field title string | nil | table
--@field relative string
--@field ns integer namespace for highlight
--@field zindex? integer
--@field enter? boolean cursor should [enter] window
--@field animation table window animation
-- local ns = opts.ns
-- local buf = opts.buf
-- local col = opts.col

View File

@ -21,6 +21,7 @@ local Trans = require('Trans')
local M = Trans.metatable('frontend.hover', {
ns = vim.api.nvim_create_namespace('TransHoverWin'),
queue = {},
})
M.__index = M
@ -29,7 +30,7 @@ M.__index = M
---@return hover new_instance
function M.new()
local new_instance = {
buffer = Trans.wrapper.buffer.new(),
buffer = Trans.buffer.new(),
destroy_funcs = {},
}
M.queue[#M.queue + 1] = new_instance
@ -75,6 +76,7 @@ function M:init_window(opts)
local m_opts = self.opts
opts.ns = self.ns
opts.buffer = self.buffer
win_opts.col = 1
win_opts.row = 1
@ -89,7 +91,7 @@ function M:init_window(opts)
self.window = Trans.wrapper.window.new(opts)
self.window = Trans.window.new(opts)
return self.window
end
@ -98,8 +100,8 @@ end
---@param name string @key to be checked
---@param timeout number @timeout for waiting
function M:wait(tbl, name, timeout)
local msg = self.opts.fallback_message
local wid = msg:width()
local opts = self.opts
local width = opts.width
local spinner = Trans.style.spinner[self.opts.spinner]
local size = #spinner
local cell = self.opts.icon.cell
@ -112,15 +114,16 @@ function M:wait(tbl, name, timeout)
self:init_window({
win_opts = {
height = 1,
width = wid,
width = width,
}
})
local interval = math.floor(timeout / wid)
local interval = math.floor(timeout / width)
local pause = Trans.util.pause
for i = 1, wid do
for i = 1, width do
if tbl[name] ~= nil then break end
self.buffer[1] = update_text(i)
self.buffer:add_highlight(1, 'MoreMsg')
pause(interval)
end
@ -141,7 +144,6 @@ function M:is_available()
end
return M
-- local error_msg = conf.icon.notfound .. ' 没有找到相关的翻译'
-- local buffer = require('Trans.buffer')()

View File

@ -7,9 +7,7 @@ local function metatable(folder_name, origin)
__index = function(tbl, key)
local status, result = pcall(require, ('Trans.%s.%s'):format(folder_name, key))
if not status then
error('fail to load: ' .. key .. '\n' .. result)
end
if not status then error('fail to load: ' .. key .. '\n' .. result) end
tbl[key] = result
return result
@ -17,12 +15,13 @@ local function metatable(folder_name, origin)
})
end
local M = metatable('core')
M.metatable = metatable
M.style = metatable("style")
M.wrapper = metatable("wrapper")
M.cache = {}
M.cache = {}
return M

View File

@ -25,12 +25,7 @@ local text_meta = {
item_meta.__index = item_meta
text_meta.__index = function(self, key)
local res = text_meta[key]
if res then
return res
elseif key == 1 then
return table.concat(self.strs, self.step)
end
return text_meta[key] or (key == 1 and table.concat(self.strs, self.step) or nil)
end
return {