Skip to content

Commit 50a927f

Browse files
authored
feat: add popup information (#1042)
1 parent 4fedb93 commit 50a927f

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ require'nvim-tree'.setup {
222222
- `W` will collapse the whole tree
223223
- `S` will prompt the user to enter a path and then expands the tree to match the path
224224
- `.` will enter vim command mode with the file the cursor is on
225+
- `C-k` will show file infos about the file under the cursor
225226

226227
### Settings
227228

@@ -287,6 +288,7 @@ local list = {
287288
{ key = "g?", action = "toggle_help" },
288289
{ key = "W", action = "collapse_all" },
289290
{ key = "S", action = "search_node" },
291+
{ key = "<C-k>", action = "show_file_info" },
290292
{ key = ".", action = "run_file_command" }
291293
}
292294
```

doc/nvim-tree-lua.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ INFORMATIONS *nvim-tree-info*
616616
- `W` will collapse the whole tree
617617
- `S` will prompt the user to enter a path and then expands the tree to match the path
618618
- `.` will enter vim command mode with the file the cursor is on
619+
- `C-k` will show file infos about the file under the cursor
619620

620621
Defaults to:
621622
>
@@ -657,7 +658,8 @@ Defaults to:
657658
{ key = "g?", action = "toggle_help" },
658659
{ key = 'W', action = "collapse_all" },
659660
{ key = "S", action = "search_node" },
660-
{ key = ".", action = "run_file_command" }
661+
{ key = ".", action = "run_file_command" },
662+
{ key = "<C-k>", action = "show_file_info" }
661663
}
662664
<
663665
The `list` option in `view.mappings.list` is a table of

lua/nvim-tree/actions/file-popup.lua

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
local a = vim.api
2+
local uv = vim.loop
3+
4+
local M = {}
5+
6+
local function get_formatted_lines(cwd)
7+
local stats = uv.fs_stat(cwd)
8+
local fpath = ' fullpath: ' .. cwd
9+
local created_at = ' created: ' .. os.date("%x %X", stats.birthtime.sec)
10+
local modified_at = ' modified: ' .. os.date("%x %X", stats.mtime.sec)
11+
local accessed_at = ' accessed: ' .. os.date("%x %X", stats.atime.sec)
12+
local size = ' size: ' .. stats.size .. ' bytes'
13+
14+
return {
15+
fpath,
16+
size,
17+
accessed_at,
18+
modified_at,
19+
created_at,
20+
}
21+
end
22+
23+
local winnr = nil
24+
25+
local function setup_window(lines)
26+
local max_width = vim.fn.max(vim.tbl_map(function(n) return #n end, lines))
27+
winnr = a.nvim_open_win(0, false, {
28+
col = 1,
29+
row = 1,
30+
relative = "cursor",
31+
width = max_width + 1,
32+
height = #lines,
33+
border = 'shadow',
34+
noautocmd = true,
35+
style = 'minimal'
36+
})
37+
local bufnr = a.nvim_create_buf(false, true)
38+
a.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
39+
a.nvim_win_set_buf(winnr, bufnr)
40+
end
41+
42+
function M.close_popup()
43+
if winnr ~= nil then
44+
a.nvim_win_close(winnr, { force = true })
45+
vim.cmd "augroup NvimTreeRemoveFilePopup | au! CursorMoved | augroup END"
46+
47+
winnr = nil
48+
end
49+
end
50+
51+
function M.show_file_info(node)
52+
M.close_popup()
53+
54+
local lines = get_formatted_lines(node.absolute_path)
55+
setup_window(lines)
56+
57+
vim.cmd [[
58+
augroup NvimTreeRemoveFilePopup
59+
au CursorMoved * lua require'nvim-tree.actions.file-popup'.close_popup()
60+
augroup END
61+
]]
62+
end
63+
64+
return M

lua/nvim-tree/actions/init.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ local M = {
4343
{ key = "g?", action = "toggle_help" },
4444
{ key = 'W', action = "collapse_all" },
4545
{ key = "S", action = "search_node" },
46-
{ key = ".", action = "run_file_command" }
46+
{ key = ".", action = "run_file_command" },
47+
{ key = "<C-k>", action = "show_file_info" }
4748
},
4849
custom_keypress_funcs = {},
4950
}
@@ -73,6 +74,7 @@ local keypress_funcs = {
7374
rename = require'nvim-tree.actions.rename-file'.fn(false),
7475
run_file_command = require'nvim-tree.actions.run-command'.run_file_command,
7576
search_node = require'nvim-tree.actions.search-node'.fn,
77+
show_file_info = require'nvim-tree.actions.file-popup'.show_file_info,
7678
system_open = require'nvim-tree.actions.system-open'.fn,
7779
toggle_dotfiles = require"nvim-tree.actions.toggles".dotfiles,
7880
toggle_help = require"nvim-tree.actions.toggles".help,

0 commit comments

Comments
 (0)