|
| 1 | +local HL_POSITION = require("nvim-tree.enum").HL_POSITION |
| 2 | +local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT |
| 3 | +local Decorator = require "nvim-tree.renderer.decorator" |
| 4 | + |
| 5 | +---@class DecoratorSize: Decorator |
| 6 | +---@field icon HighlightedString|nil |
| 7 | +local DecoratorSize = Decorator:new() |
| 8 | + |
| 9 | +---@param opts table |
| 10 | +---@return DecoratorSize |
| 11 | +function DecoratorSize:new(opts) |
| 12 | + 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 |
| 17 | + units = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }, |
| 18 | + icon_placement = ICON_PLACEMENT.right_align, |
| 19 | + }) |
| 20 | + |
| 21 | + o.format_size = function(size) |
| 22 | + local formatted = string |
| 23 | + .format("%.2f", size) |
| 24 | + -- Remove trailing zeros after the decimal point |
| 25 | + :gsub("(%..-)0+$", "%1") |
| 26 | + -- Remove the trailing decimal point if it's a whole number |
| 27 | + :gsub("%.$", "") |
| 28 | + return formatted |
| 29 | + end |
| 30 | + |
| 31 | + return o |
| 32 | +end |
| 33 | + |
| 34 | +---@param node Node |
| 35 | +---@return string|nil group |
| 36 | +function DecoratorSize:calculate_highlight(_) |
| 37 | + return nil |
| 38 | +end |
| 39 | + |
| 40 | +--- Convert a size to a human-readable format (e.g., KB, MB, GB) with fixed width |
| 41 | +---@private |
| 42 | +---@param size number size in bytes |
| 43 | +---@return string |
| 44 | +---NOTE: This function tries it's best to minified the string |
| 45 | +--- generated, but this implies that we have more than 3 branchs |
| 46 | +--- 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. |
| 50 | +--- 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 | +function DecoratorSize:human_readable_size(size) |
| 53 | + -- Check for nan, negative, etc. |
| 54 | + if type(size) ~= "number" or size ~= size or size < 0 then |
| 55 | + return "" |
| 56 | + end |
| 57 | + local index = 1 |
| 58 | + |
| 59 | + -- We check for index here because for exaple, let's say |
| 60 | + -- on a given iteration you have : |
| 61 | + -- 1024 YB, then index is equal to #units, normally we would devide again, |
| 62 | + -- but we don't have more units, so keep as is. |
| 63 | + while size >= 1024 and index < #self.units do |
| 64 | + size = size / 1024 |
| 65 | + index = index + 1 |
| 66 | + end |
| 67 | + |
| 68 | + local unit_str = self.format_unit(self.units[index]) |
| 69 | + |
| 70 | + -- Apparently string.format already rounds then number |
| 71 | + local size_str = self.format_size(size) |
| 72 | + local result = size_str .. unit_str |
| 73 | + |
| 74 | + -- We Need a max length to align size redering properly |
| 75 | + -- So the result must at at most this column width |
| 76 | + local max_length = self.column_width |
| 77 | + |
| 78 | + if #result > max_length then |
| 79 | + if (index + 1) < #self.units then |
| 80 | + vim.fn.confirm(result) |
| 81 | + size = size / 1024 |
| 82 | + index = index + 1 |
| 83 | + size_str = self.format_size(size) |
| 84 | + unit_str = self.format_unit(self.units[index]) |
| 85 | + result = size_str .. unit_str |
| 86 | + -- Now that we divided one more time to make it even smaller |
| 87 | + -- we are garanteed the size string to have lenght of <= 4 (from 0.xx size) |
| 88 | + assert(#size_str <= 4) |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + -- After we're sure we divided as much as we can when we |
| 93 | + -- actually need it, only then we add the padding of max_length |
| 94 | + result = string.format("%" .. max_length .. "s", result) |
| 95 | + |
| 96 | + -- If still too big after all that, |
| 97 | + -- then we just set to empty string |
| 98 | + if #result > max_length then |
| 99 | + result = "" |
| 100 | + end |
| 101 | + |
| 102 | + return result |
| 103 | +end |
| 104 | + |
| 105 | +---@param node Node |
| 106 | +---@return HighlightedString[]|nil icons |
| 107 | +function DecoratorSize:calculate_icons(node) |
| 108 | + 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 icon = { |
| 111 | + str = (self.show_folder_size or node.nodes == nil) and self:human_readable_size(size) or folder_size_str, |
| 112 | + hl = { "NvimTreeFileSize" }, |
| 113 | + } |
| 114 | + return { icon } |
| 115 | +end |
| 116 | + |
| 117 | +return DecoratorSize |
0 commit comments