From 9d0aca954d84629fb3b784f6a4711cdb85a48bdc Mon Sep 17 00:00:00 2001 From: JuanZoran <1430359574@qq.com> Date: Sun, 12 Mar 2023 23:17:36 +0800 Subject: [PATCH] refactor: implement waitting window animation --- lua/Trans/frontend/hover/init.lua | 39 +++++++++++++++++++++++++++---- lua/Trans/wrapper/buffer.lua | 15 ++++++------ lua/Trans/wrapper/window.lua | 1 - 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/lua/Trans/frontend/hover/init.lua b/lua/Trans/frontend/hover/init.lua index 15ff64d..ddfe755 100644 --- a/lua/Trans/frontend/hover/init.lua +++ b/lua/Trans/frontend/hover/init.lua @@ -1,5 +1,4 @@ local Trans = require('Trans') -local style = Trans.conf.style local M = Trans.metatable('frontend.hover') @@ -25,13 +24,38 @@ end function M.clear_dead_instance() for i = #M.queue, 1, -1 do if not M.queue[i]:is_available() then + --- FIXME :Del Buffer or ... ? table.remove(M.queue, i) end end end -function M.new_window() +---Init hover window +---@param opts table @window options: width, height +---@return unknown +function M:init_window(opts) + opts = opts or {} + local win_opts = opts.win_opts or {} + opts.win_opts = win_opts + local m_opts = self.opts + + opts.buffer = self.buffer + win_opts.col = 1 + win_opts.row = 1 + win_opts.relative = 'cursor' + win_opts.title = m_opts.title + if win_opts.title then + win_opts.title_pos = 'center' + end + win_opts.width = win_opts.width or m_opts.width + win_opts.height = win_opts.height or m_opts.height + opts.animation = m_opts.animation + + + + self.window = Trans.wrapper.window.new(opts) + return self.window end function M:wait(tbl, name, timeout) @@ -44,12 +68,19 @@ function M:wait(tbl, name, timeout) return spinner[times % size + 1] .. ('.'):rep(times) end + self:init_window({ + win_opts = { + height = 1, + width = wid, + } + }) local interval = math.floor(timeout / wid) + local pause = Trans.util.pause for i = 1, wid do if tbl[name] ~= nil then break end - print(update_text(i)) - Trans.util.pause(interval) + self.buffer[1] = update_text(i) + pause(interval) end -- TODO : End waitting animation diff --git a/lua/Trans/wrapper/buffer.lua b/lua/Trans/wrapper/buffer.lua index decf965..a9c2027 100644 --- a/lua/Trans/wrapper/buffer.lua +++ b/lua/Trans/wrapper/buffer.lua @@ -130,13 +130,6 @@ function buffer:addline(nodes, index) end end -function buffer:init() - self.bufnr = api.nvim_create_buf(false, false) - self:set('filetype', 'Trans') - self:set('buftype', 'nofile') - self.size = 0 -end - ---@private buffer.__index = function(self, key) local res = buffer[key] @@ -158,10 +151,16 @@ end ---buffer constructor ---@return buf function buffer.new() - return setmetatable({ + local new_buf = setmetatable({ bufnr = -1, size = 0, }, buffer) + + + new_buf.bufnr = api.nvim_create_buf(false, false) + new_buf:set('filetype', 'Trans') + new_buf:set('buftype', 'nofile') + return new_buf end return buffer diff --git a/lua/Trans/wrapper/window.lua b/lua/Trans/wrapper/window.lua index 1e38e82..778022f 100644 --- a/lua/Trans/wrapper/window.lua +++ b/lua/Trans/wrapper/window.lua @@ -113,7 +113,6 @@ function window:set_hl(name, opts) end function window:open() - assert(self.winid == nil, 'window already opened') local win_opts = self.win_opts local open_animation = self.animation.open if open_animation then