aboutsummaryrefslogtreecommitdiff
path: root/docs/modules/system.html
diff options
context:
space:
mode:
Diffstat (limited to 'docs/modules/system.html')
-rw-r--r--docs/modules/system.html916
1 files changed, 506 insertions, 410 deletions
diff --git a/docs/modules/system.html b/docs/modules/system.html
index fa41458..0a2447e 100644
--- a/docs/modules/system.html
+++ b/docs/modules/system.html
@@ -37,7 +37,12 @@
37<li><a href="#Environment">Environment </a></li> 37<li><a href="#Environment">Environment </a></li>
38<li><a href="#Random">Random </a></li> 38<li><a href="#Random">Random </a></li>
39<li><a href="#Terminal">Terminal </a></li> 39<li><a href="#Terminal">Terminal </a></li>
40<li><a href="#Terminal_Windows">Terminal_Windows</a></li>
41<li><a href="#Terminal_Posix">Terminal_Posix</a></li>
42<li><a href="#Terminal_Input">Terminal_Input</a></li>
43<li><a href="#Terminal_UTF_8">Terminal_UTF-8</a></li>
40<li><a href="#Time">Time </a></li> 44<li><a href="#Time">Time </a></li>
45<li><a href="#Terminal_Backup">Terminal_Backup</a></li>
41</ul> 46</ul>
42 47
43 48
@@ -112,93 +117,93 @@
112<h2><a href="#Terminal">Terminal </a></h2> 117<h2><a href="#Terminal">Terminal </a></h2>
113<table class="function_list"> 118<table class="function_list">
114 <tr> 119 <tr>
115 <td class="name" nowrap><a href="#CODEPAGE_UTF8">CODEPAGE_UTF8</a></td> 120 <td class="name" nowrap><a href="#isatty">isatty (file)</a></td>
116 <td class="summary">UTF8 codepage.</td> 121 <td class="summary">Checks if a file-handle is a TTY.</td>
117 </tr>
118 <tr>
119 <td class="name" nowrap><a href="#_readkey">_readkey ()</a></td>
120 <td class="summary">Reads a key from the console non-blocking.</td>
121 </tr>
122 <tr>
123 <td class="name" nowrap><a href="#autotermrestore">autotermrestore ()</a></td>
124 <td class="summary">Backs up terminal settings and restores them on application exit.</td>
125 </tr> 122 </tr>
126 <tr> 123 <tr>
127 <td class="name" nowrap><a href="#getconsolecp">getconsolecp ()</a></td> 124 <td class="name" nowrap><a href="#termsize">termsize ()</a></td>
128 <td class="summary">Gets the current console code page (Windows).</td> 125 <td class="summary">Get the size of the terminal in rows and columns.</td>
129 </tr> 126 </tr>
127</table>
128<h2><a href="#Terminal_Windows">Terminal_Windows</a></h2>
129<table class="function_list">
130 <tr> 130 <tr>
131 <td class="name" nowrap><a href="#getconsoleflags">getconsoleflags (file)</a></td> 131 <td class="name" nowrap><a href="#getconsoleflags">getconsoleflags (file)</a></td>
132 <td class="summary">Gets console flags (Windows).</td> 132 <td class="summary">Gets console flags (Windows).</td>
133 </tr> 133 </tr>
134 <tr> 134 <tr>
135 <td class="name" nowrap><a href="#getconsoleoutputcp">getconsoleoutputcp ()</a></td> 135 <td class="name" nowrap><a href="#listconsoleflags">listconsoleflags (fh)</a></td>
136 <td class="summary">Gets the current console output code page (Windows).</td> 136 <td class="summary">Debug function for console flags (Windows).</td>
137 </tr> 137 </tr>
138 <tr> 138 <tr>
139 <td class="name" nowrap><a href="#getnonblock">getnonblock (fd)</a></td> 139 <td class="name" nowrap><a href="#setconsoleflags">setconsoleflags (file, bitflags)</a></td>
140 <td class="summary">Gets non-blocking mode status for a file (Posix).</td> 140 <td class="summary">Sets the console flags (Windows).</td>
141 </tr> 141 </tr>
142</table>
143<h2><a href="#Terminal_Posix">Terminal_Posix</a></h2>
144<table class="function_list">
142 <tr> 145 <tr>
143 <td class="name" nowrap><a href="#isatty">isatty (file)</a></td> 146 <td class="name" nowrap><a href="#detachfds">detachfds ()</a></td>
144 <td class="summary">Checks if a file-handle is a TTY.</td> 147 <td class="summary">Creates new file descriptions for <code>stdout</code> and <code>stderr</code>.</td>
145 </tr> 148 </tr>
146 <tr> 149 <tr>
147 <td class="name" nowrap><a href="#listconsoleflags">listconsoleflags (fh)</a></td> 150 <td class="name" nowrap><a href="#getnonblock">getnonblock (fd)</a></td>
148 <td class="summary">Debug function for console flags (Windows).</td> 151 <td class="summary">Gets non-blocking mode status for a file (Posix).</td>
149 </tr> 152 </tr>
150 <tr> 153 <tr>
151 <td class="name" nowrap><a href="#listtermflags">listtermflags (fh)</a></td> 154 <td class="name" nowrap><a href="#listtermflags">listtermflags (fh)</a></td>
152 <td class="summary">Debug function for terminal flags (Posix).</td> 155 <td class="summary">Debug function for terminal flags (Posix).</td>
153 </tr> 156 </tr>
154 <tr> 157 <tr>
155 <td class="name" nowrap><a href="#readansi">readansi (timeout[, fsleep=system.sleep])</a></td> 158 <td class="name" nowrap><a href="#setnonblock">setnonblock (fd, make_non_block)</a></td>
156 <td class="summary">Reads a single key, if it is the start of ansi escape sequence then it reads 159 <td class="summary">Enables or disables non-blocking mode for a file (Posix).</td>
157 the full sequence.</td>
158 </tr>
159 <tr>
160 <td class="name" nowrap><a href="#readkey">readkey (timeout[, fsleep=system.sleep])</a></td>
161 <td class="summary">Reads a single byte from the console, with a timeout.</td>
162 </tr> 160 </tr>
163 <tr> 161 <tr>
164 <td class="name" nowrap><a href="#setconsolecp">setconsolecp (cp)</a></td> 162 <td class="name" nowrap><a href="#tcgetattr">tcgetattr (fd)</a></td>
165 <td class="summary">Sets the current console code page (Windows).</td> 163 <td class="summary">Get termios state (Posix).</td>
166 </tr> 164 </tr>
167 <tr> 165 <tr>
168 <td class="name" nowrap><a href="#setconsoleflags">setconsoleflags (file, bitflags)</a></td> 166 <td class="name" nowrap><a href="#tcsetattr">tcsetattr (fd, actions, termios)</a></td>
169 <td class="summary">Sets the console flags (Windows).</td> 167 <td class="summary">Set termios state (Posix).</td>
170 </tr> 168 </tr>
169</table>
170<h2><a href="#Terminal_Input">Terminal_Input</a></h2>
171<table class="function_list">
171 <tr> 172 <tr>
172 <td class="name" nowrap><a href="#setconsoleoutputcp">setconsoleoutputcp (cp)</a></td> 173 <td class="name" nowrap><a href="#_readkey">_readkey ()</a></td>
173 <td class="summary">Sets the current console output code page (Windows).</td> 174 <td class="summary">Reads a key from the console non-blocking.</td>
174 </tr> 175 </tr>
175 <tr> 176 <tr>
176 <td class="name" nowrap><a href="#setnonblock">setnonblock (fd, make_non_block)</a></td> 177 <td class="name" nowrap><a href="#readansi">readansi (timeout[, fsleep=system.sleep])</a></td>
177 <td class="summary">Enables or disables non-blocking mode for a file (Posix).</td> 178 <td class="summary">Reads a single key, if it is the start of ansi escape sequence then it reads
179 the full sequence.</td>
178 </tr> 180 </tr>
179 <tr> 181 <tr>
180 <td class="name" nowrap><a href="#tcgetattr">tcgetattr (fd)</a></td> 182 <td class="name" nowrap><a href="#readkey">readkey (timeout[, fsleep=system.sleep])</a></td>
181 <td class="summary">Get termios state (Posix).</td> 183 <td class="summary">Reads a single byte from the console, with a timeout.</td>
182 </tr> 184 </tr>
185</table>
186<h2><a href="#Terminal_UTF_8">Terminal_UTF-8</a></h2>
187<table class="function_list">
183 <tr> 188 <tr>
184 <td class="name" nowrap><a href="#tcsetattr">tcsetattr (fd, actions, termios)</a></td> 189 <td class="name" nowrap><a href="#CODEPAGE_UTF8">CODEPAGE_UTF8</a></td>
185 <td class="summary">Set termios state (Posix).</td> 190 <td class="summary">UTF8 codepage.</td>
186 </tr> 191 </tr>
187 <tr> 192 <tr>
188 <td class="name" nowrap><a href="#termbackup">termbackup ()</a></td> 193 <td class="name" nowrap><a href="#getconsolecp">getconsolecp ()</a></td>
189 <td class="summary">Returns a backup of terminal settings for stdin/out/err.</td> 194 <td class="summary">Gets the current console code page (Windows).</td>
190 </tr> 195 </tr>
191 <tr> 196 <tr>
192 <td class="name" nowrap><a href="#termrestore">termrestore (backup)</a></td> 197 <td class="name" nowrap><a href="#getconsoleoutputcp">getconsoleoutputcp ()</a></td>
193 <td class="summary">Restores terminal settings from a backup</td> 198 <td class="summary">Gets the current console output code page (Windows).</td>
194 </tr> 199 </tr>
195 <tr> 200 <tr>
196 <td class="name" nowrap><a href="#termsize">termsize ()</a></td> 201 <td class="name" nowrap><a href="#setconsolecp">setconsolecp (cp)</a></td>
197 <td class="summary">Get the size of the terminal in rows and columns.</td> 202 <td class="summary">Sets the current console code page (Windows).</td>
198 </tr> 203 </tr>
199 <tr> 204 <tr>
200 <td class="name" nowrap><a href="#termwrap">termwrap (f)</a></td> 205 <td class="name" nowrap><a href="#setconsoleoutputcp">setconsoleoutputcp (cp)</a></td>
201 <td class="summary">Wraps a function to automatically restore terminal settings upon returning.</td> 206 <td class="summary">Sets the current console output code page (Windows).</td>
202 </tr> 207 </tr>
203 <tr> 208 <tr>
204 <td class="name" nowrap><a href="#utf8cwidth">utf8cwidth (utf8_char)</a></td> 209 <td class="name" nowrap><a href="#utf8cwidth">utf8cwidth (utf8_char)</a></td>
@@ -224,6 +229,25 @@
224 <td class="summary">Sleep without a busy loop.</td> 229 <td class="summary">Sleep without a busy loop.</td>
225 </tr> 230 </tr>
226</table> 231</table>
232<h2><a href="#Terminal_Backup">Terminal_Backup</a></h2>
233<table class="function_list">
234 <tr>
235 <td class="name" nowrap><a href="#autotermrestore">autotermrestore ()</a></td>
236 <td class="summary">Backs up terminal settings and restores them on application exit.</td>
237 </tr>
238 <tr>
239 <td class="name" nowrap><a href="#termbackup">termbackup ()</a></td>
240 <td class="summary">Returns a backup of terminal settings for stdin/out/err.</td>
241 </tr>
242 <tr>
243 <td class="name" nowrap><a href="#termrestore">termrestore (backup)</a></td>
244 <td class="summary">Restores terminal settings from a backup</td>
245 </tr>
246 <tr>
247 <td class="name" nowrap><a href="#termwrap">termwrap (f)</a></td>
248 <td class="summary">Wraps a function to automatically restore terminal settings upon returning.</td>
249 </tr>
250</table>
227 251
228<br/> 252<br/>
229<br/> 253<br/>
@@ -399,95 +423,66 @@ requested number of bytes, or an error, never a partial result.
399 </div> 423 </div>
400 <dl class="function"> 424 <dl class="function">
401 <dt> 425 <dt>
402 <a name = "CODEPAGE_UTF8"></a> 426 <a name = "isatty"></a>
403 <strong>CODEPAGE_UTF8</strong> 427 <strong>isatty (file)</strong>
404 </dt> 428 </dt>
405 <dd> 429 <dd>
406 UTF8 codepage. 430 Checks if a file-handle is a TTY.
407 To be used with <a href="../modules/system.html#setconsoleoutputcp">system.setconsoleoutputcp</a> and <a href="../modules/system.html#setconsolecp">system.setconsolecp</a>.
408 431
409 432
433 <h3>Parameters:</h3>
410 <ul> 434 <ul>
411 <li><span class="parameter">CODEPAGE_UTF8</span> 435 <li><span class="parameter">file</span>
412 The Windows CodePage for UTF8. 436 <span class="types"><span class="type">file</span></span>
437 the file-handle to check, one of <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stdin">io.stdin</a>, <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stdout">io.stdout</a>, <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stderr">io.stderr</a>.
413 </li> 438 </li>
414 </ul> 439 </ul>
415 440
416
417
418
419
420</dd>
421 <dt>
422 <a name = "_readkey"></a>
423 <strong>_readkey ()</strong>
424 </dt>
425 <dd>
426 Reads a key from the console non-blocking. This function should not be called
427directly, but through the <a href="../modules/system.html#readkey">system.readkey</a> or <a href="../modules/system.html#readansi">system.readansi</a> functions. It
428will return the next byte from the input stream, or <code>nil</code> if no key was pressed.</p>
429
430<p>On Posix, <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stdin">io.stdin</a> must be set to non-blocking mode using <a href="../modules/system.html#setnonblock">setnonblock</a>
431and canonical mode must be turned off using <a href="../modules/system.html#tcsetattr">tcsetattr</a>,
432before calling this function. Otherwise it will block. No conversions are
433done on Posix, so the byte read is returned as-is.</p>
434
435<p>On Windows this reads a wide character and converts it to UTF-8. Multi-byte
436sequences will be buffered internally and returned one byte at a time.
437
438
439
440 <h3>Returns:</h3> 441 <h3>Returns:</h3>
441 <ol> 442 <ol>
442 443
443 <span class="types"><span class="type">integer</span></span> 444 <span class="types"><span class="type">boolean</span></span>
444 the byte read from the input stream 445 true if the file is a tty
445 </ol>
446 <h3>Or</h3>
447 <ol>
448
449 <span class="types"><span class="type">nil</span></span>
450 if no key was pressed
451 </ol>
452 <h3>Or</h3>
453 <ol>
454 <li>
455 <span class="types"><span class="type">nil</span></span>
456 on error</li>
457 <li>
458 <span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.4">string</a></span>
459 error message</li>
460 <li>
461 <span class="types"><span class="type">int</span></span>
462 errnum (on posix)</li>
463 </ol> 446 </ol>
464 447
465 448
466 449
450 <h3>Usage:</h3>
451 <ul>
452 <pre class="example"><span class="keyword">local</span> system = <span class="global">require</span>(<span class="string">'system'</span>)
453<span class="keyword">if</span> system.<span class="function-name">isatty</span>(<span class="global">io</span>.stdin) <span class="keyword">then</span>
454 <span class="comment">-- enable ANSI coloring etc on Windows, does nothing in Posix.
455</span> <span class="keyword">local</span> flags = system.<span class="function-name">getconsoleflags</span>(<span class="global">io</span>.stdout)
456 system.<span class="function-name">setconsoleflags</span>(<span class="global">io</span>.stdout, flags + sys.COF_VIRTUAL_TERMINAL_PROCESSING)
457<span class="keyword">end</span></pre>
458 </ul>
467 459
468</dd> 460</dd>
469 <dt> 461 <dt>
470 <a name = "autotermrestore"></a> 462 <a name = "termsize"></a>
471 <strong>autotermrestore ()</strong> 463 <strong>termsize ()</strong>
472 </dt> 464 </dt>
473 <dd> 465 <dd>
474 Backs up terminal settings and restores them on application exit. 466 Get the size of the terminal in rows and columns.
475 Calls <a href="../modules/system.html#termbackup">termbackup</a> to back up terminal settings and sets up a GC method to
476 automatically restore them on application exit (also works on Lua 5.1).
477 467
478 468
479 469
480 <h3>Returns:</h3> 470 <h3>Returns:</h3>
481 <ol> 471 <ol>
482 472 <li>
483 <span class="types"><span class="type">boolean</span></span> 473 <span class="types"><span class="type">int</span></span>
484 true 474 the number of rows</li>
475 <li>
476 <span class="types"><span class="type">int</span></span>
477 the number of columns</li>
485 </ol> 478 </ol>
486 <h3>Or</h3> 479 <h3>Or</h3>
487 <ol> 480 <ol>
488 <li> 481 <li>
489 <span class="types"><span class="type">nil</span></span> 482 <span class="types"><span class="type">nil</span></span>
490 if the backup was already created</li> 483
484
485</li>
491 <li> 486 <li>
492 <span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.4">string</a></span> 487 <span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.4">string</a></span>
493 error message</li> 488 error message</li>
@@ -497,26 +492,10 @@ sequences will be buffered internally and returned one byte at a time.
497 492
498 493
499</dd> 494</dd>
500 <dt> 495</dl>
501 <a name = "getconsolecp"></a> 496 <h2 class="section-header "><a name="Terminal_Windows"></a>Terminal_Windows</h2>
502 <strong>getconsolecp ()</strong>
503 </dt>
504 <dd>
505 Gets the current console code page (Windows).
506
507
508
509 <h3>Returns:</h3>
510 <ol>
511
512 <span class="types"><span class="type">int</span></span>
513 the current code page (always 65001 on Posix systems)
514 </ol>
515
516
517
518 497
519</dd> 498 <dl class="function">
520 <dt> 499 <dt>
521 <a name = "getconsoleflags"></a> 500 <a name = "getconsoleflags"></a>
522 <strong>getconsoleflags (file)</strong> 501 <strong>getconsoleflags (file)</strong>
@@ -575,103 +554,6 @@ for more information on the flags.
575 554
576</dd> 555</dd>
577 <dt> 556 <dt>
578 <a name = "getconsoleoutputcp"></a>
579 <strong>getconsoleoutputcp ()</strong>
580 </dt>
581 <dd>
582 Gets the current console output code page (Windows).
583
584
585
586 <h3>Returns:</h3>
587 <ol>
588
589 <span class="types"><span class="type">int</span></span>
590 the current code page (always 65001 on Posix systems)
591 </ol>
592
593
594
595
596</dd>
597 <dt>
598 <a name = "getnonblock"></a>
599 <strong>getnonblock (fd)</strong>
600 </dt>
601 <dd>
602 Gets non-blocking mode status for a file (Posix).
603
604
605 <h3>Parameters:</h3>
606 <ul>
607 <li><span class="parameter">fd</span>
608 <span class="types"><span class="type">file</span></span>
609 file handle to operate on, one of <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stdin">io.stdin</a>, <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stdout">io.stdout</a>, <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stderr">io.stderr</a>
610 </li>
611 </ul>
612
613 <h3>Returns:</h3>
614 <ol>
615
616 <span class="types"><span class="type">bool</span></span>
617 <code>true</code> if set to non-blocking, <code>false</code> if not. Always returns <code>false</code> on Windows.
618 </ol>
619 <h3>Or</h3>
620 <ol>
621 <li>
622 <span class="types"><span class="type">nil</span></span>
623
624
625</li>
626 <li>
627 <span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.4">string</a></span>
628 error message</li>
629 <li>
630 <span class="types"><span class="type">int</span></span>
631 errnum</li>
632 </ol>
633
634
635
636
637</dd>
638 <dt>
639 <a name = "isatty"></a>
640 <strong>isatty (file)</strong>
641 </dt>
642 <dd>
643 Checks if a file-handle is a TTY.
644
645
646 <h3>Parameters:</h3>
647 <ul>
648 <li><span class="parameter">file</span>
649 <span class="types"><span class="type">file</span></span>
650 the file-handle to check, one of <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stdin">io.stdin</a>, <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stdout">io.stdout</a>, <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stderr">io.stderr</a>.
651 </li>
652 </ul>
653
654 <h3>Returns:</h3>
655 <ol>
656
657 <span class="types"><span class="type">boolean</span></span>
658 true if the file is a tty
659 </ol>
660
661
662
663 <h3>Usage:</h3>
664 <ul>
665 <pre class="example"><span class="keyword">local</span> system = <span class="global">require</span>(<span class="string">'system'</span>)
666<span class="keyword">if</span> system.<span class="function-name">isatty</span>(<span class="global">io</span>.stdin) <span class="keyword">then</span>
667 <span class="comment">-- enable ANSI coloring etc on Windows, does nothing in Posix.
668</span> <span class="keyword">local</span> flags = system.<span class="function-name">getconsoleflags</span>(<span class="global">io</span>.stdout)
669 system.<span class="function-name">setconsoleflags</span>(<span class="global">io</span>.stdout, flags + sys.COF_VIRTUAL_TERMINAL_PROCESSING)
670<span class="keyword">end</span></pre>
671 </ul>
672
673</dd>
674 <dt>
675 <a name = "listconsoleflags"></a> 557 <a name = "listconsoleflags"></a>
676 <strong>listconsoleflags (fh)</strong> 558 <strong>listconsoleflags (fh)</strong>
677 </dt> 559 </dt>
@@ -700,192 +582,125 @@ system.<span class="function-name">listconsoleflags</span>(<span class="global">
700 582
701</dd> 583</dd>
702 <dt> 584 <dt>
703 <a name = "listtermflags"></a> 585 <a name = "setconsoleflags"></a>
704 <strong>listtermflags (fh)</strong> 586 <strong>setconsoleflags (file, bitflags)</strong>
705 </dt> 587 </dt>
706 <dd> 588 <dd>
707 Debug function for terminal flags (Posix). 589 Sets the console flags (Windows).
708 Pretty prints the current flags set for the handle. 590The <code>CIF_</code> and <code>COF_</code> constants are available on the module table. Where <code>CIF</code> are the
709 591input flags (for use with <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stdin">io.stdin</a>) and <code>COF</code> are the output flags (for use with
710 592<a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stdout">io.stdout</a>/<a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stderr">io.stderr</a>).</p>
711 <h3>Parameters:</h3>
712 <ul>
713 <li><span class="parameter">fh</span>
714 file handle (<a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stdin">io.stdin</a>, <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stdout">io.stdout</a>, <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stderr">io.stderr</a>)
715 </li>
716 </ul>
717
718
719
720 593
721 <h3>Usage:</h3> 594<p>To see flag status and constant names check <a href="../modules/system.html#listconsoleflags">listconsoleflags</a>.</p>
722 <ul>
723 <pre class="example"><span class="comment">-- Print the flags for stdin/out/err
724</span>system.<span class="function-name">listconsoleflags</span>(<span class="global">io</span>.stdin)
725system.<span class="function-name">listconsoleflags</span>(<span class="global">io</span>.stdout)
726system.<span class="function-name">listconsoleflags</span>(<span class="global">io</span>.stderr)</pre>
727 </ul>
728 595
729</dd> 596<p>Note: not all combinations of flags are allowed, as some are mutually exclusive or mutually required.
730 <dt> 597See <a href="https://learn.microsoft.com/en-us/windows/console/setconsolemode">setconsolemode documentation</a>
731 <a name = "readansi"></a>
732 <strong>readansi (timeout[, fsleep=system.sleep])</strong>
733 </dt>
734 <dd>
735 Reads a single key, if it is the start of ansi escape sequence then it reads
736 the full sequence. The key can be a multi-byte string in case of multibyte UTF-8 character.
737 This function uses <a href="../modules/system.html#readkey">system.readkey</a>, and hence <code>fsleep</code> to wait until either a key is
738 available or the timeout is reached.
739 It returns immediately if a key is available or if <code>timeout</code> is less than or equal to <code>0</code>.
740 In case of an ANSI sequence, it will return the full sequence as a string.
741 598
742 599
743 <h3>Parameters:</h3> 600 <h3>Parameters:</h3>
744 <ul> 601 <ul>
745 <li><span class="parameter">timeout</span> 602 <li><span class="parameter">file</span>
746 <span class="types"><span class="type">number</span></span> 603 <span class="types"><span class="type">file</span></span>
747 the timeout in seconds. 604 file handle to operate on, one of <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stdin">io.stdin</a>, <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stdout">io.stdout</a>, <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stderr">io.stderr</a>
748 </li> 605 </li>
749 <li><span class="parameter">fsleep</span> 606 <li><span class="parameter">bitflags</span>
750 <span class="types"><span class="type">function</span></span> 607 <span class="types"><a class="type" href="../classes/bitflags.html#">bitflags</a></span>
751 the function to call for sleeping. 608 the flags to set/unset
752 (<em>default</em> system.sleep)
753 </li> 609 </li>
754 </ul> 610 </ul>
755 611
756 <h3>Returns:</h3> 612 <h3>Returns:</h3>
757 <ol> 613 <ol>
758 <li> 614
759 <span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.4">string</a></span> 615 <span class="types"><span class="type">boolean</span></span>
760 the character that was received (can be multi-byte), or a complete ANSI sequence</li> 616 <code>true</code> on success
761 <li>
762 <span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.4">string</a></span>
763 the type of input: <code>&quot;char&quot;</code> for a single key, <code>&quot;ansi&quot;</code> for an ANSI sequence</li>
764 </ol> 617 </ol>
765 <h3>Or</h3> 618 <h3>Or</h3>
766 <ol> 619 <ol>
767 <li> 620 <li>
768 <span class="types"><span class="type">nil</span></span> 621 <span class="types"><span class="type">nil</span></span>
769 in case of an error</li> 622
770 <li> 623
771 <span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.4">string</a></span> 624</li>
772 error message; <code>&quot;timeout&quot;</code> if the timeout was reached.</li>
773 <li> 625 <li>
774 <span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.4">string</a></span> 626 <span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.4">string</a></span>
775 partial result in case of an error while reading a sequence, the sequence so far.</li> 627 error message</li>
776 </ol> 628 </ol>
777 629
778 630
779 631
632 <h3>Usage:</h3>
633 <ul>
634 <pre class="example"><span class="keyword">local</span> system = <span class="global">require</span>(<span class="string">'system'</span>)
635system.<span class="function-name">listconsoleflags</span>(<span class="global">io</span>.stdout) <span class="comment">-- List all the available flags and their current status
636</span>
637<span class="keyword">local</span> flags = system.<span class="function-name">getconsoleflags</span>(<span class="global">io</span>.stdout)
638<span class="global">assert</span>(system.<span class="function-name">setconsoleflags</span>(<span class="global">io</span>.stdout,
639 flags + system.COF_VIRTUAL_TERMINAL_PROCESSING)
640
641system.<span class="function-name">listconsoleflags</span>(<span class="global">io</span>.stdout) <span class="comment">-- List again to check the differences</span></pre>
642 </ul>
780 643
781</dd> 644</dd>
645</dl>
646 <h2 class="section-header "><a name="Terminal_Posix"></a>Terminal_Posix</h2>
647
648 <dl class="function">
782 <dt> 649 <dt>
783 <a name = "readkey"></a> 650 <a name = "detachfds"></a>
784 <strong>readkey (timeout[, fsleep=system.sleep])</strong> 651 <strong>detachfds ()</strong>
785 </dt> 652 </dt>
786 <dd> 653 <dd>
787 Reads a single byte from the console, with a timeout. 654 Creates new file descriptions for <code>stdout</code> and <code>stderr</code>.
788 This function uses <code>fsleep</code> to wait until either a byte is available or the timeout is reached. 655Even if the file descriptors are unique, they still might point to the same
789 The sleep period is exponentially backing off, starting at 0.0125 seconds, with a maximum of 0.2 seconds. 656file description, and hence share settings like <code>O_NONBLOCK</code>. This means that
790 It returns immediately if a byte is available or if <code>timeout</code> is less than or equal to <code>0</code>.</p> 657if one of them is set to non-blocking, the other will be as well. This can
658lead to unexpected behavior.</p>
791 659
792<p> Using <a href="../modules/system.html#readansi">system.readansi</a> is preferred over this function. Since this function can leave stray/invalid 660<p>This function is used to detach <code>stdout</code> and <code>stderr</code> from the original
793 byte-sequences in the input buffer, while <a href="../modules/system.html#readansi">system.readansi</a> reads full ANSI and UTF8 sequences. 661file descriptions, and create new file descriptions for them. This allows
662independent control of flags (e.g., <code>O_NONBLOCK</code>) on <code>stdout</code> and <code>stderr</code>,
663avoiding shared side effects.</p>
664
665<p>Does not modify <code>stdin</code> (fd 0), and does nothing on Windows.
794 666
795 667
796 <h3>Parameters:</h3>
797 <ul>
798 <li><span class="parameter">timeout</span>
799 <span class="types"><span class="type">number</span></span>
800 the timeout in seconds.
801 </li>
802 <li><span class="parameter">fsleep</span>
803 <span class="types"><span class="type">function</span></span>
804 the function to call for sleeping.
805 (<em>default</em> system.sleep)
806 </li>
807 </ul>
808 668
809 <h3>Returns:</h3> 669 <h3>Returns:</h3>
810 <ol> 670 <ol>
811 671
812 <span class="types"><span class="type">byte</span></span> 672 boolean <code>true</code> on success, or throws an error on failure.
813 the byte value that was read.
814 </ol> 673 </ol>
815 <h3>Or</h3>
816 <ol>
817 <li>
818 <span class="types"><span class="type">nil</span></span>
819 if no key was read</li>
820 <li>
821 <span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.4">string</a></span>
822 error message; <code>&quot;timeout&quot;</code> if the timeout was reached.</li>
823 </ol>
824
825 674
826 675
827 676 <h3>See also:</h3>
828</dd>
829 <dt>
830 <a name = "setconsolecp"></a>
831 <strong>setconsolecp (cp)</strong>
832 </dt>
833 <dd>
834 Sets the current console code page (Windows).
835
836
837 <h3>Parameters:</h3>
838 <ul> 677 <ul>
839 <li><span class="parameter">cp</span> 678 <a href="../modules/system.html#setnonblock">setnonblock</a>
840 <span class="types"><span class="type">int</span></span>
841 the code page to set, use <a href="../modules/system.html#CODEPAGE_UTF8">system.CODEPAGE_UTF8</a> (65001) for UTF-8
842 </li>
843 </ul> 679 </ul>
844 680
845 <h3>Returns:</h3>
846 <ol>
847
848 <span class="types"><span class="type">bool</span></span>
849 <code>true</code> on success (always <code>true</code> on Posix systems)
850 </ol>
851
852
853
854 681
855</dd> 682</dd>
856 <dt> 683 <dt>
857 <a name = "setconsoleflags"></a> 684 <a name = "getnonblock"></a>
858 <strong>setconsoleflags (file, bitflags)</strong> 685 <strong>getnonblock (fd)</strong>
859 </dt> 686 </dt>
860 <dd> 687 <dd>
861 Sets the console flags (Windows). 688 Gets non-blocking mode status for a file (Posix).
862The <code>CIF_</code> and <code>COF_</code> constants are available on the module table. Where <code>CIF</code> are the
863input flags (for use with <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stdin">io.stdin</a>) and <code>COF</code> are the output flags (for use with
864<a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stdout">io.stdout</a>/<a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stderr">io.stderr</a>).</p>
865
866<p>To see flag status and constant names check <a href="../modules/system.html#listconsoleflags">listconsoleflags</a>.</p>
867
868<p>Note: not all combinations of flags are allowed, as some are mutually exclusive or mutually required.
869See <a href="https://learn.microsoft.com/en-us/windows/console/setconsolemode">setconsolemode documentation</a>
870 689
871 690
872 <h3>Parameters:</h3> 691 <h3>Parameters:</h3>
873 <ul> 692 <ul>
874 <li><span class="parameter">file</span> 693 <li><span class="parameter">fd</span>
875 <span class="types"><span class="type">file</span></span> 694 <span class="types"><span class="type">file</span></span>
876 file handle to operate on, one of <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stdin">io.stdin</a>, <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stdout">io.stdout</a>, <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stderr">io.stderr</a> 695 file handle to operate on, one of <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stdin">io.stdin</a>, <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stdout">io.stdout</a>, <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stderr">io.stderr</a>
877 </li> 696 </li>
878 <li><span class="parameter">bitflags</span>
879 <span class="types"><a class="type" href="../classes/bitflags.html#">bitflags</a></span>
880 the flags to set/unset
881 </li>
882 </ul> 697 </ul>
883 698
884 <h3>Returns:</h3> 699 <h3>Returns:</h3>
885 <ol> 700 <ol>
886 701
887 <span class="types"><span class="type">boolean</span></span> 702 <span class="types"><span class="type">bool</span></span>
888 <code>true</code> on success 703 <code>true</code> if set to non-blocking, <code>false</code> if not. Always returns <code>false</code> on Windows.
889 </ol> 704 </ol>
890 <h3>Or</h3> 705 <h3>Or</h3>
891 <ol> 706 <ol>
@@ -897,48 +712,45 @@ See <a href="https://learn.microsoft.com/en-us/windows/console/setconsolemode">s
897 <li> 712 <li>
898 <span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.4">string</a></span> 713 <span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.4">string</a></span>
899 error message</li> 714 error message</li>
715 <li>
716 <span class="types"><span class="type">int</span></span>
717 errnum</li>
900 </ol> 718 </ol>
901 719
902 720
903 721 <h3>See also:</h3>
904 <h3>Usage:</h3>
905 <ul> 722 <ul>
906 <pre class="example"><span class="keyword">local</span> system = <span class="global">require</span>(<span class="string">'system'</span>) 723 <a href="../modules/system.html#setnonblock">setnonblock</a>
907system.<span class="function-name">listconsoleflags</span>(<span class="global">io</span>.stdout) <span class="comment">-- List all the available flags and their current status
908</span>
909<span class="keyword">local</span> flags = system.<span class="function-name">getconsoleflags</span>(<span class="global">io</span>.stdout)
910<span class="global">assert</span>(system.<span class="function-name">setconsoleflags</span>(<span class="global">io</span>.stdout,
911 flags + system.COF_VIRTUAL_TERMINAL_PROCESSING)
912
913system.<span class="function-name">listconsoleflags</span>(<span class="global">io</span>.stdout) <span class="comment">-- List again to check the differences</span></pre>
914 </ul> 724 </ul>
915 725
726
916</dd> 727</dd>
917 <dt> 728 <dt>
918 <a name = "setconsoleoutputcp"></a> 729 <a name = "listtermflags"></a>
919 <strong>setconsoleoutputcp (cp)</strong> 730 <strong>listtermflags (fh)</strong>
920 </dt> 731 </dt>
921 <dd> 732 <dd>
922 Sets the current console output code page (Windows). 733 Debug function for terminal flags (Posix).
734 Pretty prints the current flags set for the handle.
923 735
924 736
925 <h3>Parameters:</h3> 737 <h3>Parameters:</h3>
926 <ul> 738 <ul>
927 <li><span class="parameter">cp</span> 739 <li><span class="parameter">fh</span>
928 <span class="types"><span class="type">int</span></span> 740 file handle (<a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stdin">io.stdin</a>, <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stdout">io.stdout</a>, <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stderr">io.stderr</a>)
929 the code page to set, use <a href="../modules/system.html#CODEPAGE_UTF8">system.CODEPAGE_UTF8</a> (65001) for UTF-8
930 </li> 741 </li>
931 </ul> 742 </ul>
932 743
933 <h3>Returns:</h3>
934 <ol>
935
936 <span class="types"><span class="type">bool</span></span>
937 <code>true</code> on success (always <code>true</code> on Posix systems)
938 </ol>
939 744
940 745
941 746
747 <h3>Usage:</h3>
748 <ul>
749 <pre class="example"><span class="comment">-- Print the flags for stdin/out/err
750</span>system.<span class="function-name">listconsoleflags</span>(<span class="global">io</span>.stdin)
751system.<span class="function-name">listconsoleflags</span>(<span class="global">io</span>.stdout)
752system.<span class="function-name">listconsoleflags</span>(<span class="global">io</span>.stderr)</pre>
753 </ul>
942 754
943</dd> 755</dd>
944 <dt> 756 <dt>
@@ -947,6 +759,7 @@ system.<span class="function-name">listconsoleflags</span>(<span class="global">
947 </dt> 759 </dt>
948 <dd> 760 <dd>
949 Enables or disables non-blocking mode for a file (Posix). 761 Enables or disables non-blocking mode for a file (Posix).
762Check <a href="../modules/system.html#detachfds">detachfds</a> in case there are shared file descriptions.
950 763
951 764
952 <h3>Parameters:</h3> 765 <h3>Parameters:</h3>
@@ -985,13 +798,15 @@ system.<span class="function-name">listconsoleflags</span>(<span class="global">
985 798
986 <h3>See also:</h3> 799 <h3>See also:</h3>
987 <ul> 800 <ul>
988 <a href="../modules/system.html#getnonblock">getnonblock</a> 801 <li><a href="../modules/system.html#getnonblock">getnonblock</a></li>
802 <li><a href="../modules/system.html#detachfds">detachfds</a></li>
989 </ul> 803 </ul>
990 804
991 <h3>Usage:</h3> 805 <h3>Usage:</h3>
992 <ul> 806 <ul>
993 <pre class="example"><span class="keyword">local</span> sys = <span class="global">require</span>(<span class="string">'system'</span>) 807 <pre class="example"><span class="keyword">local</span> sys = <span class="global">require</span>(<span class="string">'system'</span>)
994 808sys.<span class="function-name">detachfds</span>() <span class="comment">-- detach stdout and stderr, so only stdin becomes non-blocking
809</span>
995<span class="comment">-- set io.stdin to non-blocking mode 810<span class="comment">-- set io.stdin to non-blocking mode
996</span><span class="keyword">local</span> old_setting = sys.<span class="function-name">getnonblock</span>(<span class="global">io</span>.stdin) 811</span><span class="keyword">local</span> old_setting = sys.<span class="function-name">getnonblock</span>(<span class="global">io</span>.stdin)
997sys.<span class="function-name">setnonblock</span>(<span class="global">io</span>.stdin, <span class="keyword">true</span>) 812sys.<span class="function-name">setnonblock</span>(<span class="global">io</span>.stdin, <span class="keyword">true</span>)
@@ -1151,21 +966,52 @@ flags for the <code>iflags</code>, <code>oflags</code>, and <code>lflags</code>
1151 </ul> 966 </ul>
1152 967
1153</dd> 968</dd>
969</dl>
970 <h2 class="section-header "><a name="Terminal_Input"></a>Terminal_Input</h2>
971
972 <dl class="function">
1154 <dt> 973 <dt>
1155 <a name = "termbackup"></a> 974 <a name = "_readkey"></a>
1156 <strong>termbackup ()</strong> 975 <strong>_readkey ()</strong>
1157 </dt> 976 </dt>
1158 <dd> 977 <dd>
1159 Returns a backup of terminal settings for stdin/out/err. 978 Reads a key from the console non-blocking. This function should not be called
1160 Handles terminal/console flags, Windows codepage, and non-block flags on the streams. 979directly, but through the <a href="../modules/system.html#readkey">system.readkey</a> or <a href="../modules/system.html#readansi">system.readansi</a> functions. It
1161 Backs up terminal/console flags only if a stream is a tty. 980will return the next byte from the input stream, or <code>nil</code> if no key was pressed.</p>
981
982<p>On Posix, <a href="https://www.lua.org/manual/5.4/manual.html#pdf-io.stdin">io.stdin</a> must be set to non-blocking mode using <a href="../modules/system.html#setnonblock">setnonblock</a>
983and canonical mode must be turned off using <a href="../modules/system.html#tcsetattr">tcsetattr</a>,
984before calling this function. Otherwise it will block. No conversions are
985done on Posix, so the byte read is returned as-is.</p>
986
987<p>On Windows this reads a wide character and converts it to UTF-8. Multi-byte
988sequences will be buffered internally and returned one byte at a time.
1162 989
1163 990
1164 991
1165 <h3>Returns:</h3> 992 <h3>Returns:</h3>
1166 <ol> 993 <ol>
1167 994
1168 table with backup of terminal settings 995 <span class="types"><span class="type">integer</span></span>
996 the byte read from the input stream
997 </ol>
998 <h3>Or</h3>
999 <ol>
1000
1001 <span class="types"><span class="type">nil</span></span>
1002 if no key was pressed
1003 </ol>
1004 <h3>Or</h3>
1005 <ol>
1006 <li>
1007 <span class="types"><span class="type">nil</span></span>
1008 on error</li>
1009 <li>
1010 <span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.4">string</a></span>
1011 error message</li>
1012 <li>
1013 <span class="types"><span class="type">int</span></span>
1014 errnum (on posix)</li>
1169 </ol> 1015 </ol>
1170 1016
1171 1017
@@ -1173,60 +1019,153 @@ flags for the <code>iflags</code>, <code>oflags</code>, and <code>lflags</code>
1173 1019
1174</dd> 1020</dd>
1175 <dt> 1021 <dt>
1176 <a name = "termrestore"></a> 1022 <a name = "readansi"></a>
1177 <strong>termrestore (backup)</strong> 1023 <strong>readansi (timeout[, fsleep=system.sleep])</strong>
1178 </dt> 1024 </dt>
1179 <dd> 1025 <dd>
1180 Restores terminal settings from a backup 1026 Reads a single key, if it is the start of ansi escape sequence then it reads
1027 the full sequence. The key can be a multi-byte string in case of multibyte UTF-8 character.
1028 This function uses <a href="../modules/system.html#readkey">system.readkey</a>, and hence <code>fsleep</code> to wait until either a key is
1029 available or the timeout is reached.
1030 It returns immediately if a key is available or if <code>timeout</code> is less than or equal to <code>0</code>.
1031 In case of an ANSI sequence, it will return the full sequence as a string.
1181 1032
1182 1033
1183 <h3>Parameters:</h3> 1034 <h3>Parameters:</h3>
1184 <ul> 1035 <ul>
1185 <li><span class="parameter">backup</span> 1036 <li><span class="parameter">timeout</span>
1186 <span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.6">table</a></span> 1037 <span class="types"><span class="type">number</span></span>
1187 the backup of terminal settings, see <a href="../modules/system.html#termbackup">termbackup</a>. 1038 the timeout in seconds.
1039 </li>
1040 <li><span class="parameter">fsleep</span>
1041 <span class="types"><span class="type">function</span></span>
1042 the function to call for sleeping.
1043 (<em>default</em> system.sleep)
1188 </li> 1044 </li>
1189 </ul> 1045 </ul>
1190 1046
1191 <h3>Returns:</h3> 1047 <h3>Returns:</h3>
1192 <ol> 1048 <ol>
1193 1049 <li>
1194 <span class="types"><span class="type">boolean</span></span> 1050 <span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.4">string</a></span>
1195 true 1051 the character that was received (can be multi-byte), or a complete ANSI sequence</li>
1052 <li>
1053 <span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.4">string</a></span>
1054 the type of input: <code>&quot;ctrl&quot;</code> for 0-31 and 127 bytes, <code>&quot;char&quot;</code> for other UTF-8 characters, <code>&quot;ansi&quot;</code> for an ANSI sequence</li>
1055 </ol>
1056 <h3>Or</h3>
1057 <ol>
1058 <li>
1059 <span class="types"><span class="type">nil</span></span>
1060 in case of an error</li>
1061 <li>
1062 <span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.4">string</a></span>
1063 error message; <code>&quot;timeout&quot;</code> if the timeout was reached.</li>
1064 <li>
1065 <span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.4">string</a></span>
1066 partial result in case of an error while reading a sequence, the sequence so far.
1067 The function retains its own internal buffer, so on the next call the incomplete buffer is used to
1068 complete the sequence.</li>
1196 </ol> 1069 </ol>
1197 1070
1198 1071
1199 1072
1073 <h3>Usage:</h3>
1074 <ul>
1075 <pre class="example"><span class="keyword">local</span> key, keytype = system.<span class="function-name">readansi</span>(<span class="number">5</span>)
1076<span class="keyword">if</span> keytype == <span class="string">"char"</span> <span class="keyword">then</span> ... <span class="keyword">end</span> <span class="comment">-- printable character
1077</span><span class="keyword">if</span> keytype ~= <span class="string">"char"</span> <span class="keyword">then</span> ... <span class="keyword">end</span> <span class="comment">-- non-printable character or sequence
1078</span><span class="keyword">if</span> keytype == <span class="string">"ansi"</span> <span class="keyword">then</span> ... <span class="keyword">end</span> <span class="comment">-- a multi-byte sequence, but not a UTF8 character
1079</span><span class="keyword">if</span> keytype ~= <span class="string">"ansi"</span> <span class="keyword">then</span> ... <span class="keyword">end</span> <span class="comment">-- a valid UTF8 character (which includes control characters)
1080</span><span class="keyword">if</span> keytype == <span class="string">"ctrl"</span> <span class="keyword">then</span> ... <span class="keyword">end</span> <span class="comment">-- a single-byte ctrl character (0-31, 127)</span></pre>
1081 </ul>
1200 1082
1201</dd> 1083</dd>
1202 <dt> 1084 <dt>
1203 <a name = "termsize"></a> 1085 <a name = "readkey"></a>
1204 <strong>termsize ()</strong> 1086 <strong>readkey (timeout[, fsleep=system.sleep])</strong>
1205 </dt> 1087 </dt>
1206 <dd> 1088 <dd>
1207 Get the size of the terminal in rows and columns. 1089 Reads a single byte from the console, with a timeout.
1090 This function uses <code>fsleep</code> to wait until either a byte is available or the timeout is reached.
1091 The sleep period is exponentially backing off, starting at 0.0125 seconds, with a maximum of 0.1 seconds.
1092 It returns immediately if a byte is available or if <code>timeout</code> is less than or equal to <code>0</code>.</p>
1208 1093
1094<p> Using <a href="../modules/system.html#readansi">system.readansi</a> is preferred over this function. Since this function can leave stray/invalid
1095 byte-sequences in the input buffer, while <a href="../modules/system.html#readansi">system.readansi</a> reads full ANSI and UTF8 sequences.
1209 1096
1210 1097
1098 <h3>Parameters:</h3>
1099 <ul>
1100 <li><span class="parameter">timeout</span>
1101 <span class="types"><span class="type">number</span></span>
1102 the timeout in seconds.
1103 </li>
1104 <li><span class="parameter">fsleep</span>
1105 <span class="types"><span class="type">function</span></span>
1106 the function to call for sleeping; <code>ok, err = fsleep(secs)</code>
1107 (<em>default</em> system.sleep)
1108 </li>
1109 </ul>
1110
1211 <h3>Returns:</h3> 1111 <h3>Returns:</h3>
1212 <ol> 1112 <ol>
1213 <li> 1113
1214 <span class="types"><span class="type">int</span></span> 1114 <span class="types"><span class="type">byte</span></span>
1215 the number of rows</li> 1115 the byte value that was read.
1216 <li>
1217 <span class="types"><span class="type">int</span></span>
1218 the number of columns</li>
1219 </ol> 1116 </ol>
1220 <h3>Or</h3> 1117 <h3>Or</h3>
1221 <ol> 1118 <ol>
1222 <li> 1119 <li>
1223 <span class="types"><span class="type">nil</span></span> 1120 <span class="types"><span class="type">nil</span></span>
1224 1121 if no key was read</li>
1225
1226</li>
1227 <li> 1122 <li>
1228 <span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.4">string</a></span> 1123 <span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.4">string</a></span>
1229 error message</li> 1124 error message when the timeout was reached (<code>&quot;timeout&quot;</code>), or if <a href="../modules/system.html#sleep">sleep</a> failed.</li>
1125 </ol>
1126
1127
1128
1129
1130</dd>
1131</dl>
1132 <h2 class="section-header "><a name="Terminal_UTF_8"></a>Terminal_UTF-8</h2>
1133
1134 <dl class="function">
1135 <dt>
1136 <a name = "CODEPAGE_UTF8"></a>
1137 <strong>CODEPAGE_UTF8</strong>
1138 </dt>
1139 <dd>
1140 UTF8 codepage.
1141 To be used with <a href="../modules/system.html#setconsoleoutputcp">system.setconsoleoutputcp</a> and <a href="../modules/system.html#setconsolecp">system.setconsolecp</a>.
1142
1143
1144 <ul>
1145 <li><span class="parameter">CODEPAGE_UTF8</span>
1146 The Windows CodePage for UTF8.
1147 </li>
1148 </ul>
1149
1150
1151
1152
1153
1154</dd>
1155 <dt>
1156 <a name = "getconsolecp"></a>
1157 <strong>getconsolecp ()</strong>
1158 </dt>
1159 <dd>
1160 Gets the current console code page (Windows).
1161
1162
1163
1164 <h3>Returns:</h3>
1165 <ol>
1166
1167 <span class="types"><span class="type">int</span></span>
1168 the current code page (always 65001 on Posix systems)
1230 </ol> 1169 </ol>
1231 1170
1232 1171
@@ -1234,27 +1173,73 @@ flags for the <code>iflags</code>, <code>oflags</code>, and <code>lflags</code>
1234 1173
1235</dd> 1174</dd>
1236 <dt> 1175 <dt>
1237 <a name = "termwrap"></a> 1176 <a name = "getconsoleoutputcp"></a>
1238 <strong>termwrap (f)</strong> 1177 <strong>getconsoleoutputcp ()</strong>
1239 </dt> 1178 </dt>
1240 <dd> 1179 <dd>
1241 Wraps a function to automatically restore terminal settings upon returning. 1180 Gets the current console output code page (Windows).
1242 Calls <a href="../modules/system.html#termbackup">termbackup</a> before calling the function and <a href="../modules/system.html#termrestore">termrestore</a> after. 1181
1182
1183
1184 <h3>Returns:</h3>
1185 <ol>
1186
1187 <span class="types"><span class="type">int</span></span>
1188 the current code page (always 65001 on Posix systems)
1189 </ol>
1190
1191
1192
1193
1194</dd>
1195 <dt>
1196 <a name = "setconsolecp"></a>
1197 <strong>setconsolecp (cp)</strong>
1198 </dt>
1199 <dd>
1200 Sets the current console code page (Windows).
1243 1201
1244 1202
1245 <h3>Parameters:</h3> 1203 <h3>Parameters:</h3>
1246 <ul> 1204 <ul>
1247 <li><span class="parameter">f</span> 1205 <li><span class="parameter">cp</span>
1248 <span class="types"><span class="type">function</span></span> 1206 <span class="types"><span class="type">int</span></span>
1249 function to wrap 1207 the code page to set, use <a href="../modules/system.html#CODEPAGE_UTF8">system.CODEPAGE_UTF8</a> (65001) for UTF-8
1250 </li> 1208 </li>
1251 </ul> 1209 </ul>
1252 1210
1253 <h3>Returns:</h3> 1211 <h3>Returns:</h3>
1254 <ol> 1212 <ol>
1255 1213
1256 <span class="types"><span class="type">function</span></span> 1214 <span class="types"><span class="type">bool</span></span>
1257 wrapped function 1215 <code>true</code> on success (always <code>true</code> on Posix systems)
1216 </ol>
1217
1218
1219
1220
1221</dd>
1222 <dt>
1223 <a name = "setconsoleoutputcp"></a>
1224 <strong>setconsoleoutputcp (cp)</strong>
1225 </dt>
1226 <dd>
1227 Sets the current console output code page (Windows).
1228
1229
1230 <h3>Parameters:</h3>
1231 <ul>
1232 <li><span class="parameter">cp</span>
1233 <span class="types"><span class="type">int</span></span>
1234 the code page to set, use <a href="../modules/system.html#CODEPAGE_UTF8">system.CODEPAGE_UTF8</a> (65001) for UTF-8
1235 </li>
1236 </ul>
1237
1238 <h3>Returns:</h3>
1239 <ol>
1240
1241 <span class="types"><span class="type">bool</span></span>
1242 <code>true</code> on success (always <code>true</code> on Posix systems)
1258 </ol> 1243 </ol>
1259 1244
1260 1245
@@ -1416,13 +1401,124 @@ This function will sleep, without doing a busy-loop and wasting CPU cycles.
1416 1401
1417</dd> 1402</dd>
1418</dl> 1403</dl>
1404 <h2 class="section-header "><a name="Terminal_Backup"></a>Terminal_Backup</h2>
1405
1406 <dl class="function">
1407 <dt>
1408 <a name = "autotermrestore"></a>
1409 <strong>autotermrestore ()</strong>
1410 </dt>
1411 <dd>
1412 Backs up terminal settings and restores them on application exit.
1413 Calls <a href="../modules/system.html#termbackup">termbackup</a> to back up terminal settings and sets up a GC method to
1414 automatically restore them on application exit (also works on Lua 5.1).
1415
1416
1417
1418 <h3>Returns:</h3>
1419 <ol>
1420
1421 <span class="types"><span class="type">boolean</span></span>
1422 true
1423 </ol>
1424 <h3>Or</h3>
1425 <ol>
1426 <li>
1427 <span class="types"><span class="type">nil</span></span>
1428 if the backup was already created</li>
1429 <li>
1430 <span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.4">string</a></span>
1431 error message</li>
1432 </ol>
1433
1434
1435
1436
1437</dd>
1438 <dt>
1439 <a name = "termbackup"></a>
1440 <strong>termbackup ()</strong>
1441 </dt>
1442 <dd>
1443 Returns a backup of terminal settings for stdin/out/err.
1444 Handles terminal/console flags, Windows codepage, and non-block flags on the streams.
1445 Backs up terminal/console flags only if a stream is a tty.
1446
1447
1448
1449 <h3>Returns:</h3>
1450 <ol>
1451
1452 table with backup of terminal settings
1453 </ol>
1454
1455
1456
1457
1458</dd>
1459 <dt>
1460 <a name = "termrestore"></a>
1461 <strong>termrestore (backup)</strong>
1462 </dt>
1463 <dd>
1464 Restores terminal settings from a backup
1465
1466
1467 <h3>Parameters:</h3>
1468 <ul>
1469 <li><span class="parameter">backup</span>
1470 <span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.6">table</a></span>
1471 the backup of terminal settings, see <a href="../modules/system.html#termbackup">termbackup</a>.
1472 </li>
1473 </ul>
1474
1475 <h3>Returns:</h3>
1476 <ol>
1477
1478 <span class="types"><span class="type">boolean</span></span>
1479 true
1480 </ol>
1481
1482
1483
1484
1485</dd>
1486 <dt>
1487 <a name = "termwrap"></a>
1488 <strong>termwrap (f)</strong>
1489 </dt>
1490 <dd>
1491 Wraps a function to automatically restore terminal settings upon returning.
1492 Calls <a href="../modules/system.html#termbackup">termbackup</a> before calling the function and <a href="../modules/system.html#termrestore">termrestore</a> after.
1493
1494
1495 <h3>Parameters:</h3>
1496 <ul>
1497 <li><span class="parameter">f</span>
1498 <span class="types"><span class="type">function</span></span>
1499 function to wrap
1500 </li>
1501 </ul>
1502
1503 <h3>Returns:</h3>
1504 <ol>
1505
1506 <span class="types"><span class="type">function</span></span>
1507 wrapped function
1508 </ol>
1509
1510
1511
1512
1513</dd>
1514</dl>
1419 1515
1420 1516
1421</div> <!-- id="content" --> 1517</div> <!-- id="content" -->
1422</div> <!-- id="main" --> 1518</div> <!-- id="main" -->
1423<div id="about"> 1519<div id="about">
1424<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i> 1520<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i>
1425<i style="float:right;">Last updated 2025-03-12 15:17:50 </i> 1521<i style="float:right;">Last updated 2025-07-11 22:55:05 </i>
1426</div> <!-- id="about" --> 1522</div> <!-- id="about" -->
1427</div> <!-- id="container" --> 1523</div> <!-- id="container" -->
1428</body> 1524</body>