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/read.lua | |
| parent | a22b5c8e14105b9617c8b2000f6353b011d1d0f9 (diff) | |
| download | luasystem-c8a2d1d2dacde9fea43ebbacf924707d563cf99a.tar.gz luasystem-c8a2d1d2dacde9fea43ebbacf924707d563cf99a.tar.bz2 luasystem-c8a2d1d2dacde9fea43ebbacf924707d563cf99a.zip | |
make readkey async, and add keyboard parser; ansi + utf8
Diffstat (limited to 'examples/read.lua')
| -rw-r--r-- | examples/read.lua | 65 |
1 files changed, 8 insertions, 57 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" | |||
| 25 | 25 | ||
| 26 | 26 | ||
| 27 | 27 | ||
| 28 | local read_input do | ||
| 29 | local left_over_key | ||
| 30 | |||
| 31 | -- Reads a single key, if it is a 27 (start of ansi escape sequence) then it reads | ||
| 32 | -- the rest of the sequence. | ||
| 33 | -- This function is non-blocking, and will return nil if no key is available. | ||
| 34 | -- In case of an ANSI sequence, it will return the full sequence as a string. | ||
| 35 | -- @return nil|string the key read, or nil if no key is available | ||
| 36 | function read_input() | ||
| 37 | if left_over_key then | ||
| 38 | -- we still have a cached key, return it | ||
| 39 | local key = left_over_key | ||
| 40 | left_over_key = nil | ||
| 41 | return string.char(key) | ||
| 42 | end | ||
| 43 | |||
| 44 | local key = sys.readkey() | ||
| 45 | if key == nil then | ||
| 46 | return nil | ||
| 47 | end | ||
| 48 | |||
| 49 | if key ~= 27 then | ||
| 50 | return string.char(key) | ||
| 51 | end | ||
| 52 | |||
| 53 | -- looks like an ansi escape sequence, immediately read next char | ||
| 54 | -- as an heuristic against manually typing escape sequences | ||
| 55 | local brack = sys.readkey() | ||
| 56 | if brack ~= 91 then | ||
| 57 | -- not the expected [ character, so we return the key as is | ||
| 58 | -- and store the extra key read for the next call | ||
| 59 | left_over_key = brack | ||
| 60 | return string.char(key) | ||
| 61 | end | ||
| 62 | |||
| 63 | -- escape sequence detected, read the rest of the sequence | ||
| 64 | local seq = { key, brack } | ||
| 65 | while true do | ||
| 66 | key = sys.readkey() | ||
| 67 | table.insert(seq, key) | ||
| 68 | if (key >= 65 and key <= 90) or (key >= 97 and key <= 126) then | ||
| 69 | -- end of sequence, return the full sequence | ||
| 70 | return string.char((unpack or table.unpack)(seq)) | ||
| 71 | end | ||
| 72 | end | ||
| 73 | -- unreachable | ||
| 74 | end | ||
| 75 | end | ||
| 76 | |||
| 77 | |||
| 78 | |||
| 79 | print("Press a key, or 'A' to get cursor position, 'ESC' to exit") | 28 | print("Press a key, or 'A' to get cursor position, 'ESC' to exit") |
| 80 | while true do | 29 | while true do |
| 81 | local key | 30 | local key, keytype |
| 82 | 31 | ||
| 83 | -- wait for a key, and sleep a bit to not do a busy-wait | 32 | -- wait for a key |
| 84 | while not key do | 33 | while not key do |
| 85 | key = read_input() | 34 | key, keytype = sys.readansi(math.huge) |
| 86 | if not key then sys.sleep(0.1) end | ||
| 87 | end | 35 | end |
| 88 | 36 | ||
| 89 | if key == "A" then io.write(get_cursor_pos); io.flush() end | 37 | if key == "A" then io.write(get_cursor_pos); io.flush() end |
| 90 | 38 | ||
| 91 | -- check if we got a key or ANSI sequence | 39 | -- check if we got a key or ANSI sequence |
| 92 | if #key == 1 then | 40 | if keytype == "key" then |
| 93 | -- just a key | 41 | -- just a key |
| 94 | local b = key:byte() | 42 | local b = key:byte() |
| 95 | if b < 32 then | 43 | if b < 32 then |
| @@ -102,10 +50,13 @@ while true do | |||
| 102 | break | 50 | break |
| 103 | end | 51 | end |
| 104 | 52 | ||
| 105 | else | 53 | elseif keytype == "ansi" then |
| 106 | -- we got an ANSI sequence | 54 | -- we got an ANSI sequence |
| 107 | local seq = { key:byte(1, #key) } | 55 | local seq = { key:byte(1, #key) } |
| 108 | print("ANSI sequence received: " .. key:sub(2,-1), "(bytes: " .. table.concat(seq, ", ")..")") | 56 | print("ANSI sequence received: " .. key:sub(2,-1), "(bytes: " .. table.concat(seq, ", ")..")") |
| 57 | |||
| 58 | else | ||
| 59 | print("unknown key type received: " .. tostring(keytype)) | ||
| 109 | end | 60 | end |
| 110 | end | 61 | end |
| 111 | 62 | ||
