//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
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
3 changed files with 25 additions and 2 deletions

View file

@ -6,7 +6,7 @@
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ -- ██ ██ ██ ██ ██ ██ ██ ██ ██
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ -- ██ ██ ██ ██ ██ ██ ██ ██ ██
-- ██████ ██████ ██████ ██ ████ ██ -- ██████ ██████ ██████ ██ ████ ██
function worldeditadditions.count(pos1, pos2) function worldeditadditions.count(pos1, pos2, do_human_counts)
pos1, pos2 = worldedit.sort_pos(pos1, pos2) pos1, pos2 = worldedit.sort_pos(pos1, pos2)
-- pos2 will always have the highest co-ordinates now -- pos2 will always have the highest co-ordinates now
@ -35,6 +35,12 @@ function worldeditadditions.count(pos1, pos2)
end end
table.sort(results, function(a, b) return a[1] < b[1] 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 -- Save the modified nodes back to disk & return
-- No need to save - this function doesn't actually change anything -- No need to save - this function doesn't actually change anything
-- worldedit.manip_helpers.finish(manip, data) -- worldedit.manip_helpers.finish(manip, data)

View file

@ -269,6 +269,19 @@ function worldeditadditions.human_time(ms)
end end
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. --- Makes a seed from a string.
-- If the input is a number, it is returned as-is. -- If the input is a number, it is returned as-is.

View file

@ -19,7 +19,11 @@ worldedit.register_command("count", {
func = function(name) func = function(name)
local start_time = worldeditadditions.get_ms_time() 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".. local result = worldeditadditions.make_ascii_table(counts).."\n"..
string.rep("=", 6 + #tostring(total) + 6).."\n".. string.rep("=", 6 + #tostring(total) + 6).."\n"..
"Total "..total.." nodes\n" "Total "..total.." nodes\n"