From 50a927f176bd86ab33a2081d2590fe5bf0656947 Mon Sep 17 00:00:00 2001 From: Andreas Bissinger Date: Thu, 3 Mar 2022 21:13:10 +0100 Subject: [PATCH] feat: add popup information (#1042) --- README.md | 2 + doc/nvim-tree-lua.txt | 4 +- lua/nvim-tree/actions/file-popup.lua | 64 ++++++++++++++++++++++++++++ lua/nvim-tree/actions/init.lua | 4 +- 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 lua/nvim-tree/actions/file-popup.lua diff --git a/README.md b/README.md index 2a39b25d..49937ce7 100644 --- a/README.md +++ b/README.md @@ -222,6 +222,7 @@ require'nvim-tree'.setup { - `W` will collapse the whole tree - `S` will prompt the user to enter a path and then expands the tree to match the path - `.` will enter vim command mode with the file the cursor is on +- `C-k` will show file infos about the file under the cursor ### Settings @@ -287,6 +288,7 @@ local list = { { key = "g?", action = "toggle_help" }, { key = "W", action = "collapse_all" }, { key = "S", action = "search_node" }, + { key = "", action = "show_file_info" }, { key = ".", action = "run_file_command" } } ``` diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 86b73873..da929f7f 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -616,6 +616,7 @@ INFORMATIONS *nvim-tree-info* - `W` will collapse the whole tree - `S` will prompt the user to enter a path and then expands the tree to match the path - `.` will enter vim command mode with the file the cursor is on +- `C-k` will show file infos about the file under the cursor Defaults to: > @@ -657,7 +658,8 @@ Defaults to: { key = "g?", action = "toggle_help" }, { key = 'W', action = "collapse_all" }, { key = "S", action = "search_node" }, - { key = ".", action = "run_file_command" } + { key = ".", action = "run_file_command" }, + { key = "", action = "show_file_info" } } < The `list` option in `view.mappings.list` is a table of diff --git a/lua/nvim-tree/actions/file-popup.lua b/lua/nvim-tree/actions/file-popup.lua new file mode 100644 index 00000000..c414b68c --- /dev/null +++ b/lua/nvim-tree/actions/file-popup.lua @@ -0,0 +1,64 @@ +local a = vim.api +local uv = vim.loop + +local M = {} + +local function get_formatted_lines(cwd) + local stats = uv.fs_stat(cwd) + local fpath = ' fullpath: ' .. cwd + local created_at = ' created: ' .. os.date("%x %X", stats.birthtime.sec) + local modified_at = ' modified: ' .. os.date("%x %X", stats.mtime.sec) + local accessed_at = ' accessed: ' .. os.date("%x %X", stats.atime.sec) + local size = ' size: ' .. stats.size .. ' bytes' + + return { + fpath, + size, + accessed_at, + modified_at, + created_at, + } +end + +local winnr = nil + +local function setup_window(lines) + local max_width = vim.fn.max(vim.tbl_map(function(n) return #n end, lines)) + winnr = a.nvim_open_win(0, false, { + col = 1, + row = 1, + relative = "cursor", + width = max_width + 1, + height = #lines, + border = 'shadow', + noautocmd = true, + style = 'minimal' + }) + local bufnr = a.nvim_create_buf(false, true) + a.nvim_buf_set_lines(bufnr, 0, -1, false, lines) + a.nvim_win_set_buf(winnr, bufnr) +end + +function M.close_popup() + if winnr ~= nil then + a.nvim_win_close(winnr, { force = true }) + vim.cmd "augroup NvimTreeRemoveFilePopup | au! CursorMoved | augroup END" + + winnr = nil + end +end + +function M.show_file_info(node) + M.close_popup() + + local lines = get_formatted_lines(node.absolute_path) + setup_window(lines) + + vim.cmd [[ + augroup NvimTreeRemoveFilePopup + au CursorMoved * lua require'nvim-tree.actions.file-popup'.close_popup() + augroup END + ]] +end + +return M diff --git a/lua/nvim-tree/actions/init.lua b/lua/nvim-tree/actions/init.lua index e49a5cfa..8d2d527b 100644 --- a/lua/nvim-tree/actions/init.lua +++ b/lua/nvim-tree/actions/init.lua @@ -43,7 +43,8 @@ local M = { { key = "g?", action = "toggle_help" }, { key = 'W', action = "collapse_all" }, { key = "S", action = "search_node" }, - { key = ".", action = "run_file_command" } + { key = ".", action = "run_file_command" }, + { key = "", action = "show_file_info" } }, custom_keypress_funcs = {}, } @@ -73,6 +74,7 @@ local keypress_funcs = { rename = require'nvim-tree.actions.rename-file'.fn(false), run_file_command = require'nvim-tree.actions.run-command'.run_file_command, search_node = require'nvim-tree.actions.search-node'.fn, + show_file_info = require'nvim-tree.actions.file-popup'.show_file_info, system_open = require'nvim-tree.actions.system-open'.fn, toggle_dotfiles = require"nvim-tree.actions.toggles".dotfiles, toggle_help = require"nvim-tree.actions.toggles".help,