diff options
Diffstat (limited to 'examples/readline.lua')
-rw-r--r-- | examples/readline.lua | 38 |
1 files changed, 17 insertions, 21 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) | ||