mirror of
https://github.com/folke/snacks.nvim
synced 2025-08-05 19:28:24 +00:00
feat(dashboard): added Snacks.dashboard.update(). Closes #121
This commit is contained in:
parent
cf72c06ee6
commit
c770ebeaf7
3 changed files with 43 additions and 10 deletions
|
@ -34,6 +34,7 @@ Table of Contents *snacks-dashboard-table-of-contents*
|
||||||
- Snacks.dashboard.sections.startup()|snacks-dashboard-module-snacks.dashboard.sections.startup()|
|
- Snacks.dashboard.sections.startup()|snacks-dashboard-module-snacks.dashboard.sections.startup()|
|
||||||
- Snacks.dashboard.sections.terminal()|snacks-dashboard-module-snacks.dashboard.sections.terminal()|
|
- Snacks.dashboard.sections.terminal()|snacks-dashboard-module-snacks.dashboard.sections.terminal()|
|
||||||
- Snacks.dashboard.setup()|snacks-dashboard-module-snacks.dashboard.setup()|
|
- Snacks.dashboard.setup()|snacks-dashboard-module-snacks.dashboard.setup()|
|
||||||
|
- Snacks.dashboard.update()|snacks-dashboard-module-snacks.dashboard.update()|
|
||||||
8. Links |snacks-dashboard-links|
|
8. Links |snacks-dashboard-links|
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
|
@ -601,6 +602,15 @@ Check if the dashboard should be opened
|
||||||
Snacks.dashboard.setup()
|
Snacks.dashboard.setup()
|
||||||
<
|
<
|
||||||
|
|
||||||
|
|
||||||
|
`Snacks.dashboard.update()` *Snacks.dashboard.update()*
|
||||||
|
|
||||||
|
Update the dashboard
|
||||||
|
|
||||||
|
>lua
|
||||||
|
Snacks.dashboard.update()
|
||||||
|
<
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
8. Links *snacks-dashboard-links*
|
8. Links *snacks-dashboard-links*
|
||||||
|
|
||||||
|
|
|
@ -536,3 +536,11 @@ Check if the dashboard should be opened
|
||||||
```lua
|
```lua
|
||||||
Snacks.dashboard.setup()
|
Snacks.dashboard.setup()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### `Snacks.dashboard.update()`
|
||||||
|
|
||||||
|
Update the dashboard
|
||||||
|
|
||||||
|
```lua
|
||||||
|
Snacks.dashboard.update()
|
||||||
|
```
|
||||||
|
|
|
@ -194,6 +194,7 @@ Snacks.util.set_hl(links, { prefix = "SnacksDashboard", default = true })
|
||||||
---@field col? number
|
---@field col? number
|
||||||
---@field panes? snacks.dashboard.Item[][]
|
---@field panes? snacks.dashboard.Item[][]
|
||||||
---@field lines? string[]
|
---@field lines? string[]
|
||||||
|
---@field augroup integer
|
||||||
local D = {}
|
local D = {}
|
||||||
|
|
||||||
---@param opts? snacks.dashboard.Opts
|
---@param opts? snacks.dashboard.Opts
|
||||||
|
@ -203,9 +204,10 @@ function M.open(opts)
|
||||||
self.opts = Snacks.config.get("dashboard", defaults, opts) --[[@as snacks.dashboard.Opts]]
|
self.opts = Snacks.config.get("dashboard", defaults, opts) --[[@as snacks.dashboard.Opts]]
|
||||||
self.buf = self.opts.buf or vim.api.nvim_create_buf(false, true)
|
self.buf = self.opts.buf or vim.api.nvim_create_buf(false, true)
|
||||||
self.win = self.opts.win or Snacks.win({ style = "dashboard", buf = self.buf, enter = true }).win --[[@as number]]
|
self.win = self.opts.win or Snacks.win({ style = "dashboard", buf = self.buf, enter = true }).win --[[@as number]]
|
||||||
|
self.augroup = vim.api.nvim_create_augroup("snacks_dashboard", { clear = true })
|
||||||
self:init()
|
self:init()
|
||||||
self:update()
|
self:update()
|
||||||
self:fire("Opened")
|
self.fire("Opened")
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -225,6 +227,7 @@ function D:init()
|
||||||
end
|
end
|
||||||
vim.keymap.set("n", "q", "<cmd>bd<cr>", { silent = true, buffer = self.buf })
|
vim.keymap.set("n", "q", "<cmd>bd<cr>", { silent = true, buffer = self.buf })
|
||||||
vim.api.nvim_create_autocmd("WinResized", {
|
vim.api.nvim_create_autocmd("WinResized", {
|
||||||
|
group = self.augroup,
|
||||||
buffer = self.buf,
|
buffer = self.buf,
|
||||||
callback = function(ev)
|
callback = function(ev)
|
||||||
-- only re-render if the same window and size has changed
|
-- only re-render if the same window and size has changed
|
||||||
|
@ -236,9 +239,13 @@ function D:init()
|
||||||
vim.api.nvim_create_autocmd("BufWipeout", {
|
vim.api.nvim_create_autocmd("BufWipeout", {
|
||||||
buffer = self.buf,
|
buffer = self.buf,
|
||||||
callback = function()
|
callback = function()
|
||||||
self:fire("Closed")
|
self.fire("Closed")
|
||||||
|
vim.api.nvim_del_augroup_by_id(self.augroup)
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
self.on("Update", function()
|
||||||
|
self:update()
|
||||||
|
end, self.augroup)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@return {width:number, height:number}
|
---@return {width:number, height:number}
|
||||||
|
@ -485,12 +492,15 @@ function D:padding(item)
|
||||||
return item.padding and (type(item.padding) == "table" and item.padding or { item.padding, 0 }) or { 0, 0 }
|
return item.padding and (type(item.padding) == "table" and item.padding or { item.padding, 0 }) or { 0, 0 }
|
||||||
end
|
end
|
||||||
|
|
||||||
function D:fire(event)
|
function D.fire(event)
|
||||||
vim.api.nvim_exec_autocmds("User", { pattern = "SnacksDashboard" .. event, modeline = false })
|
vim.api.nvim_exec_autocmds("User", { pattern = "SnacksDashboard" .. event, modeline = false })
|
||||||
end
|
end
|
||||||
|
|
||||||
function D:on(event, cb)
|
---@param event string|string[]
|
||||||
vim.api.nvim_create_autocmd("User", { pattern = "SnacksDashboard" .. event, callback = cb })
|
---@param cb fun()
|
||||||
|
---@param group? string|integer
|
||||||
|
function D.on(event, cb, group)
|
||||||
|
vim.api.nvim_create_autocmd("User", { pattern = "SnacksDashboard" .. event, callback = cb, group = group })
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param pos {[1]:number, [2]:number}
|
---@param pos {[1]:number, [2]:number}
|
||||||
|
@ -634,7 +644,7 @@ function D:keys()
|
||||||
end
|
end
|
||||||
|
|
||||||
function D:update()
|
function D:update()
|
||||||
self:fire("UpdatePre")
|
self.fire("UpdatePre")
|
||||||
self._size = self:size()
|
self._size = self:size()
|
||||||
|
|
||||||
self.items = self:resolve(self.opts.sections)
|
self.items = self:resolve(self.opts.sections)
|
||||||
|
@ -670,7 +680,7 @@ function D:update()
|
||||||
vim.api.nvim_win_set_cursor(self.win, last)
|
vim.api.nvim_win_set_cursor(self.win, last)
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
self:fire("UpdatePost")
|
self.fire("UpdatePost")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Get an icon
|
-- Get an icon
|
||||||
|
@ -939,8 +949,8 @@ function M.sections.terminal(opts)
|
||||||
pcall(vim.fn.jobstop, jid)
|
pcall(vim.fn.jobstop, jid)
|
||||||
return true
|
return true
|
||||||
end)
|
end)
|
||||||
self:on("UpdatePre", close)
|
self.on("UpdatePre", close)
|
||||||
self:on("Closed", close)
|
self.on("Closed", close)
|
||||||
self:trace()
|
self:trace()
|
||||||
end,
|
end,
|
||||||
text = ("\n"):rep(height - 1),
|
text = ("\n"):rep(height - 1),
|
||||||
|
@ -1026,7 +1036,7 @@ function M.setup()
|
||||||
|
|
||||||
local options = { showtabline = vim.o.showtabline, laststatus = vim.o.laststatus }
|
local options = { showtabline = vim.o.showtabline, laststatus = vim.o.laststatus }
|
||||||
vim.o.showtabline, vim.o.laststatus = 0, 0
|
vim.o.showtabline, vim.o.laststatus = 0, 0
|
||||||
M.open({ buf = buf, win = wins[1] }):on("Closed", function()
|
M.open({ buf = buf, win = wins[1] }).on("Closed", function()
|
||||||
for k, v in pairs(options) do
|
for k, v in pairs(options) do
|
||||||
if vim.o[k] == 0 and v ~= 0 then
|
if vim.o[k] == 0 and v ~= 0 then
|
||||||
vim.o[k] = v
|
vim.o[k] = v
|
||||||
|
@ -1039,6 +1049,11 @@ function M.setup()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Update the dashboard
|
||||||
|
function M.update()
|
||||||
|
D.fire("Update")
|
||||||
|
end
|
||||||
|
|
||||||
function M.health()
|
function M.health()
|
||||||
if Snacks.config.dashboard.enabled then
|
if Snacks.config.dashboard.enabled then
|
||||||
if M.status.did_setup then
|
if M.status.did_setup then
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue