Display human-friendly scheduled delays
John Smith

John Smith commited on 2022-29 22:43:58
Showing 1 changed files, with 44 additions and 4 deletions.


The possible delays to be scheduled with each evaluation response,
initially displayed as numbers of seconds (in f6a2100),
are now displayed in a more human-friendly way,
with a small value and a unit suffix -- one of
* h for hours
* d for days
* m for months
* y for years,
or, for very short times, a time in mm:ss format.
... ...
@@ -67,6 +67,46 @@ local function imgify(text)
67 67
     end
68 68
 end
69 69
 
70
+--[[
71
+    rounds `x` to `n` places (default 0) after the radix point
72
+    in base `b` (default 10)
73
+]]
74
+local function round(x, n, b)
75
+    local b = b or 10
76
+    local n = n or 0
77
+    if n == 0 then
78
+        return math.tointeger(math.floor(x * b ^ n) / b ^ n)
79
+    else
80
+        return math.floor(x * b ^ n) / b ^ n
81
+    end
82
+end
83
+
84
+--[[
85
+    return human-friendly string to present a duration (input in seconds),
86
+    e.g. "44:37", "4.8h", "11d", "8.3m", "17.4y"
87
+]]
88
+local function disptime(ts)
89
+    local YDAY = 365.24
90
+    local MDAY = YDAY / 12
91
+    local DSEC = 86400
92
+    local HSEC = 3600
93
+    if ts >= 3 * YDAY * DSEC then
94
+        return round(ts / (YDAY * DSEC), 1) .. "y"
95
+    elseif ts >= 4 * MDAY * DSEC then
96
+        return round(ts / (MDAY * DSEC), 1) .. "m"
97
+    elseif ts >= 10 * DSEC then
98
+        return round(ts / DSEC) .. "d"
99
+    elseif ts >= 1.5 * DSEC then
100
+        return round(ts / DSEC, 1) .. "d"
101
+    elseif ts >= 8 * HSEC then
102
+        return round(ts / HSEC) .. "h"
103
+    elseif ts >= HSEC then
104
+        return round(ts / HSEC, 1) .. "h"
105
+    else
106
+        return string.format("%d:%02d", round(ts / 60), round(ts % 60))
107
+    end
108
+end
109
+
70 110
 --[[
71 111
     check action based on first command-line argument;
72 112
     if nothing valid given, default to help
... ...
@@ -156,10 +196,10 @@ elseif action == "r" then
156 196
                 io.read("l")
157 197
                 imgify(pst:gsub("%%A", (pr.ans:gsub("%%", "%%%%"))))
158 198
                 io.write(string.format(
159
-                    "\nAgain / Hard (%d) / Good (%d) / Easy (%d) / Correction ",
160
-                    math.floor(review.schedule(pr, st, 0.5)),
161
-                    math.floor(review.schedule(pr, st, 1)),
162
-                    math.floor(review.schedule(pr, st, 1.5))
199
+                    "\nAgain / Hard (%s) / Good (%s) / Easy (%s) / Correction ",
200
+                    disptime(review.schedule(pr, st, 0.5)),
201
+                    disptime(review.schedule(pr, st, 1)),
202
+                    disptime(review.schedule(pr, st, 1.5))
163 203
                 ))
164 204
                 local resp = io.read("l")
165 205
                 local sc = succfromstr(resp)
166 206