diff --git a/lua/dropbar/sources/path.lua b/lua/dropbar/sources/path.lua index 323d6802..9a5429fe 100644 --- a/lua/dropbar/sources/path.lua +++ b/lua/dropbar/sources/path.lua @@ -53,6 +53,123 @@ local function convert(path, buf, win) jump = function(_) vim.cmd.edit(path) end, + preview = vim.schedule_wrap(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', + relative = 'win', + 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 = 0, + col = vim.api.nvim_win_get_width(self.entry.menu.win) + 1, + -- 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, + }) + vim.schedule(function() + vim.api.nvim_exec_autocmds( + 'CursorMoved', + { buffer = self.entry.menu.buf } + ) + 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 + vim.wo[self.entry.menu.preview_float].relativenumber = 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)