From 81aafd9841f5d7556c7f1a6430df31c8ebbd241a Mon Sep 17 00:00:00 2001 From: Andreas Bissinger Date: Fri, 4 Mar 2022 21:15:50 +0100 Subject: [PATCH 1/2] feat: show formatted size in file popup --- lua/nvim-tree/actions/file-popup.lua | 3 ++- lua/nvim-tree/utils.lua | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lua/nvim-tree/actions/file-popup.lua b/lua/nvim-tree/actions/file-popup.lua index c414b68c29b..c05836df681 100644 --- a/lua/nvim-tree/actions/file-popup.lua +++ b/lua/nvim-tree/actions/file-popup.lua @@ -1,3 +1,4 @@ +local utils = require'nvim-tree.utils' local a = vim.api local uv = vim.loop @@ -9,7 +10,7 @@ local function get_formatted_lines(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' + local size = ' size: ' .. utils.format_bytes(stats.size) return { fpath, diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index e0772c8cff1..f468a7f83ef 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -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 From 3633478949d5c47e73a9d68cdcfd51936b2c5902 Mon Sep 17 00:00:00 2001 From: Andreas Bissinger Date: Fri, 4 Mar 2022 21:24:27 +0100 Subject: [PATCH 2/2] refactor node builder --- lua/nvim-tree/actions/file-popup.lua | 9 ++++----- lua/nvim-tree/explorer/node-builders.lua | 16 +++------------- lua/nvim-tree/explorer/utils.lua | 13 ++++++++++++- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/lua/nvim-tree/actions/file-popup.lua b/lua/nvim-tree/actions/file-popup.lua index c05836df681..79cdd4969e4 100644 --- a/lua/nvim-tree/actions/file-popup.lua +++ b/lua/nvim-tree/actions/file-popup.lua @@ -1,12 +1,11 @@ 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) @@ -52,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 [[ diff --git a/lua/nvim-tree/explorer/node-builders.lua b/lua/nvim-tree/explorer/node-builders.lua index 2f4f9b95d42..4f78b082535 100644 --- a/lua/nvim-tree/explorer/node-builders.lua +++ b/lua/nvim-tree/explorer/node-builders.lua @@ -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 diff --git a/lua/nvim-tree/explorer/utils.lua b/lua/nvim-tree/explorer/utils.lua index 2373ff92733..f0c3ca0a603 100644 --- a/lua/nvim-tree/explorer/utils.lua +++ b/lua/nvim-tree/explorer/utils.lua @@ -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.