aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThijs <thijs@thijsschreijer.nl>2024-05-21 22:10:36 +0200
committerThijs <thijs@thijsschreijer.nl>2024-05-21 22:19:15 +0200
commiteb310864f70efc4bb46da73f2ee2b9c0d2940c1e (patch)
tree58d6c39f2ab33de74d37b5355cf6f0432dedb4de
parent96e278e67729f1897496916b650e6be337a571d6 (diff)
downloadluasystem-eb310864f70efc4bb46da73f2ee2b9c0d2940c1e.tar.gz
luasystem-eb310864f70efc4bb46da73f2ee2b9c0d2940c1e.tar.bz2
luasystem-eb310864f70efc4bb46da73f2ee2b9c0d2940c1e.zip
add example for termsize
-rw-r--r--examples/terminalsize.lua36
1 files changed, 36 insertions, 0 deletions
diff --git a/examples/terminalsize.lua b/examples/terminalsize.lua
new file mode 100644
index 0000000..78d1910
--- /dev/null
+++ b/examples/terminalsize.lua
@@ -0,0 +1,36 @@
1local sys = require("system")
2
3sys.autotermrestore() -- set up auto restore of terminal settings on exit
4
5-- setup Windows console to handle ANSI processing
6sys.setconsoleflags(io.stdout, sys.getconsoleflags(io.stdout) + sys.COF_VIRTUAL_TERMINAL_PROCESSING)
7sys.setconsoleflags(io.stdin, sys.getconsoleflags(io.stdin) + sys.CIF_VIRTUAL_TERMINAL_INPUT)
8
9-- setup Posix to disable canonical mode and echo
10local of_attr = sys.tcgetattr(io.stdin)
11sys.setnonblock(io.stdin, true)
12sys.tcsetattr(io.stdin, sys.TCSANOW, {
13 lflag = of_attr.lflag - sys.L_ICANON - sys.L_ECHO, -- disable canonical mode and echo
14})
15
16
17-- generate string to move cursor horizontally
18-- positive goes right, negative goes left
19local function cursor_move_horiz(n)
20 if n == 0 then
21 return ""
22 end
23 return "\27[" .. (n > 0 and n or -n) .. (n > 0 and "C" or "D")
24end
25
26
27local w, h
28print("Change the terminal window size, press any key to exit")
29while not sys.readkey(0.2) do
30 local nw, nh = sys.termsize()
31 if w ~= nw or h ~= nh then
32 w, h = nw, nh
33 local text = "Terminal size: " .. w .. "x" .. h .. " "
34 io.write(text .. cursor_move_horiz(-#text))
35 end
36end