robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Olivia Kinnearfeat(excmd): add :uptime command #39331645a588aa6

master
1.3 KiB35 linesraw
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)
12  local unit = math.floor(remaining / seconds_in_unit)
13  if unit ~= 0 or #times ~= 0 or seconds_in_unit == 1 then
14    local display = unit == 1 and singular or fplural:format(unit)
15    times[#times + 1] = display
16  end
17  return 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)
25  local times = {}
26  seconds = time_part(86400, N_('1 day'), N_('%s days'), times, seconds)
27  seconds = time_part(3600, N_('1 hour'), N_('%s hours'), times, seconds)
28  seconds = time_part(60, N_('1 minute'), N_('%s minutes'), times, seconds)
29  seconds = time_part(1, N_('1 second'), N_('%s seconds'), times, seconds)
30  assert(seconds == 0)
31
32  return table.concat(times, ', ')
33end
34
35return M