mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-02-19 13:13:43 +08:00
feat(nvim-plug): add default nvim-plug ui
This commit is contained in:
parent
e8eb3fb5e1
commit
1a1464149f
1
.ci/detach_plugin.sh
vendored
1
.ci/detach_plugin.sh
vendored
@ -153,6 +153,7 @@ main () {
|
|||||||
_detact_bundle $1 lua/plug/init.lua
|
_detact_bundle $1 lua/plug/init.lua
|
||||||
_detact_bundle $1 lua/plug/instealler.lua
|
_detact_bundle $1 lua/plug/instealler.lua
|
||||||
_detact_bundle $1 lua/plug/loader.lua
|
_detact_bundle $1 lua/plug/loader.lua
|
||||||
|
_detact_bundle $1 lua/plug/ui.lua
|
||||||
_checkdir test
|
_checkdir test
|
||||||
_detact_bundle $1 test/init.lua
|
_detact_bundle $1 test/init.lua
|
||||||
;;
|
;;
|
||||||
|
5
.github/workflows/async.yml
vendored
5
.github/workflows/async.yml
vendored
@ -1,6 +1,9 @@
|
|||||||
name: Detach Plugins
|
name: Detach Plugins
|
||||||
|
|
||||||
on: [push]
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- maser
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
check:
|
check:
|
||||||
|
12
bundle/nvim-plug/README.md
vendored
12
bundle/nvim-plug/README.md
vendored
@ -14,6 +14,7 @@ require('plug').setup({
|
|||||||
bundle_dir = 'D:/bundle_dir',
|
bundle_dir = 'D:/bundle_dir',
|
||||||
max_processes = 5, -- max number of processes used for nvim-plug job
|
max_processes = 5, -- max number of processes used for nvim-plug job
|
||||||
base_url = 'https://github.com',
|
base_url = 'https://github.com',
|
||||||
|
ui = 'notify', -- default ui is notify, use `default` for split window UI
|
||||||
})
|
})
|
||||||
|
|
||||||
require('plug').add({
|
require('plug').add({
|
||||||
@ -59,6 +60,17 @@ require('plug').add({
|
|||||||
|
|
||||||
- `:PlugInstall`: install specific plugin
|
- `:PlugInstall`: install specific plugin
|
||||||
|
|
||||||
|
## Custom Plugin UI
|
||||||
|
|
||||||
|
The default is ui is inspired by [vundle](https://github.com/VundleVim/Vundle.vim), to setup cutom ui, you need to creat a on_update function, this function is called with two arges:
|
||||||
|
|
||||||
|
- name: `string`
|
||||||
|
- date: `PlugUiData`
|
||||||
|
|
||||||
|
| key | description |
|
||||||
|
| ------------ | ---------------------------------------- |
|
||||||
|
| `clone_done` | boolead, is true when clone successfully |
|
||||||
|
|
||||||
## Feedback
|
## Feedback
|
||||||
|
|
||||||
The development of this plugin is in [`SpaceVim/bundle/nvim-plug`](https://github.com/SpaceVim/SpaceVim/tree/master/bundle/nvim-plug) directory.
|
The development of this plugin is in [`SpaceVim/bundle/nvim-plug`](https://github.com/SpaceVim/SpaceVim/tree/master/bundle/nvim-plug) directory.
|
||||||
|
2
bundle/nvim-plug/lua/plug/config.lua
vendored
2
bundle/nvim-plug/lua/plug/config.lua
vendored
@ -10,10 +10,12 @@ local M = {}
|
|||||||
M.bundle_dir = vim.fn.stdpath('data') .. '/bundle_dir'
|
M.bundle_dir = vim.fn.stdpath('data') .. '/bundle_dir'
|
||||||
M.max_processes = 5
|
M.max_processes = 5
|
||||||
M.base_url = 'https://github.com/'
|
M.base_url = 'https://github.com/'
|
||||||
|
M.ui = 'default'
|
||||||
function M.setup(opt)
|
function M.setup(opt)
|
||||||
M.bundle_dir = opt.bundle_dir or M.bundle_dir
|
M.bundle_dir = opt.bundle_dir or M.bundle_dir
|
||||||
M.max_processes = opt.max_processes or M.max_processes
|
M.max_processes = opt.max_processes or M.max_processes
|
||||||
M.base_url = opt.base_url or M.base_url
|
M.base_url = opt.base_url or M.base_url
|
||||||
|
M.ui = opt.ui or M.ui
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
29
bundle/nvim-plug/lua/plug/installer.lua
vendored
29
bundle/nvim-plug/lua/plug/installer.lua
vendored
@ -12,6 +12,14 @@ local notify = require('spacevim.api.notify')
|
|||||||
local jobs = {}
|
local jobs = {}
|
||||||
local config = require('plug.config')
|
local config = require('plug.config')
|
||||||
|
|
||||||
|
local on_uidate
|
||||||
|
|
||||||
|
if config.ui == 'default' then
|
||||||
|
on_uidate = require('plug.ui').on_update
|
||||||
|
elseif config.ui == 'notify' then
|
||||||
|
on_uidate = function(name, date) end
|
||||||
|
end
|
||||||
|
|
||||||
local processes = 0
|
local processes = 0
|
||||||
|
|
||||||
local installation_queue = {}
|
local installation_queue = {}
|
||||||
@ -36,9 +44,15 @@ local function build(plugSpec)
|
|||||||
end,
|
end,
|
||||||
on_exit = function(id, data, single)
|
on_exit = function(id, data, single)
|
||||||
if data == 0 and single == 0 then
|
if data == 0 and single == 0 then
|
||||||
notify.notify('Successfully build ' .. jobs['jobid_' .. id])
|
if config.ui == 'default' then
|
||||||
|
elseif config.ui == 'notify' then
|
||||||
|
notify.notify('Successfully build ' .. jobs['jobid_' .. id])
|
||||||
|
end
|
||||||
else
|
else
|
||||||
|
if config.ui == 'default' then
|
||||||
|
elseif config.ui == 'notify' then
|
||||||
notify.notify('failed to build ' .. jobs['jobid_' .. id])
|
notify.notify('failed to build ' .. jobs['jobid_' .. id])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
processes = processes - 1
|
processes = processes - 1
|
||||||
if #building_queue > 0 then
|
if #building_queue > 0 then
|
||||||
@ -57,9 +71,10 @@ local function install_plugin(plugSpec)
|
|||||||
return
|
return
|
||||||
elseif vim.fn.isdirectory(plugSpec.path) == 1 then
|
elseif vim.fn.isdirectory(plugSpec.path) == 1 then
|
||||||
-- if the directory exists, skip installation
|
-- if the directory exists, skip installation
|
||||||
|
on_uidate(plugSpec.name, { downloaded = true })
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local cmd = { 'git', 'clone', '--depth', '1' }
|
local cmd = { 'git', 'clone', '--depth', '1', '--progress' }
|
||||||
if plugSpec.branch then
|
if plugSpec.branch then
|
||||||
table.insert(cmd, '--branch')
|
table.insert(cmd, '--branch')
|
||||||
table.insert(cmd, plugSpec.branch)
|
table.insert(cmd, plugSpec.branch)
|
||||||
@ -70,10 +85,14 @@ local function install_plugin(plugSpec)
|
|||||||
|
|
||||||
table.insert(cmd, plugSpec.url)
|
table.insert(cmd, plugSpec.url)
|
||||||
table.insert(cmd, plugSpec.path)
|
table.insert(cmd, plugSpec.path)
|
||||||
|
on_uidate(plugSpec.name, { downloaded = false, download_process = 0 })
|
||||||
local jobid = job.start(cmd, {
|
local jobid = job.start(cmd, {
|
||||||
on_stdout = function(id, data)
|
on_stdout = function(id, data)
|
||||||
for _, v in ipairs(data) do
|
for _, v in ipairs(data) do
|
||||||
notify.notify(jobs['jobid_' .. id .. ':' .. v])
|
local status = vim.fn.matchstr(v, [[\d\+%\s(\d\+/\d\+)]])
|
||||||
|
if vim.fn.empty(status) == 1 then
|
||||||
|
on_uidate(plugSpec.name, { clone_process = status })
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
on_stderr = function(id, data)
|
on_stderr = function(id, data)
|
||||||
@ -83,12 +102,12 @@ local function install_plugin(plugSpec)
|
|||||||
end,
|
end,
|
||||||
on_exit = function(id, data, single)
|
on_exit = function(id, data, single)
|
||||||
if data == 0 and single == 0 then
|
if data == 0 and single == 0 then
|
||||||
notify.notify('Successfully installed ' .. jobs['jobid_' .. id])
|
on_uidate(plugSpec.name, { downloaded = true, download_process = 100 })
|
||||||
if plugSpec.build then
|
if plugSpec.build then
|
||||||
build(plugSpec)
|
build(plugSpec)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
notify.notify('failed to install ' .. jobs['jobid_' .. id])
|
on_uidate(plugSpec.name, { downloaded = false, download_process = 0 })
|
||||||
end
|
end
|
||||||
processes = processes - 1
|
processes = processes - 1
|
||||||
if #installation_queue > 0 then
|
if #installation_queue > 0 then
|
||||||
|
88
bundle/nvim-plug/lua/plug/ui.lua
vendored
Normal file
88
bundle/nvim-plug/lua/plug/ui.lua
vendored
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
--=============================================================================
|
||||||
|
-- ui.lua
|
||||||
|
-- Copyright 2025 Eric Wong
|
||||||
|
-- Author: Eric Wong < wsdjeg@outlook.com >
|
||||||
|
-- License: GPLv3
|
||||||
|
--=============================================================================
|
||||||
|
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
local bufnr = -1
|
||||||
|
local winid = -1
|
||||||
|
local done = 0
|
||||||
|
local total = -1
|
||||||
|
local weight = math.floor(vim.o.columns / 2)
|
||||||
|
local base = function()
|
||||||
|
weight = math.floor(vim.o.columns / 2)
|
||||||
|
return {
|
||||||
|
'plugins:(' .. done .. '/' .. total .. ')',
|
||||||
|
'',
|
||||||
|
'[' .. string.rep('=', math.floor(done / total * weight)) .. string.rep(
|
||||||
|
' ',
|
||||||
|
weight - math.floor(done / total * weight)
|
||||||
|
) .. ']',
|
||||||
|
'',
|
||||||
|
}
|
||||||
|
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')
|
||||||
|
else
|
||||||
|
table.insert(b, '- ' .. plug.name .. string.format(' (%s%%)', plug.download_process))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return b
|
||||||
|
end
|
||||||
|
|
||||||
|
M.open = function()
|
||||||
|
if not vim.api.nvim_buf_is_valid(bufnr) then
|
||||||
|
bufnr = vim.api.nvim_create_buf(false, true)
|
||||||
|
end
|
||||||
|
if not vim.api.nvim_win_is_valid(winid) then
|
||||||
|
winid = vim.api.nvim_open_win(bufnr, false, {
|
||||||
|
split = 'left',
|
||||||
|
})
|
||||||
|
end
|
||||||
|
if vim.api.nvim_buf_is_valid(bufnr) then
|
||||||
|
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, build_context())
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
--- @class PlugUiData
|
||||||
|
--- Job 的消息推送到 UI manager
|
||||||
|
--- install:
|
||||||
|
--- @filed clone_process string
|
||||||
|
--- @filed clone_done boolean
|
||||||
|
--- buile:
|
||||||
|
--- @filed building boolean
|
||||||
|
--- @filed clone_done boolean
|
||||||
|
|
||||||
|
--- @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
|
||||||
|
if vim.api.nvim_buf_is_valid(bufnr) then
|
||||||
|
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, build_context())
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
35
bundle/nvim-plug/plugin/plug.lua
vendored
35
bundle/nvim-plug/plugin/plug.lua
vendored
@ -5,20 +5,31 @@
|
|||||||
-- License: GPLv3
|
-- License: GPLv3
|
||||||
--=============================================================================
|
--=============================================================================
|
||||||
|
|
||||||
vim.api.nvim_create_user_command("PlugInstall", function(opt)
|
vim.api.nvim_create_user_command('PlugInstall', function(opt)
|
||||||
local plugs = {}
|
local plugs = {}
|
||||||
local all_plugins = require('plug').get()
|
local all_plugins = require('plug').get()
|
||||||
for _, v in ipairs(opt.fargs) do
|
if #opt.fargs == 0 then
|
||||||
local p = all_plugins[v]
|
require('plug.installer').install(all_plugins)
|
||||||
if p then
|
else
|
||||||
table.insert(plugs, p)
|
for _, v in ipairs(opt.fargs) do
|
||||||
|
local p = all_plugins[v]
|
||||||
|
if p then
|
||||||
|
table.insert(plugs, p)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
require('plug.installer').install(plugs)
|
||||||
end
|
end
|
||||||
require("plug.installer").install(plugs)
|
local c = require('plug.config')
|
||||||
end, { nargs = "*", complete = function()
|
if c.ui == 'default' then
|
||||||
local plug_name = {}
|
require('plug.ui').open()
|
||||||
for k, _ in pairs(require('plug').get()) do
|
|
||||||
table.insert(plug_name, k)
|
|
||||||
end
|
end
|
||||||
return plug_name
|
end, {
|
||||||
end })
|
nargs = '*',
|
||||||
|
complete = function()
|
||||||
|
local plug_name = {}
|
||||||
|
for k, _ in pairs(require('plug').get()) do
|
||||||
|
table.insert(plug_name, k)
|
||||||
|
end
|
||||||
|
return plug_name
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
1
bundle/nvim-plug/test/init.lua
vendored
1
bundle/nvim-plug/test/init.lua
vendored
@ -11,6 +11,7 @@ vim.opt.runtimepath:append('~/.SpaceVim')
|
|||||||
require('plug').setup({
|
require('plug').setup({
|
||||||
|
|
||||||
bundle_dir = 'D:/bundle_dir',
|
bundle_dir = 'D:/bundle_dir',
|
||||||
|
ui = 'default',
|
||||||
})
|
})
|
||||||
|
|
||||||
require('plug').add({
|
require('plug').add({
|
||||||
|
5
bundle/nvim-plug/test/ui.lua
vendored
Normal file
5
bundle/nvim-plug/test/ui.lua
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
local ui = require('plug.ui')
|
||||||
|
ui.open()
|
||||||
|
ui.on_update('test.vim', {
|
||||||
|
downdloaded = true,
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user