diff options
author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2024-05-07 19:42:13 +0200 |
---|---|---|
committer | Thijs Schreijer <thijs@thijsschreijer.nl> | 2024-05-07 19:56:24 +0200 |
commit | c8a2d1d2dacde9fea43ebbacf924707d563cf99a (patch) | |
tree | 2be118fc759855635c45ffe048b1d61a2f173248 /examples | |
parent | a22b5c8e14105b9617c8b2000f6353b011d1d0f9 (diff) | |
download | luasystem-c8a2d1d2dacde9fea43ebbacf924707d563cf99a.zip |
make readkey async, and add keyboard parser; ansi + utf8
Diffstat (limited to 'examples')
-rw-r--r-- | examples/read.lua | 65 | ||||
-rw-r--r-- | examples/spinner.lua | 4 |
2 files changed, 10 insertions, 59 deletions
diff --git a/examples/read.lua b/examples/read.lua index 7a1c747..bd5cbff 100644 --- a/examples/read.lua +++ b/examples/read.lua @@ -25,71 +25,19 @@ local get_cursor_pos = "\27[6n" -local read_input do - local left_over_key - - -- Reads a single key, if it is a 27 (start of ansi escape sequence) then it reads - -- the rest of the sequence. - -- This function is non-blocking, and will return nil if no key is available. - -- In case of an ANSI sequence, it will return the full sequence as a string. - -- @return nil|string the key read, or nil if no key is available - function read_input() - if left_over_key then - -- we still have a cached key, return it - local key = left_over_key - left_over_key = nil - return string.char(key) - end - - local key = sys.readkey() - if key == nil then - return nil - end - - if key ~= 27 then - return string.char(key) - end - - -- looks like an ansi escape sequence, immediately read next char - -- as an heuristic against manually typing escape sequences - local brack = sys.readkey() - if brack ~= 91 then - -- not the expected [ character, so we return the key as is - -- and store the extra key read for the next call - left_over_key = brack - return string.char(key) - end - - -- escape sequence detected, read the rest of the sequence - local seq = { key, brack } - while true do - key = sys.readkey() - table.insert(seq, key) - if (key >= 65 and key <= 90) or (key >= 97 and key <= 126) then - -- end of sequence, return the full sequence - return string.char((unpack or table.unpack)(seq)) - end - end - -- unreachable - end -end - - - print("Press a key, or 'A' to get cursor position, 'ESC' to exit") while true do - local key + local key, keytype - -- wait for a key, and sleep a bit to not do a busy-wait + -- wait for a key while not key do - key = read_input() - if not key then sys.sleep(0.1) end + key, keytype = sys.readansi(math.huge) end if key == "A" then io.write(get_cursor_pos); io.flush() end -- check if we got a key or ANSI sequence - if #key == 1 then + if keytype == "key" then -- just a key local b = key:byte() if b < 32 then @@ -102,10 +50,13 @@ while true do break end - else + elseif keytype == "ansi" then -- we got an ANSI sequence local seq = { key:byte(1, #key) } print("ANSI sequence received: " .. key:sub(2,-1), "(bytes: " .. table.concat(seq, ", ")..")") + + else + print("unknown key type received: " .. tostring(keytype)) end end diff --git a/examples/spinner.lua b/examples/spinner.lua index 5526adc..e518e60 100644 --- a/examples/spinner.lua +++ b/examples/spinner.lua @@ -44,8 +44,8 @@ local spinner do i = i + 1 if i > #spin then i = 1 end - if sys.keypressed() then - sys.readkey() -- consume key pressed + if sys.readkey(0) ~= nil then + while sys.readkey(0) ~= nil do end -- consume keys pressed io.write(" "); left() showCursor() |