feat: add file size in popup (#1049)

This commit is contained in:
Andreas Bissinger 2022-03-06 11:26:35 +01:00 committed by GitHub
parent 5015e7226c
commit ceadf83809
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 20 deletions

View File

@ -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,
@ -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 [[

View File

@ -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 '!!'
@ -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
@ -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
@ -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

View File

@ -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.

View File

@ -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