aboutsummaryrefslogtreecommitdiff
path: root/examples/spinner.lua
diff options
context:
space:
mode:
Diffstat (limited to 'examples/spinner.lua')
-rw-r--r--examples/spinner.lua64
1 files changed, 64 insertions, 0 deletions
diff --git a/examples/spinner.lua b/examples/spinner.lua
new file mode 100644
index 0000000..5526adc
--- /dev/null
+++ b/examples/spinner.lua
@@ -0,0 +1,64 @@
1local sys = require("system")
2
3print [[
4
5An example to display a spinner, whilst a long running task executes.
6
7]]
8
9
10-- start make backup, to auto-restore on exit
11sys.autotermrestore()
12-- configure console
13sys.setconsoleflags(io.stdin, sys.getconsoleflags(io.stdin) - sys.CIF_ECHO_INPUT - sys.CIF_LINE_INPUT)
14local of = sys.tcgetattr(io.stdin)
15sys.tcsetattr(io.stdin, sys.TCSANOW, { lflag = of.lflag - sys.L_ICANON - sys.L_ECHO })
16sys.setnonblock(io.stdin, true)
17
18
19
20local function hideCursor()
21 io.write("\27[?25l")
22 io.flush()
23end
24
25local function showCursor()
26 io.write("\27[?25h")
27 io.flush()
28end
29
30local function left(n)
31 io.write("\27[",n or 1,"D")
32 io.flush()
33end
34
35
36
37local spinner do
38 local spin = [[|/-\]]
39 local i = 1
40 spinner = function()
41 hideCursor()
42 io.write(spin:sub(i, i))
43 left()
44 i = i + 1
45 if i > #spin then i = 1 end
46
47 if sys.keypressed() then
48 sys.readkey() -- consume key pressed
49 io.write(" ");
50 left()
51 showCursor()
52 return true
53 else
54 return false
55 end
56 end
57end
58
59io.stdout:write("press any key to stop the spinner... ")
60while not spinner() do
61 sys.sleep(0.1)
62end
63
64print("Done!")