summaryrefslogtreecommitdiff
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 @@
+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!")