diff options
Diffstat (limited to '')
-rw-r--r-- | CHANGELOG.md | 4 | ||||
-rw-r--r-- | spec/04-term_spec.lua | 26 | ||||
-rw-r--r-- | system/init.lua | 9 |
3 files changed, 36 insertions, 3 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c9e15e..2fd09d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md | |||
@@ -27,6 +27,10 @@ The scope of what is covered by the version number excludes: | |||
27 | 27 | ||
28 | ## Version history | 28 | ## Version history |
29 | 29 | ||
30 | ### version 0.6.0, unreleased | ||
31 | |||
32 | - Fix: when sleep returns an error, pass that on in `readkey`. | ||
33 | |||
30 | ### version 0.5.1, released 12-Mar-2025 | 34 | ### version 0.5.1, released 12-Mar-2025 |
31 | 35 | ||
32 | - Fix: on older unixes with glibc < 2.25, fall back to `/dev/urandom` | 36 | - Fix: on older unixes with glibc < 2.25, fall back to `/dev/urandom` |
diff --git a/spec/04-term_spec.lua b/spec/04-term_spec.lua index 907f903..b3de461 100644 --- a/spec/04-term_spec.lua +++ b/spec/04-term_spec.lua | |||
@@ -855,6 +855,32 @@ describe("Terminal:", function() | |||
855 | assert.is.near(1, timing, 0.5) -- this also works for MacOS in CI | 855 | assert.is.near(1, timing, 0.5) -- this also works for MacOS in CI |
856 | end) | 856 | end) |
857 | 857 | ||
858 | |||
859 | it("calls flseep to execute the sleep", function() | ||
860 | setbuffer("") | ||
861 | |||
862 | local sleep_called = false | ||
863 | local mysleep = function() | ||
864 | sleep_called = true | ||
865 | end | ||
866 | |||
867 | system.readkey(0.01, mysleep) | ||
868 | assert.is_true(sleep_called) | ||
869 | end) | ||
870 | |||
871 | |||
872 | it("returns errors by fsleep", function() | ||
873 | setbuffer("") | ||
874 | |||
875 | local mysleep = function() | ||
876 | return nil, "boom!" | ||
877 | end | ||
878 | |||
879 | local ok, err = system.readkey(1, mysleep) | ||
880 | assert.is.falsy(ok) | ||
881 | assert.equals("boom!", err) | ||
882 | end) | ||
883 | |||
858 | end) | 884 | end) |
859 | 885 | ||
860 | 886 | ||
diff --git a/system/init.lua b/system/init.lua index a81978e..28fe65c 100644 --- a/system/init.lua +++ b/system/init.lua | |||
@@ -236,10 +236,10 @@ do | |||
236 | -- Using `system.readansi` is preferred over this function. Since this function can leave stray/invalid | 236 | -- Using `system.readansi` is preferred over this function. Since this function can leave stray/invalid |
237 | -- byte-sequences in the input buffer, while `system.readansi` reads full ANSI and UTF8 sequences. | 237 | -- byte-sequences in the input buffer, while `system.readansi` reads full ANSI and UTF8 sequences. |
238 | -- @tparam number timeout the timeout in seconds. | 238 | -- @tparam number timeout the timeout in seconds. |
239 | -- @tparam[opt=system.sleep] function fsleep the function to call for sleeping. | 239 | -- @tparam[opt=system.sleep] function fsleep the function to call for sleeping; `ok, err = fsleep(secs)` |
240 | -- @treturn[1] byte the byte value that was read. | 240 | -- @treturn[1] byte the byte value that was read. |
241 | -- @treturn[2] nil if no key was read | 241 | -- @treturn[2] nil if no key was read |
242 | -- @treturn[2] string error message; `"timeout"` if the timeout was reached. | 242 | -- @treturn[2] string error message when the timeout was reached (`"timeout"`), or if `sleep` failed. |
243 | function system.readkey(timeout, fsleep) | 243 | function system.readkey(timeout, fsleep) |
244 | if type(timeout) ~= "number" then | 244 | if type(timeout) ~= "number" then |
245 | error("arg #1 to readkey, expected timeout in seconds, got " .. type(timeout), 2) | 245 | error("arg #1 to readkey, expected timeout in seconds, got " .. type(timeout), 2) |
@@ -248,7 +248,10 @@ do | |||
248 | local interval = 0.0125 | 248 | local interval = 0.0125 |
249 | local key = system._readkey() | 249 | local key = system._readkey() |
250 | while key == nil and timeout > 0 do | 250 | while key == nil and timeout > 0 do |
251 | (fsleep or system.sleep)(math.min(interval, timeout)) | 251 | local ok, err = (fsleep or system.sleep)(math.min(interval, timeout)) |
252 | if not ok then | ||
253 | return nil, err | ||
254 | end | ||
252 | timeout = timeout - interval | 255 | timeout = timeout - interval |
253 | interval = math.min(0.2, interval * 2) | 256 | interval = math.min(0.2, interval * 2) |
254 | key = system._readkey() | 257 | key = system._readkey() |