move format_progress to mpd module

The function is not used by any other module and is only really useful
for formatting durations as exposed by mpd.
This commit is contained in:
Jörg Thalheim 2018-12-14 15:31:08 +00:00 committed by Nguyễn Gia Phong
parent a902a23b5c
commit d11703d589
2 changed files with 19 additions and 20 deletions

View File

@ -136,25 +136,6 @@ function helpers.uformat(array, key, value, unit)
end end
-- }}} -- }}}
-- {{{ Format playing progress
function helpers.format_progress(elapsed, duration)
local em, es = elapsed / 60, elapsed % 60
local dm, ds = duration / 60, duration % 60
if dm < 10 then
return ("%d:%02d"):format(em, es), ("%d:%02d"):format(dm, ds)
elseif dm < 60 then
return ("%02d:%02d"):format(em, es), ("%02d:%02d"):format(dm, ds)
elseif dm < 600 then
return ("%d:%02d:%02d"):format(em / 60, em % 60, es),
("%d:%02d:%02d"):format(dm / 60, dm % 60, ds)
else
return ("%02d:%02d:%02d"):format(em / 60, em % 60, es),
("%02d:%02d:%02d"):format(dm / 60, dm % 60, ds)
end
end
-- }}}
-- {{{ Escape a string -- {{{ Escape a string
function helpers.escape(text) function helpers.escape(text)
local xml_entities = { local xml_entities = {

View File

@ -23,6 +23,24 @@ local function cbool(number)
end end
-- }}} -- }}}
-- {{{ Format playing progress
function format_progress(elapsed, duration)
local em, es = elapsed / 60, elapsed % 60
local dm, ds = duration / 60, duration % 60
if dm < 10 then
return ("%d:%02d"):format(em, es), ("%d:%02d"):format(dm, ds)
elseif dm < 60 then
return ("%02d:%02d"):format(em, es), ("%02d:%02d"):format(dm, ds)
elseif dm < 600 then
return ("%d:%02d:%02d"):format(em / 60, em % 60, es),
("%d:%02d:%02d"):format(dm / 60, dm % 60, ds)
else
return ("%02d:%02d:%02d"):format(em / 60, em % 60, es),
("%02d:%02d:%02d"):format(dm / 60, dm % 60, ds)
end
end
-- }}}
-- {{{ MPD widget type -- {{{ MPD widget type
local function worker(format, warg) local function worker(format, warg)
@ -72,7 +90,7 @@ local function worker(format, warg)
f:close() f:close()
-- Formatted elapsed and duration -- Formatted elapsed and duration
mpd_state["{Elapsed}"], mpd_state["{Duration}"] = helpers.format_progress( mpd_state["{Elapsed}"], mpd_state["{Duration}"] = format_progress(
mpd_state["{elapsed}"], mpd_state["{duration}"]) mpd_state["{elapsed}"], mpd_state["{duration}"])
return mpd_state return mpd_state