Skip to content

Commit ceadf83

Browse files
authored
feat: add file size in popup (#1049)
1 parent 5015e72 commit ceadf83

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1+
local utils = require'nvim-tree.utils'
12
local a = vim.api
2-
local uv = vim.loop
33

44
local M = {}
55

6-
local function get_formatted_lines(cwd)
7-
local stats = uv.fs_stat(cwd)
8-
local fpath = ' fullpath: ' .. cwd
6+
local function get_formatted_lines(node)
7+
local stats = node.fs_stat
8+
local fpath = ' fullpath: ' .. node.absolute_path
99
local created_at = ' created: ' .. os.date("%x %X", stats.birthtime.sec)
1010
local modified_at = ' modified: ' .. os.date("%x %X", stats.mtime.sec)
1111
local accessed_at = ' accessed: ' .. os.date("%x %X", stats.atime.sec)
12-
local size = ' size: ' .. stats.size .. ' bytes'
12+
local size = ' size: ' .. utils.format_bytes(stats.size)
1313

1414
return {
1515
fpath,
@@ -51,7 +51,7 @@ end
5151
function M.show_file_info(node)
5252
M.close_popup()
5353

54-
local lines = get_formatted_lines(node.absolute_path)
54+
local lines = get_formatted_lines(node)
5555
setup_window(lines)
5656

5757
vim.cmd [[

lua/nvim-tree/explorer/node-builders.lua

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,6 @@ local M = {
55
is_windows = vim.fn.has('win32') == 1
66
}
77

8-
local function get_last_modified(absolute_path)
9-
local stat = uv.fs_stat(absolute_path)
10-
11-
local last_modified = 0
12-
if stat ~= nil then
13-
last_modified = stat.mtime.sec
14-
end
15-
return last_modified
16-
end
17-
188
function M.get_dir_git_status(parent_ignored, status, absolute_path)
199
if parent_ignored then
2010
return '!!'
@@ -40,7 +30,7 @@ function M.folder(absolute_path, name, status, parent_ignored)
4030
name = name,
4131
nodes = {},
4232
open = false,
43-
last_modified = get_last_modified(absolute_path),
33+
fs_stat = uv.fs_stat(absolute_path),
4434
}
4535
end
4636

@@ -60,7 +50,7 @@ function M.file(absolute_path, name, status, parent_ignored)
6050
extension = ext,
6151
git_status = M.get_git_status(parent_ignored, status, absolute_path),
6252
name = name,
63-
last_modified = get_last_modified(absolute_path),
53+
fs_stat = uv.fs_stat(absolute_path),
6454
}
6555
end
6656

@@ -82,11 +72,11 @@ function M.link(absolute_path, name, status, parent_ignored)
8272
absolute_path = absolute_path,
8373
git_status = M.get_git_status(parent_ignored, status, absolute_path),
8474
group_next = nil, -- If node is grouped, this points to the next child dir/link node
85-
last_modified = get_last_modified(absolute_path),
8675
link_to = link_to,
8776
name = name,
8877
nodes = nodes,
8978
open = open,
79+
fs_stat = uv.fs_stat(absolute_path),
9080
}
9181
end
9282

lua/nvim-tree/explorer/utils.lua

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,18 @@ function M.node_comparator_modification_time(a, b)
3131
return false
3232
end
3333

34-
return b.last_modified <= a.last_modified
34+
local last_modified_a = 0
35+
local last_modified_b = 0
36+
37+
if a.fs_stat ~= nil then
38+
last_modified_a = a.fs_stat.mtime.sec
39+
end
40+
41+
if b.fs_stat ~= nil then
42+
last_modified_b = b.fs_stat.mtime.sec
43+
end
44+
45+
return last_modified_a <= last_modified_b
3546
end
3647

3748
---Check if the given path should be ignored.

lua/nvim-tree/utils.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,19 @@ function M.table_create_missing(tbl, sub)
245245
return t
246246
end
247247

248+
function M.format_bytes(bytes)
249+
local units = {'B', 'K', 'M', 'G', 'T'}
250+
251+
bytes = math.max(bytes, 0)
252+
local pow = math.floor((bytes and math.log(bytes) or 0) / math.log(1024))
253+
pow = math.min(pow, #units)
254+
255+
local value = bytes / (1024 ^ pow)
256+
value = math.floor((value * 10) + 0.5) / 10
257+
258+
pow = pow + 1
259+
260+
return (units[pow] == nil) and (bytes .. 'B') or (value .. units[pow])
261+
end
262+
248263
return M

0 commit comments

Comments
 (0)