DeviceMenu: Colons and more information (#8435)

Add information to device menu entries
This commit is contained in:
zwim
2021-11-23 21:17:07 +01:00
committed by GitHub
parent 1b2b4da9e5
commit e9ba854ff0
8 changed files with 147 additions and 86 deletions

View File

@@ -147,8 +147,9 @@ end
---- @int seconds number of seconds
---- @bool withoutSeconds if true 1h30', if false 1h30'10''
---- @bool hmsFormat, if true format 1h30m10s
---- @treturn string clock string in the form of 1h30' or 1h30'10''
---- @treturn string clock string in the form of 1h30'10'' or 1h30m10s
function util.secondsToHClock(seconds, withoutSeconds, hmsFormat)
local SECONDS_SYMBOL = "\""
seconds = tonumber(seconds)
if seconds == 0 then
if withoutSeconds then
@@ -161,86 +162,63 @@ function util.secondsToHClock(seconds, withoutSeconds, hmsFormat)
if hmsFormat then
return T(_("%1s"), "0")
else
return "0''"
return "0" .. SECONDS_SYMBOL
end
end
elseif seconds < 60 then
if withoutSeconds and seconds < 30 then
if hmsFormat then
-- @translators This is the 'm' for minute, like in 30m30s. This is a duration.
return T(_("%1m"), "0")
else
return "0'"
end
elseif withoutSeconds and seconds >= 30 then
if hmsFormat then
-- @translators This is the 'm' for minute, like in 30m30s. This is a duration.
return T(_("%1m"), "1")
else
return "1'"
end
else
if hmsFormat then
-- @translators This is the 'm' for minute and 's' for seconds, like in 30m30s. This is a duration.
return T(_("%1m%2s"), "0", string.format("%02d", seconds))
else
return "0'" .. string.format("%02d", seconds) .. "''"
return "0'" .. string.format("%02d", seconds) .. SECONDS_SYMBOL
end
end
else
local round = withoutSeconds and require("optmath").round or passthrough
local hours = string.format("%d", seconds / 3600)
local mins = string.format("%02d", round(seconds % 3600 / 60))
local time_string = util.secondsToClock(seconds, withoutSeconds)
if withoutSeconds then
if mins == "60" then
mins = string.format("%02d", 0)
hours = string.format("%d", hours + 1)
end
if hours == "0" then
-- We can optimize out the % 3600 since the branch ensures we're < than 3600
mins = string.format("%d", round(seconds / 60))
if hmsFormat then
return T(_("%1m"), mins)
else
return mins .. "'"
end
end
-- @translators This is the 'h' for hour, like in 1h30. This is a duration.
return T(_("%1h%2"), hours, mins)
time_string = time_string .. ":"
end
if hmsFormat then
-- @translators This is the 'h' for hour, like in 1h30m30s. This is a duration.
time_string = time_string:gsub(":", _("h"), 1)
-- @translators This is the 'm' for minute, like in 1h30m30s. This is a duration.
time_string = time_string:gsub(":", _("m"), 1)
-- @translators This is the 's' for second, like in 1h30m30s. This is a duration.
time_string = time_string:gsub("00" .. _("h"), "") -- delete leading "00h"
time_string = time_string:gsub("^0", "") -- delete leading "0"
return withoutSeconds and time_string or (time_string .. _("s"))
else
local secs = string.format("%02d", seconds % 60)
if hours == "0" then
mins = string.format("%d", round(seconds / 60))
if hmsFormat then
-- @translators This is the 'm' for minute and the 's' for second, like in 1m30s. This is a duration.
return T(_("%1m%2s"), mins, secs)
else
return mins .. "'" .. secs .. "''"
end
end
if hmsFormat then
if secs == "00" then
-- @translators This is the 'h' for hour and the 'm' for minute, like in 1h30m. This is a duration.
return T(_("%1h%2m"), hours, mins)
else
-- @translators This is the 'h' for hour, the 'm' for minute and the 's' for second, like in 1h30m30s. This is a duration.
return T(_("%1h%2m%3s"), hours, mins, secs)
end
else
if secs == "00" then
return T(_("%1h%2'"), hours, mins)
else
return T(_("%1h%2'%3''"), hours, mins, secs)
end
end
-- @translators This is the 'h' for hour, like in 1h30m30s. This is a duration.
time_string = time_string:gsub(":", _("h"), 1)
time_string = time_string:gsub(":", "'", 1)
time_string = time_string:gsub("00" .. _("h"), "") -- delete leading "00h"
time_string = time_string:gsub("^0", "") -- delete leading "0"
return withoutSeconds and time_string or (time_string .. SECONDS_SYMBOL)
end
end
end
--- Converts seconds to a clock type (classic or modern), based on the given format preference
--- "Classic" format calls secondsToClock, and "Modern" format calls secondsToHClock
---- @string Either "modern" for 1h30' or "classic" for 1:30
---- @bool withoutSeconds if true 1h30' or 1:30, if false 1h30'10'' or 1:30:10
---- @bool hmsFormat, modern format only, if true format 1h30m10s
---- @treturn string clock string in the specific format of 1h30', 1h30'10'' or 1:30'
---- @string Either "modern" for 1h30'10" or "classic" for 1:30:10
---- @bool withoutSeconds if true 1h30' or 1h30m, if false 1h30'10" or 1h30m10s
---- @bool hmsFormat, modern format only, if true format 1h30m or 1h30m10s
---- @treturn string clock string in the specific format of 1h30', 1h30'10" resp. 1h30m, 1h30m10s
function util.secondsToClockDuration(format, seconds, withoutSeconds, hmsFormat)
if format == "modern" then
return util.secondsToHClock(seconds, withoutSeconds, hmsFormat)