Skip to content

Commit

Permalink
feat(sources): WIP preview entries in path source menus
Browse files Browse the repository at this point in the history
  • Loading branch information
willothy committed Sep 10, 2023
1 parent d9e2b24 commit ec3b03d
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions lua/dropbar/sources/path.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,113 @@ local function convert(path, buf, win)
jump = function(_)
vim.cmd.edit(path)
end,
preview = function(self)
if vim.uv.fs_stat(path).type == 'directory' then
-- TODO: preview directory entries
self.entry.menu.preview_buf = nil
self:preview_close_view()
return
end
self.entry.menu.preview_buf = vim.fn.bufnr(path)
if
self.entry.menu.preview_buf == nil
or self.entry.menu.preview_buf == -1
then
self.entry.menu.preview_buf = vim.fn.bufadd(path)
vim.api.nvim_exec_autocmds(
'BufReadPost',
{ buffer = self.entry.menu.preview_buf }
)
end
if
self.entry.menu == nil
or self.entry.menu.win == nil
or self.entry.menu.preview_buf == nil
then
if self.entry.menu then
self.entry.menu.preview_buf = nil
end
self:preview_close_view()
return
end
local function make_title()
local pat = vim.fs.normalize(
configs.eval(
configs.opts.sources.path.relative_to,
self.entry.menu.preview_buf
)
)
return {
{ icon, icon_hl },
{
vim.api
.nvim_buf_get_name(self.entry.menu.preview_buf)
:gsub('' .. pat .. '/', '')
:gsub('^' .. pat, ''), -- ':~'
'NormalFloat',
},
}
end
if
self.entry.menu.preview_float == nil
or vim.api.nvim_win_is_valid(self.entry.menu.preview_float) == false
then
self.entry.menu.preview_float =
vim.api.nvim_open_win(self.entry.menu.preview_buf, false, {
relative = 'editor',
style = 'minimal',
-- focusable = false,
width = math.min(80, math.floor(vim.o.columns / 2)),
height = math.min(25, math.floor(vim.o.lines / 2)),
row = vim.api.nvim_win_get_position(self.entry.menu.win)[1],
col = vim.api.nvim_win_get_position(self.entry.menu.win)[2]
+ vim.api.nvim_win_get_width(self.entry.menu.win)
+ 1,
border = {
{ ' ', 'NormalFloat' },
{ ' ', 'NormalFloat' },
{ ' ', 'NormalFloat' },
{ ' ', 'NormalFloat' },
{ ' ', 'NormalFloat' },
{ ' ', 'NormalFloat' },
{ ' ', 'NormalFloat' },
{ ' ', 'NormalFloat' },
},
title = make_title(),
})
vim.api.nvim_create_autocmd('BufLeave', {
buffer = self.entry.menu.buf,
callback = function()
self:preview_close_view()
end,
})
else
vim.api.nvim_win_set_buf(
self.entry.menu.preview_float,
self.entry.menu.preview_buf
)
local config =
vim.api.nvim_win_get_config(self.entry.menu.preview_float)
config.title = make_title()
vim.api.nvim_win_set_config(self.entry.menu.preview_float, config)
end
vim.api.nvim_win_set_cursor(self.entry.menu.preview_float, { 1, 0 })
vim.wo[self.entry.menu.preview_float].winbar = ''
vim.wo[self.entry.menu.preview_float].stc = ''
vim.wo[self.entry.menu.preview_float].signcolumn = 'no'
vim.wo[self.entry.menu.preview_float].number = false
end,
preview_restore_view = function(self) end,
preview_close_view = function(self)
if
self.entry.menu.preview_float
and vim.api.nvim_win_is_valid(self.entry.menu.preview_float)
then
vim.api.nvim_win_close(self.entry.menu.preview_float, true)
end
self.entry.menu.preview_float = nil
self.entry.menu.preview_buf = nil
end,
}, {
---@param self dropbar_symbol_t
__index = function(self, k)
Expand Down

0 comments on commit ec3b03d

Please sign in to comment.