aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorThijs Schreijer <thijs@thijsschreijer.nl>2024-05-07 19:42:13 +0200
committerThijs Schreijer <thijs@thijsschreijer.nl>2024-05-07 19:56:24 +0200
commitc8a2d1d2dacde9fea43ebbacf924707d563cf99a (patch)
tree2be118fc759855635c45ffe048b1d61a2f173248 /examples
parenta22b5c8e14105b9617c8b2000f6353b011d1d0f9 (diff)
downloadluasystem-c8a2d1d2dacde9fea43ebbacf924707d563cf99a.tar.gz
luasystem-c8a2d1d2dacde9fea43ebbacf924707d563cf99a.tar.bz2
luasystem-c8a2d1d2dacde9fea43ebbacf924707d563cf99a.zip
make readkey async, and add keyboard parser; ansi + utf8
Diffstat (limited to 'examples')
-rw-r--r--examples/read.lua65
-rw-r--r--examples/spinner.lua4
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"
25 25
26 26
27 27
28local 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
75end
76
77
78
79print("Press a key, or 'A' to get cursor position, 'ESC' to exit") 28print("Press a key, or 'A' to get cursor position, 'ESC' to exit")
80while true do 29while 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
110end 61end
111 62
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
44 i = i + 1 44 i = i + 1
45 if i > #spin then i = 1 end 45 if i > #spin then i = 1 end
46 46
47 if sys.keypressed() then 47 if sys.readkey(0) ~= nil then
48 sys.readkey() -- consume key pressed 48 while sys.readkey(0) ~= nil do end -- consume keys pressed
49 io.write(" "); 49 io.write(" ");
50 left() 50 left()
51 showCursor() 51 showCursor()