From 42cc99b36aa361403fb438426b6d1d8a4b11a803 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 6 Feb 2025 13:37:31 +0800 Subject: [PATCH] docs(nvim-plug): update default ui --- bundle/nvim-plug/README.md | 16 ++++++++--- bundle/nvim-plug/lua/plug/ui.lua | 46 +++++++++++++++++--------------- bundle/nvim-plug/test/ui.lua | 8 +++++- 3 files changed, 44 insertions(+), 26 deletions(-) diff --git a/bundle/nvim-plug/README.md b/bundle/nvim-plug/README.md index 93cd6e78bb00..aa0f17b91255 100644 --- a/bundle/nvim-plug/README.md +++ b/bundle/nvim-plug/README.md @@ -60,10 +60,20 @@ require('plug').add({ - `:PlugInstall`: install specific plugin +## Default UI + +The default is ui is inspired by [vundle](https://github.com/VundleVim/Vundle.vim) + +The default highlight group. + +| highlight group name | description | +| -------------------- | ------------------------------- | +| `PlugTitle` | the first line of plugin window | +| `PlugProcess` | the process of downloading | + ## Custom Plugin UI -The default is ui is inspired by [vundle](https://github.com/VundleVim/Vundle.vim), to setup custom UI, -you need to creat a on_update function, this function is called with two arges: +To setup custom UI, you need to creat a on_update function, this function is called with two arges: - name: `string` - date: `PlugUiData` @@ -72,7 +82,6 @@ you need to creat a on_update function, this function is called with two arges: | ------------ | ---------------------------------------- | | `clone_done` | boolead, is true when clone successfully | - ```lua --- your custom UI @@ -89,7 +98,6 @@ require('plug').setup({ }) ``` - ## Feedback The development of this plugin is in [`SpaceVim/bundle/nvim-plug`](https://github.com/SpaceVim/SpaceVim/tree/master/bundle/nvim-plug) directory. diff --git a/bundle/nvim-plug/lua/plug/ui.lua b/bundle/nvim-plug/lua/plug/ui.lua index b4ad518a81eb..c53dd4ef8d6f 100644 --- a/bundle/nvim-plug/lua/plug/ui.lua +++ b/bundle/nvim-plug/lua/plug/ui.lua @@ -11,9 +11,22 @@ local bufnr = -1 local winid = -1 local done = 0 local total = -1 -local weight = math.floor(vim.o.columns / 2) +local weight = 100 +local plugin_status = {} + +local function count_done(p) + done = 0 + for _, v in pairs(p) do + if v.clone_done then + done = done + 1 + end + end + return done +end local base = function() - weight = math.floor(vim.o.columns / 2) + total = #vim.tbl_keys(plugin_status) + done = count_done(plugin_status) + weight = vim.api.nvim_win_get_width(winid) - 10 return { 'plugins:(' .. done .. '/' .. total .. ')', '', @@ -25,21 +38,14 @@ local base = function() } end ---- @clase PluginStatus ---- @filed downloaded boolean ---- @filed download_process number 0 - 100 - -local plugin_status = {} - local function build_context() - total = #plugin_status local b = base() - for _, plug in ipairs(plugin_status) do - if type(plug.downloaded) == 'boolean' and plug.downloaded then - table.insert(b, '+ ' .. plug.name .. ' downloaded') + for k, plug in pairs(plugin_status) do + if plug.clone_done then + table.insert(b, '+ ' .. k .. ' downloaded') else - table.insert(b, '- ' .. plug.name .. string.format(' (%s%%)', plug.download_process)) + table.insert(b, '- ' .. k .. string.format(' (%s%%)', plug.clone_process)) end end @@ -58,9 +64,13 @@ M.open = function() if vim.api.nvim_buf_is_valid(bufnr) then vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, build_context()) end + --- setup highlight + vim.cmd('hi def link PlugTitle TODO') + vim.cmd('hi def link PlugProcess Repeat') + vim.fn.matchadd('PlugTitle', '', 2, -1, { window = winid }) + vim.fn.matchadd('PlugProcess', '^\\[\\zs=*', 2, -1, { window = winid }) end - --- @class PlugUiData --- Job 的消息推送到 UI manager --- install: @@ -73,13 +83,7 @@ end --- @param name string --- @param data PlugUiData M.on_update = function(name, data) - if not plugin_status[name] then - plugin_status[name] = { - downloaded = data.downloaded or false, - download_process = data.download_process or 0, - } - else - end + plugin_status[name] = vim.tbl_deep_extend('force', plugin_status[name] or {}, data) if vim.api.nvim_buf_is_valid(bufnr) then vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, build_context()) end diff --git a/bundle/nvim-plug/test/ui.lua b/bundle/nvim-plug/test/ui.lua index 443972f9b8da..7fad243d0f97 100644 --- a/bundle/nvim-plug/test/ui.lua +++ b/bundle/nvim-plug/test/ui.lua @@ -1,5 +1,11 @@ local ui = require('plug.ui') ui.open() ui.on_update('test.vim', { - downdloaded = true, + clone_done = true, +}) +ui.on_update('test2.vim', { + clone_process = '67', +}) +ui.on_update('test3.vim', { + clone_done = true, })