Skip to content

Commit 8d44388

Browse files
committed
feat(file_size): Add another option for showing a symbol on folder not shown
1 parent cdbd154 commit 8d44388

File tree

3 files changed

+80
-39
lines changed

3 files changed

+80
-39
lines changed

doc/nvim-tree-lua.txt

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,13 @@ Following is the default configuration. See |nvim-tree-opts| for details.
442442
none = " ",
443443
},
444444
},
445+
size = {
446+
enable = false,
447+
column_width = 12,
448+
show_folder_size = false,
449+
format_unit = "double",
450+
noshow_folder_size_glyph = "•",
451+
},
445452
icons = {
446453
web_devicons = {
447454
file = {
@@ -514,12 +521,6 @@ Following is the default configuration. See |nvim-tree-opts| for details.
514521
cmd = "",
515522
args = {},
516523
},
517-
size = {
518-
enable = true,
519-
column_width = 12,
520-
show_folder_size = false,
521-
format_unit = "double",
522-
},
523524
git = {
524525
enable = true,
525526
show_on_dirs = true,
@@ -847,7 +848,7 @@ Use nvim-tree in a floating window.
847848
5.3 OPTS: RENDERER *nvim-tree-opts-renderer*
848849

849850
Highlight precedence, additive:
850-
git < opened < modified < bookmarked < diagnostics < copied < cut
851+
git < opened < modified < bookmarked < diagnostics < copied < cut < size
851852

852853
*nvim-tree.renderer.add_trailing*
853854
Appends a trailing slash to folder names.
@@ -955,6 +956,37 @@ Configuration options for tree indent markers.
955956
none = " ",
956957
}
957958
<
959+
*nvim-tree.renderer.size*
960+
Configuration for file size display column
961+
962+
*nvim-tree.renderer.size.enable*
963+
Determine if we should render file size information.
964+
Type: `boolean`, Default: `false`
965+
966+
*nvim-tree.renderer.size.column_width*
967+
Determines the amount of space (in characters) the
968+
file size information will take.
969+
Type: `integer`, Default: `12`
970+
971+
*nvim-tree.renderer.size.show_folder_size*
972+
Whether to show the size for folder as well.
973+
Type: `boolean`, Default: `false`
974+
975+
*nvim-tree.renderer.size.format_unit*
976+
Determines how format unit is displayed.
977+
If a string is passed we have the options:
978+
- `"single"` : Show only 1 character for unit. Ex: for 10.4 Megabytes we render "10.4M"
979+
- `"double"` : Show only 2 character for unit and add a space in between. Ex: "10.4 MB"
980+
981+
If more controle we needed, such as lowercase unit, a function can be
982+
passed by the user that takes one of the possible units as string.
983+
units = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }
984+
Type: `string | fun(unit: string): string`, Default: `"double"`
985+
986+
*nvim-tree.renderer.size.noshow_folder_size_glyph*
987+
Glyph to be shown when `nvim-tree.renderer.size.show_folder_size` is disabled.
988+
Type: `string`, Default: `""`
989+
958990
*nvim-tree.renderer.icons*
959991
Configuration options for icons.
960992

@@ -2922,6 +2954,12 @@ highlight group is not, hard linking as follows: >
29222954
|nvim-tree.renderer.indent_markers.inline_arrows|
29232955
|nvim-tree.renderer.indent_width|
29242956
|nvim-tree.renderer.root_folder_label|
2957+
|nvim-tree.renderer.size|
2958+
|nvim-tree.renderer.size.column_width|
2959+
|nvim-tree.renderer.size.enable|
2960+
|nvim-tree.renderer.size.format_unit|
2961+
|nvim-tree.renderer.size.noshow_folder_size_glyph|
2962+
|nvim-tree.renderer.size.show_folder_size|
29252963
|nvim-tree.renderer.special_files|
29262964
|nvim-tree.renderer.symlink_destination|
29272965
|nvim-tree.respect_buf_cwd|

lua/nvim-tree.lua

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,13 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
417417
none = " ",
418418
},
419419
},
420+
size = {
421+
enable = false,
422+
column_width = 12,
423+
show_folder_size = false,
424+
format_unit = "double",
425+
noshow_folder_size_glyph = "",
426+
},
420427
icons = {
421428
web_devicons = {
422429
file = {
@@ -489,12 +496,6 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
489496
cmd = "",
490497
args = {},
491498
},
492-
size = {
493-
enable = true,
494-
column_width = 12,
495-
show_folder_size = false,
496-
format_unit = "double",
497-
},
498499
git = {
499500
enable = true,
500501
show_on_dirs = true,
@@ -653,18 +654,19 @@ local ACCEPTED_TYPES = {
653654
},
654655
},
655656
renderer = {
657+
size = {
658+
enable = { "boolean" },
659+
column_width = { "integer" },
660+
show_folder_size = { "boolean" },
661+
format_unit = { "function", "string" },
662+
noshow_folder_size_glyph = { "string" },
663+
},
656664
group_empty = { "boolean", "function" },
657665
root_folder_label = { "function", "string", "boolean" },
658666
},
659667
update_focused_file = {
660668
exclude = { "function" },
661669
},
662-
size = {
663-
enable = { "boolean" },
664-
column_width = { "integer" },
665-
show_folder_size = { "boolean" },
666-
format_unit = { "function", "string" },
667-
},
668670
git = {
669671
disable_for_dirs = { "function" },
670672
},
@@ -699,6 +701,9 @@ local ACCEPTED_STRINGS = {
699701
highlight_bookmarks = { "none", "icon", "name", "all" },
700702
highlight_diagnostics = { "none", "icon", "name", "all" },
701703
highlight_clipboard = { "none", "icon", "name", "all" },
704+
size = {
705+
format_unit = { "single", "double" },
706+
},
702707
icons = {
703708
git_placement = { "before", "after", "signcolumn", "right_align" },
704709
modified_placement = { "before", "after", "signcolumn", "right_align" },
@@ -710,9 +715,6 @@ local ACCEPTED_STRINGS = {
710715
help = {
711716
sort_by = { "key", "desc" },
712717
},
713-
size = {
714-
format_unit = { "single", "double" },
715-
},
716718
}
717719

718720
---@param conf table|nil
@@ -842,18 +844,18 @@ function M.setup(conf)
842844
log.raw("config", "%s\n", vim.inspect(opts))
843845
end
844846

845-
if M.config.size.column_width < 6 then
847+
if M.config.renderer.size.column_width < 6 then
846848
notify.warn "`size.right_padding` is a small number, problably won't show any size numbers, try using 12."
847849
end
848850

849-
if M.config.size.format_unit == "single" then
851+
if M.config.renderer.size.format_unit == "single" then
850852
-- The unit. Ex: 10.12M
851-
M.config.size.format_unit = function(unit)
853+
M.config.renderer.size.format_unit = function(unit)
852854
return string.format("%1s", unit:sub(1, 1))
853855
end
854-
elseif M.config.size.format_unit == "double" then
856+
elseif M.config.renderer.size.format_unit == "double" then
855857
-- The unit. Ex: 10.12 MB
856-
M.config.size.format_unit = function(unit)
858+
M.config.renderer.size.format_unit = function(unit)
857859
return string.format(" %2s", unit)
858860
end
859861
end

lua/nvim-tree/renderer/decorator/size.lua

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ local DecoratorSize = Decorator:new()
1010
---@return DecoratorSize
1111
function DecoratorSize:new(opts)
1212
local o = Decorator.new(self, {
13-
enabled = opts.size.enable,
14-
column_width = opts.size.column_width,
15-
show_folder_size = opts.size.show_folder_size,
16-
format_unit = opts.size.format_unit, -- Assumed to be a function
13+
enabled = opts.renderer.size.enable,
14+
noshow_folder_size_glyph = opts.renderer.size.noshow_folder_size_glyph or "",
15+
column_width = opts.renderer.size.column_width,
16+
show_folder_size = opts.renderer.size.show_folder_size,
17+
format_unit = opts.renderer.size.format_unit, -- Assumed to be a function
1718
units = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" },
1819
icon_placement = ICON_PLACEMENT.right_align,
1920
})
@@ -32,7 +33,7 @@ function DecoratorSize:new(opts)
3233
end
3334

3435
---@param node Node
35-
---@return string|nil group
36+
---@return nil
3637
function DecoratorSize:calculate_highlight(_)
3738
return nil
3839
end
@@ -41,14 +42,15 @@ end
4142
---@private
4243
---@param size number size in bytes
4344
---@return string
44-
---NOTE: This function tries it's best to minified the string
45+
---NOTE: This function still try it's best to minified the string
4546
--- generated, but this implies that we have more than 3 branchs
4647
--- to determined how much bytes can we shave from the string to
47-
--- comply to self.max_lengh. Since we know self.max_length doesn't
48-
--- change, a better way would be decide a version of human_readable_size based
49-
--- on self.max_lenght once at this object's construction.
48+
--- comply to self.column_width. Since we know self.column_width doesn't
49+
--- change, a better way could be to decide a version of 'human_readable_size' based
50+
--- on self.column_width once at this object's construction.
5051
--- Basically, instead of this method, we would baking all ifs first to decide which function to bind to possible field `self.human_readable_size`
51-
--- I don't actually know if it would be faster without test, but most likely it would be faster.
52+
--- I don't actually know if it would be faster without test.
53+
--- edit: Since then I've moved a lot of ifs around getting down to only three, but I'll keep the comment just in case.
5254
function DecoratorSize:human_readable_size(size)
5355
-- Check for nan, negative, etc.
5456
if type(size) ~= "number" or size ~= size or size < 0 then
@@ -77,7 +79,6 @@ function DecoratorSize:human_readable_size(size)
7779

7880
if #result > max_length then
7981
if (index + 1) < #self.units then
80-
vim.fn.confirm(result)
8182
size = size / 1024
8283
index = index + 1
8384
size_str = self.format_size(size)
@@ -106,7 +107,7 @@ end
106107
---@return HighlightedString[]|nil icons
107108
function DecoratorSize:calculate_icons(node)
108109
local size = node and node.fs_stat and node.fs_stat.size or 0
109-
local folder_size_str = self.column_width > 0 and string.rep(" ", self.column_width - 1) .. "-" or ""
110+
local folder_size_str = self.column_width > 0 and string.rep(" ", self.column_width - 1) .. self.noshow_folder_size_glyph or ""
110111
local icon = {
111112
str = (self.show_folder_size or node.nodes == nil) and self:human_readable_size(size) or folder_size_str,
112113
hl = { "NvimTreeFileSize" },

0 commit comments

Comments
 (0)