aboutsummaryrefslogtreecommitdiff
path: root/examples/readline.lua
diff options
context:
space:
mode:
Diffstat (limited to 'examples/readline.lua')
-rw-r--r--examples/readline.lua38
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
1local sys = require("system") 7local 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
439local backup = sys.termbackup()
432 440
433-- setup Windows console to handle ANSI processing 441-- setup Windows console to handle ANSI processing
434local of_in = sys.getconsoleflags(io.stdin)
435local cp_in = sys.getconsolecp()
436-- sys.setconsolecp(65001)
437sys.setconsolecp(850)
438local of_out = sys.getconsoleflags(io.stdout)
439local cp_out = sys.getconsoleoutputcp()
440sys.setconsoleoutputcp(65001)
441sys.setconsoleflags(io.stdout, sys.getconsoleflags(io.stdout) + sys.COF_VIRTUAL_TERMINAL_PROCESSING) 442sys.setconsoleflags(io.stdout, sys.getconsoleflags(io.stdout) + sys.COF_VIRTUAL_TERMINAL_PROCESSING)
442sys.setconsoleflags(io.stdin, sys.getconsoleflags(io.stdin) + sys.CIF_VIRTUAL_TERMINAL_INPUT) 443sys.setconsoleflags(io.stdin, sys.getconsoleflags(io.stdin) + sys.CIF_VIRTUAL_TERMINAL_INPUT)
444-- set output to UTF-8
445sys.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
445local of_attr = sys.tcgetattr(io.stdin)
446local of_block = sys.getnonblock(io.stdin)
447sys.setnonblock(io.stdin, true)
448sys.tcsetattr(io.stdin, sys.TCSANOW, { 448sys.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
452sys.setnonblock(io.stdin, true)
451 453
452 454
453local rl = readline.new{ 455local 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
470sys.setnonblock(io.stdin, false) 472sys.termrestore(backup)
471sys.setconsoleflags(io.stdout, of_out)
472sys.setconsoleflags(io.stdin, of_in)
473sys.tcsetattr(io.stdin, sys.TCSANOW, of_attr)
474sys.setnonblock(io.stdin, of_block)
475sys.setconsolecp(cp_in)
476sys.setconsoleoutputcp(cp_out)