summaryrefslogtreecommitdiff
path: root/docs/examples/read.lua.html
diff options
context:
space:
mode:
Diffstat (limited to 'docs/examples/read.lua.html')
-rw-r--r--docs/examples/read.lua.html150
1 files changed, 150 insertions, 0 deletions
diff --git a/docs/examples/read.lua.html b/docs/examples/read.lua.html
new file mode 100644
index 0000000..c7697d3
--- /dev/null
+++ b/docs/examples/read.lua.html
@@ -0,0 +1,150 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html>
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
+<head>
+ <title>Lua-System docs</title>
+ <link rel="stylesheet" href="../ldoc.css" type="text/css" />
+</head>
+<body>
+
+<div id="container">
+
+<div id="product">
+ <div id="product_logo"></div>
+ <div id="product_name"><big><b></b></big></div>
+ <div id="product_description"></div>
+</div> <!-- id="product" -->
+
+
+<div id="main">
+
+
+<!-- Menu -->
+
+<div id="navigation">
+<br/>
+<h1>Lua-System</h1>
+
+
+<ul>
+ <li><a href="../index.html">Index</a></li>
+</ul>
+
+
+
+<h2>Examples</h2>
+<ul class="nowrap">
+ <li><a href="../examples/compat.lua.html">compat.lua</a></li>
+ <li><a href="../examples/flag_debugging.lua.html">flag_debugging.lua</a></li>
+ <li><a href="../examples/password_input.lua.html">password_input.lua</a></li>
+ <li><strong>read.lua</strong></li>
+ <li><a href="../examples/readline.lua.html">readline.lua</a></li>
+ <li><a href="../examples/spinner.lua.html">spinner.lua</a></li>
+ <li><a href="../examples/spiral_snake.lua.html">spiral_snake.lua</a></li>
+ <li><a href="../examples/terminalsize.lua.html">terminalsize.lua</a></li>
+</ul>
+<h2>Modules</h2>
+<ul class="nowrap">
+ <li><a href="../modules/system.html">system</a></li>
+</ul>
+<h2>Classes</h2>
+<ul class="nowrap">
+ <li><a href="../classes/bitflags.html">bitflags</a></li>
+</ul>
+<h2>Topics</h2>
+<ul class="">
+ <li><a href="../topics/01-introduction.md.html">1. Introduction</a></li>
+ <li><a href="../topics/02-development.md.html">2. Development</a></li>
+ <li><a href="../topics/03-terminal.md.html">3. Terminal functionality</a></li>
+ <li><a href="../topics/CHANGELOG.md.html">CHANGELOG</a></li>
+ <li><a href="../topics/LICENSE.md.html">MIT License</a></li>
+</ul>
+
+</div>
+
+<div id="content">
+
+ <h2>read.lua</h2>
+<pre>
+<span class="keyword">local</span> sys = <span class="global">require</span> <span class="string">"system"</span>
+
+<span class="global">print</span> <span class="string">[[
+
+This example shows how to do a non-blocking read from the cli.
+
+]]</span>
+
+<span class="comment">-- setup Windows console to handle ANSI processing
+</span><span class="keyword">local</span> of_in = sys.<span class="function-name">getconsoleflags</span>(<span class="global">io</span>.stdin)
+<span class="keyword">local</span> of_out = sys.<span class="function-name">getconsoleflags</span>(<span class="global">io</span>.stdout)
+sys.<span class="function-name">setconsoleflags</span>(<span class="global">io</span>.stdout, sys.<span class="function-name">getconsoleflags</span>(<span class="global">io</span>.stdout) + sys.COF_VIRTUAL_TERMINAL_PROCESSING)
+sys.<span class="function-name">setconsoleflags</span>(<span class="global">io</span>.stdin, sys.<span class="function-name">getconsoleflags</span>(<span class="global">io</span>.stdin) + sys.CIF_VIRTUAL_TERMINAL_INPUT)
+
+<span class="comment">-- setup Posix terminal to use non-blocking mode, and disable line-mode
+</span><span class="keyword">local</span> of_attr = sys.<span class="function-name">tcgetattr</span>(<span class="global">io</span>.stdin)
+<span class="keyword">local</span> of_block = sys.<span class="function-name">getnonblock</span>(<span class="global">io</span>.stdin)
+sys.<span class="function-name">setnonblock</span>(<span class="global">io</span>.stdin, <span class="keyword">true</span>)
+sys.<span class="function-name">tcsetattr</span>(<span class="global">io</span>.stdin, sys.TCSANOW, {
+ lflag = of_attr.lflag - sys.L_ICANON - sys.L_ECHO, <span class="comment">-- disable canonical mode and echo
+</span>})
+
+<span class="comment">-- cursor sequences
+</span><span class="keyword">local</span> get_cursor_pos = <span class="string">"\27[6n"</span>
+
+
+
+<span class="global">print</span>(<span class="string">"Press a key, or 'A' to get cursor position, 'ESC' to exit"</span>)
+<span class="keyword">while</span> <span class="keyword">true</span> <span class="keyword">do</span>
+ <span class="keyword">local</span> key, keytype
+
+ <span class="comment">-- wait for a key
+</span> <span class="keyword">while</span> <span class="keyword">not</span> key <span class="keyword">do</span>
+ key, keytype = sys.<span class="function-name">readansi</span>(<span class="global">math</span>.huge)
+ <span class="keyword">end</span>
+
+ <span class="keyword">if</span> key == <span class="string">"A"</span> <span class="keyword">then</span> <span class="global">io</span>.<span class="function-name">write</span>(get_cursor_pos); <span class="global">io</span>.<span class="function-name">flush</span>() <span class="keyword">end</span>
+
+ <span class="comment">-- check if we got a key or ANSI sequence
+</span> <span class="keyword">if</span> keytype == <span class="string">"char"</span> <span class="keyword">then</span>
+ <span class="comment">-- just a key
+</span> <span class="keyword">local</span> b = key:<span class="function-name">byte</span>()
+ <span class="keyword">if</span> b &lt; <span class="number">32</span> <span class="keyword">then</span>
+ key = <span class="string">"."</span> <span class="comment">-- replace control characters with a simple "." to not mess up the screen
+</span> <span class="keyword">end</span>
+
+ <span class="global">print</span>(<span class="string">"you pressed: "</span> .. key .. <span class="string">" ("</span> .. b .. <span class="string">")"</span>)
+ <span class="keyword">if</span> b == <span class="number">27</span> <span class="keyword">then</span>
+ <span class="global">print</span>(<span class="string">"Escape pressed, exiting"</span>)
+ <span class="keyword">break</span>
+ <span class="keyword">end</span>
+
+ <span class="keyword">elseif</span> keytype == <span class="string">"ansi"</span> <span class="keyword">then</span>
+ <span class="comment">-- we got an ANSI sequence
+</span> <span class="keyword">local</span> seq = { key:<span class="function-name">byte</span>(<span class="number">1</span>, #key) }
+ <span class="global">print</span>(<span class="string">"ANSI sequence received: "</span> .. key:<span class="function-name">sub</span>(<span class="number">2</span>,-<span class="number">1</span>), <span class="string">"(bytes: "</span> .. <span class="global">table</span>.<span class="function-name">concat</span>(seq, <span class="string">", "</span>)..<span class="string">")"</span>)
+
+ <span class="keyword">else</span>
+ <span class="global">print</span>(<span class="string">"unknown key type received: "</span> .. <span class="global">tostring</span>(keytype))
+ <span class="keyword">end</span>
+<span class="keyword">end</span>
+
+
+
+<span class="comment">-- Clean up afterwards
+</span>sys.<span class="function-name">setnonblock</span>(<span class="global">io</span>.stdin, <span class="keyword">false</span>)
+sys.<span class="function-name">setconsoleflags</span>(<span class="global">io</span>.stdout, of_out)
+sys.<span class="function-name">setconsoleflags</span>(<span class="global">io</span>.stdin, of_in)
+sys.<span class="function-name">tcsetattr</span>(<span class="global">io</span>.stdin, sys.TCSANOW, of_attr)
+sys.<span class="function-name">setnonblock</span>(<span class="global">io</span>.stdin, of_block)</pre>
+
+
+</div> <!-- id="content" -->
+</div> <!-- id="main" -->
+<div id="about">
+<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i>
+<i style="float:right;">Last updated 2024-06-20 23:11:37 </i>
+</div> <!-- id="about" -->
+</div> <!-- id="container" -->
+</body>
+</html>