blob: 5526adca63709260750b0d3c42eae65b669cf5c2 (
plain)
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
|
local sys = require("system")
print [[
An example to display a spinner, whilst a long running task executes.
]]
-- start make backup, to auto-restore on exit
sys.autotermrestore()
-- configure console
sys.setconsoleflags(io.stdin, sys.getconsoleflags(io.stdin) - sys.CIF_ECHO_INPUT - sys.CIF_LINE_INPUT)
local of = sys.tcgetattr(io.stdin)
sys.tcsetattr(io.stdin, sys.TCSANOW, { lflag = of.lflag - sys.L_ICANON - sys.L_ECHO })
sys.setnonblock(io.stdin, true)
local function hideCursor()
io.write("\27[?25l")
io.flush()
end
local function showCursor()
io.write("\27[?25h")
io.flush()
end
local function left(n)
io.write("\27[",n or 1,"D")
io.flush()
end
local spinner do
local spin = [[|/-\]]
local i = 1
spinner = function()
hideCursor()
io.write(spin:sub(i, i))
left()
i = i + 1
if i > #spin then i = 1 end
if sys.keypressed() then
sys.readkey() -- consume key pressed
io.write(" ");
left()
showCursor()
return true
else
return false
end
end
end
io.stdout:write("press any key to stop the spinner... ")
while not spinner() do
sys.sleep(0.1)
end
print("Done!")
|