fix: backup

This commit is contained in:
JuanZoran
2023-03-14 13:18:53 +08:00
parent 2c490994db
commit 4931bdc74a
9 changed files with 79 additions and 348 deletions

View File

@ -1,5 +1,7 @@
---@class buf
---@field bufnr integer buffer handle
---@type buf
local buffer = {}
local api, fn = vim.api, vim.fn
@ -36,10 +38,11 @@ function buffer:option(name)
end
---Destory buffer
function buffer:destory()
function buffer:destroy()
api.nvim_buf_delete(self.bufnr, { force = true })
end
---Set buffer load keymap
---@param key string
---@param operation function | string
@ -166,8 +169,9 @@ buffer.__index = function(self, key)
elseif type(key) == 'number' then
-- return fn.getbufoneline(self.bufnr, key) -- Vimscript Function Or Lua API ??
return api.nvim_buf_get_lines(self.bufnr, key - 1, key, true)[1]
else
error('invalid key' .. key)
error('invalid key: ' .. key)
end
end

View File

@ -51,17 +51,17 @@ function M.new(opts)
return setmetatable(data, M)
end
-- ---Get the first available result [return nil if no result]
-- ---@return table?
-- function M:get_available_result()
-- local result = self.result
-- local backend = self.backend
---Get the first available result [return nil if no result]
---@return table?
function M:get_available_result()
local result = self.result
local backend = self.backend
-- for _, name in ipairs(backend) do
-- if result[name] then
-- return result[name]
-- end
-- end
-- end
for _, name in ipairs(backend) do
if result[name] then
return result[name]
end
end
end
return M

View File

@ -10,6 +10,7 @@ local function set_frontend_keymap(frontend)
for action, key in pairs(frontend.opts.keymap) do
set('n', key, function()
local instance = frontend.get_active_instance()
if instance then
instance:execute(action)
else

View File

@ -47,8 +47,12 @@ local function process(opts)
-- Find in cache
if Trans.cache[str] then
local data = Trans.cache[str]
data.frontend:process(data)
return
local result = data:get_available_result()
if result then
data.frontend:process(data, result)
return
end
end

View File

@ -49,6 +49,8 @@ function M.get_str(mode)
end
---Puase coroutine for {ms} milliseconds
---@param ms integer milliseconds
function M.pause(ms)
local co = coroutine.running()
vim.defer_fn(function()
@ -57,6 +59,10 @@ function M.pause(ms)
coroutine.yield()
end
---Detect whether the string is English
---@param str string string to detect
---@return boolean
function M.is_English(str)
local char = { str:byte(1, -1) }
for i = 1, #str do

View File

@ -54,11 +54,6 @@ 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)
@ -70,8 +65,8 @@ end
---|'target'integer
function window:smooth_expand(opts)
local field = opts.field -- width | height
local from = self[field](self)
local to = opts.target
local from = api['nvim_win_get_' .. field](self.winid)
local to = opts.to
if from == to then return end
@ -92,6 +87,27 @@ function window:smooth_expand(opts)
self:set('wrap', wrap)
end
function M:resize(opts)
local width = opts[1]
local height = opts[2]
if self:width() ~= width then
self:smooth_expand {
field = 'width',
to = width
}
end
if self:height() ~= height then
self:smooth_expand {
field = 'height',
to = height
}
end
end
---Try to close window with animation?
function window:try_close()
local close_animation = self.animation.close
@ -103,7 +119,7 @@ function window:try_close()
self:smooth_expand({
field = field,
target = 1,
to = 1,
})
end
@ -132,23 +148,23 @@ function window:open()
self.winid = api.nvim_open_win(self.buffer.bufnr, self.enter, win_opts)
self:smooth_expand({
field = field,
target = to,
to = to,
})
else
self.winid = api.nvim_open_win(self.buffer.bufnr, self.enter, win_opts)
end
end
---buffer:addline() helper function
---@param node table
---@return table node formatted node
function window:center(node)
local text = node[1]
local width = text:width()
local win_width = self.width
local space = math.max(math.floor((win_width - width) / 2), 0)
node[1] = (' '):rep(space) .. text
return node
-- TODO :
print('TODO Center')
-- local text = node[1]
-- local width = text:width()
-- local win_width = self.width
-- local space = math.max(math.floor((win_width - width) / 2), 0)
-- node[1] = (' '):rep(space) .. text
-- return node
end
window.__index = window
@ -178,25 +194,6 @@ end
return window
-- local ns = opts.ns
-- local buf = opts.buf
-- local col = opts.col
-- local row = opts.row
-- local title = opts.title
-- local width = opts.width
-- local height = opts.height
-- local border = opts.border
-- local zindex = opts.zindex
-- local relative = opts.relative
-- local animation = opts.animation
-- local open = animation.open
-- local field = ({
-- slid = 'width',
-- fold = 'height',
-- })[open]
-- local win_opt = {
-- focusable = false,
-- style = 'minimal',