-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnonBlockInput.lua
68 lines (58 loc) · 1.47 KB
/
nonBlockInput.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require 'posix'
module(..., package.seeall)
local stdin = io.open('/proc/self/fd/0', 'rb')
local initFlag = false
function ttyRaw()
os.execute("stty raw -echo -echoctl -echoe")
initFlag = true
end
function ttySane()
os.execute("stty sane")
initFlag = false
end
function ttyClear()
os.execute("clear")
end
function getChar()
if initFlag == false then ttyRaw() end
local v = posix.rpoll(stdin, 0)
if v > 0 then
local ch = stdin:read(1)
-- Check escape sequences
if ch ~= nil and ch:byte() == 27 then
local buf = ch
ch = stdin:read(1)
buf = buf .. ch
-- CSI ESC + [ + ?
if ch == '[' then
ch = stdin:read(1)
buf = buf .. ch
if ch == 'O' or ch == 'P' then
buf = buf .. stdin:read(1)
elseif ch >= '0' and ch <= '9' then
repeat
ch = stdin:read(1)
buf = buf .. ch
until ch == '~'
end
end
return buf
end
return ch
else
return nil
end
end
function getLine()
if initFlag == true then ttySane() end
local v = posix.rpoll(stdin, 0)
if v == 0 then return nil end
local buf = ''
local ch = nil
while true do
local ch = stdin:read(1)
if ch == '\10' then break end
buf = buf .. ch
end
return buf
end