Skip to content

feat: add file size in popup #1049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lua/nvim-tree/actions/file-popup.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
local utils = require'nvim-tree.utils'
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 function get_formatted_lines(node)
local stats = node.fs_stat
local fpath = ' fullpath: ' .. node.absolute_path
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'
local size = ' size: ' .. utils.format_bytes(stats.size)

return {
fpath,
Expand Down Expand Up @@ -51,7 +51,7 @@ end
function M.show_file_info(node)
M.close_popup()

local lines = get_formatted_lines(node.absolute_path)
local lines = get_formatted_lines(node)
setup_window(lines)

vim.cmd [[
Expand Down
16 changes: 3 additions & 13 deletions lua/nvim-tree/explorer/node-builders.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ local M = {
is_windows = vim.fn.has('win32') == 1
}

local function get_last_modified(absolute_path)
local stat = uv.fs_stat(absolute_path)

local last_modified = 0
if stat ~= nil then
last_modified = stat.mtime.sec
end
return last_modified
end

function M.get_dir_git_status(parent_ignored, status, absolute_path)
if parent_ignored then
return '!!'
Expand All @@ -40,7 +30,7 @@ function M.folder(absolute_path, name, status, parent_ignored)
name = name,
nodes = {},
open = false,
last_modified = get_last_modified(absolute_path),
fs_stat = uv.fs_stat(absolute_path),
}
end

Expand All @@ -60,7 +50,7 @@ function M.file(absolute_path, name, status, parent_ignored)
extension = ext,
git_status = M.get_git_status(parent_ignored, status, absolute_path),
name = name,
last_modified = get_last_modified(absolute_path),
fs_stat = uv.fs_stat(absolute_path),
}
end

Expand All @@ -82,11 +72,11 @@ function M.link(absolute_path, name, status, parent_ignored)
absolute_path = absolute_path,
git_status = M.get_git_status(parent_ignored, status, absolute_path),
group_next = nil, -- If node is grouped, this points to the next child dir/link node
last_modified = get_last_modified(absolute_path),
link_to = link_to,
name = name,
nodes = nodes,
open = open,
fs_stat = uv.fs_stat(absolute_path),
}
end

Expand Down
13 changes: 12 additions & 1 deletion lua/nvim-tree/explorer/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,18 @@ function M.node_comparator_modification_time(a, b)
return false
end

return b.last_modified <= a.last_modified
local last_modified_a = 0
local last_modified_b = 0

if a.fs_stat ~= nil then
last_modified_a = a.fs_stat.mtime.sec
end

if b.fs_stat ~= nil then
last_modified_b = b.fs_stat.mtime.sec
end

return last_modified_a <= last_modified_b
end

---Check if the given path should be ignored.
Expand Down
15 changes: 15 additions & 0 deletions lua/nvim-tree/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,19 @@ function M.table_create_missing(tbl, sub)
return t
end

function M.format_bytes(bytes)
local units = {'B', 'K', 'M', 'G', 'T'}

bytes = math.max(bytes, 0)
local pow = math.floor((bytes and math.log(bytes) or 0) / math.log(1024))
pow = math.min(pow, #units)

local value = bytes / (1024 ^ pow)
value = math.floor((value * 10) + 0.5) / 10

pow = pow + 1

return (units[pow] == nil) and (bytes .. 'B') or (value .. units[pow])
end

return M