//count: make numbers human-readable

Fixes #14
This commit is contained in:
Starbeamrainbowlabs 2021-01-31 17:43:08 +00:00
parent 8b32353f74
commit 4b1cf074d5
No known key found for this signature in database
GPG Key ID: 1BE5172E637709C2
3 changed files with 25 additions and 2 deletions

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

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

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