diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..c18fb99 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,6 @@ +# WorldEditAdditions Changelog +It's about time I started a changelog! This will serve from now on as the master changelog for WorldEditAdditions. + + +## v1.8 (unreleased) + - Update `//multi` to display human readable times (e.g. `2.11mins` instead of `126600ms`) diff --git a/worldeditadditions/utils/strings.lua b/worldeditadditions/utils/strings.lua index 9c05e4f..52a0b2b 100644 --- a/worldeditadditions/utils/strings.lua +++ b/worldeditadditions/utils/strings.lua @@ -63,14 +63,14 @@ end --- Pads str to length len with char from right -- @source https://snipplr.com/view/13092/strlpad--pad-string-to-the-left function worldeditadditions.str_padend(str, len, char) - if char == nil then char = ' ' end - return str .. string.rep(char, len - #str) + if char == nil then char = ' ' end + return str .. string.rep(char, len - #str) end --- Pads str to length len with char from left -- Adapted from the above function worldeditadditions.str_padstart(str, len, char) - if char == nil then char = ' ' end - return string.rep(char, len - #str) .. str + if char == nil then char = ' ' end + return string.rep(char, len - #str) .. str end --- Equivalent to string.startsWith in JS @@ -208,3 +208,30 @@ function worldeditadditions.parse_weighted_nodes(parts, as_list) return true, result end + +--- Converts a float milliseconds into a human-readable string. +-- Ported from PHP human_time from Pepperminty Wiki: https://github.com/sbrl/Pepperminty-Wiki/blob/fa81f0d/core/05-functions.php#L82-L104 +-- @param ms float The number of milliseconds to convert. +-- @return string A human-readable string representing the input ms. +function worldeditadditions.human_time(ms) + local tokens = { + { 31536000 * 1000, 'year' }, + { 2592000 * 1000, 'month' }, + { 604800 * 1000, 'week' }, + { 86400 * 1000, 'day' }, + { 3600 * 1000, 'hr' }, + { 60 * 1000, 'min' }, + { 1 * 1000, 's' }, + { 1, 'ms'} + } + + for _,pair in pairs(tokens) do + if ms > pair[1] or pair[2] == "ms" then + local unit = pair[2] + if ms > 60 * 1000 and math.floor(ms / pair[1]) > 1 then + unit = unit.."s" + end + return string.format("%.2f", ms / pair[1])..unit + end + end +end diff --git a/worldeditadditions_commands/multi.lua b/worldeditadditions_commands/multi.lua index 474afec..5951f37 100644 --- a/worldeditadditions_commands/multi.lua +++ b/worldeditadditions_commands/multi.lua @@ -66,12 +66,20 @@ minetest.register_chatcommand("/multi", { end local total_time = (os.clock() - master_start_time) * 1000 - local done_message = string.format("Executed %d commands in %.2fms (", #times, total_time) + local done_message = {} + table.insert(done_message, + string.format("Executed %d commands in %s (", + #times, + worldeditadditions.human_time(total_time) + ) + ) + local message_parts = {} for j=1,#times do - done_message = done_message..string.format("%.2fms, ", times[j]) + table.insert(message_parts, worldeditadditions.human_time(times[j])) end - done_message = string.sub(done_message, 0, string.len(done_message) - 2)..")" + table.insert(done_message, table.concat(message_parts, ", ")) + table.insert(done_message, ")") - worldedit.player_notify(name, done_message) + worldedit.player_notify(name, table.concat(done_message, "")) end })