From a4eda0c9844586c10e3804f4a8e5e809aebb6569 Mon Sep 17 00:00:00 2001 From: Julian Date: Tue, 26 Dec 2017 17:53:22 +0100 Subject: [PATCH] lua: implement mp_utils.format_bytes_humanized --- player/lua/defaults.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/player/lua/defaults.lua b/player/lua/defaults.lua index 1320522b26..32dfed9948 100644 --- a/player/lua/defaults.lua +++ b/player/lua/defaults.lua @@ -589,4 +589,14 @@ function mp_utils.getcwd() return mp.get_property("working-directory") end +function mp_utils.format_bytes_humanized(b) + local d = {"Bytes", "KiB", "MiB", "GiB", "TiB", "PiB"} + local i = 1 + while b >= 1024 do + b = b / 1024 + i = i + 1 + end + return string.format("%0.2f %s", b, d[i] and d[i] or "*1024^" .. (i-1)) +end + return {}