robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
645a588aa6
master
1local N_ =vim . fn . gettext 2 3local M ={} 4 5--- @param seconds_in_unit integer How many seconds make up the unit. 6--- @param singular string The singular name of the unit. ("1 second") 7--- @param fplural string The plural name of the unit, to format. ("%s seconds") 8--- @param times string[] Working list of uptime strings. 9--- @param remaining integer Remaining time, in seconds. 10--- @return integer remaining Remaining time. 11local function time_part ( seconds_in_unit , singular , fplural , times , remaining ) 12local unit =math . floor ( remaining / seconds_in_unit ) 13if unit ~= 0 or # times ~= 0 or seconds_in_unit == 1 then 14local display =unit == 1 and singular or fplural : format ( unit ) 15times [ # times + 1 ] =display 16end 17return remaining % seconds_in_unit 18end 19 20--- Display seconds in a pretty form (e.g. "1 hour, 24 minutes, 13 seconds"). 21--- 22--- @param seconds integer Time in seconds. 23--- @return string time Pretty representation of the time. 24function M . fmt_rtime ( seconds ) 25local times ={} 26seconds =time_part ( 86400 , N_ ( '1 day' ), N_ ( '%s days' ), times , seconds ) 27seconds =time_part ( 3600 , N_ ( '1 hour' ), N_ ( '%s hours' ), times , seconds ) 28seconds =time_part ( 60 , N_ ( '1 minute' ), N_ ( '%s minutes' ), times , seconds ) 29seconds =time_part ( 1 , N_ ( '1 second' ), N_ ( '%s seconds' ), times , seconds ) 30assert ( seconds == 0 ) 31 32return table . concat ( times , ', ' ) 33end 34 35return M