From 4b1cf074d5082e375246e3577844833239d3cbdf Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sun, 31 Jan 2021 17:43:08 +0000 Subject: [PATCH] //count: make numbers human-readable Fixes #14 --- worldeditadditions/lib/count.lua | 8 +++++++- worldeditadditions/utils/strings.lua | 13 +++++++++++++ worldeditadditions_commands/commands/count.lua | 6 +++++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/worldeditadditions/lib/count.lua b/worldeditadditions/lib/count.lua index 51f6adc..a3c6974 100644 --- a/worldeditadditions/lib/count.lua +++ b/worldeditadditions/lib/count.lua @@ -6,7 +6,7 @@ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ -- ██████ ██████ ██████ ██ ████ ██ -function worldeditadditions.count(pos1, pos2) +function worldeditadditions.count(pos1, pos2, do_human_counts) pos1, pos2 = worldedit.sort_pos(pos1, pos2) -- pos2 will always have the highest co-ordinates now @@ -35,6 +35,12 @@ function worldeditadditions.count(pos1, pos2) end table.sort(results, function(a, b) return a[1] < b[1] end) + if do_human_counts then + for key,item in pairs(results) do + item[1] = worldeditadditions.human_size(item[1], 2) + end + end + -- Save the modified nodes back to disk & return -- No need to save - this function doesn't actually change anything -- worldedit.manip_helpers.finish(manip, data) diff --git a/worldeditadditions/utils/strings.lua b/worldeditadditions/utils/strings.lua index 2616484..2751f4c 100644 --- a/worldeditadditions/utils/strings.lua +++ b/worldeditadditions/utils/strings.lua @@ -269,6 +269,19 @@ function worldeditadditions.human_time(ms) end end +--- Formats (usually large) numbers as human-readable strings. +-- Ported from PHP: https://github.com/sbrl/Pepperminty-Wiki/blob/0a81c940c5803856db250b29f54658476bc81e21/core/05-functions.php#L67 +-- @param n number The number to format. +-- @param decimals number The number fo decimal places to show. +-- @return string A formatted string that represents the given input number. +function worldeditadditions.human_size(n, decimals) + sizes = { "", "K", "M", "G", "T", "P", "E", "Y", "Z" } + local factor = math.floor((#tostring(n) - 1) / 3) + local multiplier = 10^(decimals or 0) + local result = math.floor(0.5 + (n / math.pow(1000, factor)) * multiplier) / multiplier + return result .. sizes[factor+1] +end + --- Makes a seed from a string. -- If the input is a number, it is returned as-is. diff --git a/worldeditadditions_commands/commands/count.lua b/worldeditadditions_commands/commands/count.lua index 04b1861..00731e8 100644 --- a/worldeditadditions_commands/commands/count.lua +++ b/worldeditadditions_commands/commands/count.lua @@ -19,7 +19,11 @@ worldedit.register_command("count", { func = function(name) local start_time = worldeditadditions.get_ms_time() - local success, counts, total = worldeditadditions.count(worldedit.pos1[name], worldedit.pos2[name], target_node) + local success, counts, total = worldeditadditions.count( + worldedit.pos1[name], worldedit.pos2[name], + true + ) + local result = worldeditadditions.make_ascii_table(counts).."\n".. string.rep("=", 6 + #tostring(total) + 6).."\n".. "Total "..total.." nodes\n"