aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/readline.lua6
1 files changed, 5 insertions, 1 deletions
diff --git a/examples/readline.lua b/examples/readline.lua
index ff215dd..98da267 100644
--- a/examples/readline.lua
+++ b/examples/readline.lua
@@ -144,10 +144,12 @@ readline.__index = readline
144-- @tparam[opt=""] string opts.value the default value 144-- @tparam[opt=""] string opts.value the default value
145-- @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
146-- @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
147-- @tparam[opt=system.sleep] function opts.fsleep the sleep function to use (see `system.readansi`)
147-- @treturn readline the new readline object 148-- @treturn readline the new readline object
148function readline.new(opts) 149function readline.new(opts)
149 local value = utf8parse(opts.value or "") 150 local value = utf8parse(opts.value or "")
150 local prompt = utf8parse(opts.prompt or "") 151 local prompt = utf8parse(opts.prompt or "")
152 local fsleep = opts.fsleep or sys.sleep
151 local pos = math.floor(opts.position or (#value + 1)) 153 local pos = math.floor(opts.position or (#value + 1))
152 pos = math.max(math.min(pos, (#value + 1)), 1) 154 pos = math.max(math.min(pos, (#value + 1)), 1)
153 local len = math.floor(opts.max_length or 80) 155 local len = math.floor(opts.max_length or 80)
@@ -175,6 +177,7 @@ function readline.new(opts)
175 position = pos, -- the current position in the input 177 position = pos, -- the current position in the input
176 drawn_before = false, -- if the prompt has been drawn 178 drawn_before = false, -- if the prompt has been drawn
177 exit_keys = exit_keys, -- the keys that will cause the readline to exit 179 exit_keys = exit_keys, -- the keys that will cause the readline to exit
180 fsleep = fsleep, -- the sleep function to use
178 } 181 }
179 182
180 setmetatable(self, readline) 183 setmetatable(self, readline)
@@ -413,7 +416,7 @@ function readline:__call(timeout, redraw)
413 local timeout_end = sys.gettime() + timeout 416 local timeout_end = sys.gettime() + timeout
414 417
415 while true do 418 while true do
416 local key, keytype = sys.readansi(timeout_end - sys.gettime()) 419 local key, keytype = sys.readansi(timeout_end - sys.gettime(), self.fsleep)
417 if not key then 420 if not key then
418 -- error or timeout 421 -- error or timeout
419 return nil, keytype 422 return nil, keytype
@@ -458,6 +461,7 @@ local rl = readline.new{
458 value = "Hello, 你-好 World 🚀!", 461 value = "Hello, 你-好 World 🚀!",
459 -- position = 2, 462 -- position = 2,
460 exit_keys = {key_sequences.enter, "\27", "\t", "\27[Z"}, -- enter, escape, tab, shift-tab 463 exit_keys = {key_sequences.enter, "\27", "\t", "\27[Z"}, -- enter, escape, tab, shift-tab
464 fsleep = sys.sleep,
461} 465}
462 466
463 467