diff options
author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2024-05-23 20:46:18 +0200 |
---|---|---|
committer | Thijs Schreijer <thijs@thijsschreijer.nl> | 2024-05-23 20:57:20 +0200 |
commit | 56db1511baeb0376a12915c69c1552b04010c26f (patch) | |
tree | d03aa6b4c33a6de39371e9be336c471bfd2cafc5 /examples | |
parent | 8f8d34f03428dbaa6cac229bbe36efc6d80d186d (diff) | |
download | luasystem-56db1511baeb0376a12915c69c1552b04010c26f.tar.gz luasystem-56db1511baeb0376a12915c69c1552b04010c26f.tar.bz2 luasystem-56db1511baeb0376a12915c69c1552b04010c26f.zip |
cleanup and documentation
Diffstat (limited to 'examples')
-rw-r--r-- | examples/readline.lua | 38 | ||||
-rw-r--r-- | examples/terminalsize.lua | 3 |
2 files changed, 19 insertions, 22 deletions
diff --git a/examples/readline.lua b/examples/readline.lua index f1e6258..286522c 100644 --- a/examples/readline.lua +++ b/examples/readline.lua | |||
@@ -1,3 +1,9 @@ | |||
1 | --- An example class for reading a line of input from the user in a non-blocking way. | ||
2 | -- It uses ANSI escape sequences to move the cursor and handle input. | ||
3 | -- It can be used to read a line of input from the user, with a prompt. | ||
4 | -- It can handle double-width UTF-8 characters. | ||
5 | -- It can be used asynchroneously if `system.sleep` is patched to yield to a coroutine scheduler. | ||
6 | |||
1 | local sys = require("system") | 7 | local sys = require("system") |
2 | 8 | ||
3 | 9 | ||
@@ -134,7 +140,7 @@ readline.__index = readline | |||
134 | --- Create a new readline object. | 140 | --- Create a new readline object. |
135 | -- @tparam table opts the options for the readline object | 141 | -- @tparam table opts the options for the readline object |
136 | -- @tparam[opt=""] string opts.prompt the prompt to display | 142 | -- @tparam[opt=""] string opts.prompt the prompt to display |
137 | -- @tparam[opt=80] number opts.max_length the maximum length of the input | 143 | -- @tparam[opt=80] number opts.max_length the maximum length of the input (in characters, not bytes) |
138 | -- @tparam[opt=""] string opts.value the default value | 144 | -- @tparam[opt=""] string opts.value the default value |
139 | -- @tparam[opt=`#value`] number opts.position of the cursor in the input | 145 | -- @tparam[opt=`#value`] number opts.position of the cursor in the input |
140 | -- @tparam[opt={"\10"/"\13"}] table opts.exit_keys an array of keys that will cause the readline to exit | 146 | -- @tparam[opt={"\10"/"\13"}] table opts.exit_keys an array of keys that will cause the readline to exit |
@@ -425,29 +431,25 @@ end | |||
425 | 431 | ||
426 | 432 | ||
427 | 433 | ||
428 | -- return readline | 434 | -- return readline -- normally we'd return here, but for the example we continue |
435 | |||
429 | 436 | ||
430 | 437 | ||
431 | 438 | ||
439 | local backup = sys.termbackup() | ||
432 | 440 | ||
433 | -- setup Windows console to handle ANSI processing | 441 | -- setup Windows console to handle ANSI processing |
434 | local of_in = sys.getconsoleflags(io.stdin) | ||
435 | local cp_in = sys.getconsolecp() | ||
436 | -- sys.setconsolecp(65001) | ||
437 | sys.setconsolecp(850) | ||
438 | local of_out = sys.getconsoleflags(io.stdout) | ||
439 | local cp_out = sys.getconsoleoutputcp() | ||
440 | sys.setconsoleoutputcp(65001) | ||
441 | sys.setconsoleflags(io.stdout, sys.getconsoleflags(io.stdout) + sys.COF_VIRTUAL_TERMINAL_PROCESSING) | 442 | sys.setconsoleflags(io.stdout, sys.getconsoleflags(io.stdout) + sys.COF_VIRTUAL_TERMINAL_PROCESSING) |
442 | sys.setconsoleflags(io.stdin, sys.getconsoleflags(io.stdin) + sys.CIF_VIRTUAL_TERMINAL_INPUT) | 443 | sys.setconsoleflags(io.stdin, sys.getconsoleflags(io.stdin) + sys.CIF_VIRTUAL_TERMINAL_INPUT) |
444 | -- set output to UTF-8 | ||
445 | sys.setconsoleoutputcp(65001) | ||
443 | 446 | ||
444 | -- setup Posix terminal to use non-blocking mode, and disable line-mode | 447 | -- setup Posix terminal to disable canonical mode and echo |
445 | local of_attr = sys.tcgetattr(io.stdin) | ||
446 | local of_block = sys.getnonblock(io.stdin) | ||
447 | sys.setnonblock(io.stdin, true) | ||
448 | sys.tcsetattr(io.stdin, sys.TCSANOW, { | 448 | sys.tcsetattr(io.stdin, sys.TCSANOW, { |
449 | lflag = of_attr.lflag - sys.L_ICANON - sys.L_ECHO, -- disable canonical mode and echo | 449 | lflag = sys.tcgetattr(io.stdin).lflag - sys.L_ICANON - sys.L_ECHO, |
450 | }) | 450 | }) |
451 | -- setup stdin to non-blocking mode | ||
452 | sys.setnonblock(io.stdin, true) | ||
451 | 453 | ||
452 | 454 | ||
453 | local rl = readline.new{ | 455 | local rl = readline.new{ |
@@ -467,10 +469,4 @@ print("Exit-Key (bytes):", key:byte(1,-1)) | |||
467 | 469 | ||
468 | 470 | ||
469 | -- Clean up afterwards | 471 | -- Clean up afterwards |
470 | sys.setnonblock(io.stdin, false) | 472 | sys.termrestore(backup) |
471 | sys.setconsoleflags(io.stdout, of_out) | ||
472 | sys.setconsoleflags(io.stdin, of_in) | ||
473 | sys.tcsetattr(io.stdin, sys.TCSANOW, of_attr) | ||
474 | sys.setnonblock(io.stdin, of_block) | ||
475 | sys.setconsolecp(cp_in) | ||
476 | sys.setconsoleoutputcp(cp_out) | ||
diff --git a/examples/terminalsize.lua b/examples/terminalsize.lua index 78d1910..ed66792 100644 --- a/examples/terminalsize.lua +++ b/examples/terminalsize.lua | |||
@@ -26,11 +26,12 @@ end | |||
26 | 26 | ||
27 | local w, h | 27 | local w, h |
28 | print("Change the terminal window size, press any key to exit") | 28 | print("Change the terminal window size, press any key to exit") |
29 | while not sys.readkey(0.2) do | 29 | while not sys.readansi(0.2) do -- use readansi to not leave stray bytes in the input buffer |
30 | local nw, nh = sys.termsize() | 30 | local nw, nh = sys.termsize() |
31 | if w ~= nw or h ~= nh then | 31 | if w ~= nw or h ~= nh then |
32 | w, h = nw, nh | 32 | w, h = nw, nh |
33 | local text = "Terminal size: " .. w .. "x" .. h .. " " | 33 | local text = "Terminal size: " .. w .. "x" .. h .. " " |
34 | io.write(text .. cursor_move_horiz(-#text)) | 34 | io.write(text .. cursor_move_horiz(-#text)) |
35 | io.flush() | ||
35 | end | 36 | end |
36 | end | 37 | end |