1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
--- Lua System Library.
-- @module system
--- Terminal
-- @section terminal
local system = require 'system.core'
--- UTF8 codepage.
-- To be used with `system.setconsoleoutputcp` and `system.setconsolecp`.
-- @field CODEPAGE_UTF8 The Windows CodePage for UTF8.
system.CODEPAGE_UTF8 = 65001
do
local backup_mt = {}
--- Returns a backup of terminal settings for stdin/out/err.
-- Handles terminal/console flags, Windows codepage, and non-block flags on the streams.
-- Backs up terminal/console flags only if a stream is a tty.
-- @return table with backup of terminal settings
function system.termbackup()
local backup = setmetatable({}, backup_mt)
if system.isatty(io.stdin) then
backup.console_in = system.getconsoleflags(io.stdin)
backup.term_in = system.tcgetattr(io.stdin)
end
if system.isatty(io.stdout) then
backup.console_out = system.getconsoleflags(io.stdout)
backup.term_out = system.tcgetattr(io.stdout)
end
if system.isatty(io.stderr) then
backup.console_err = system.getconsoleflags(io.stderr)
backup.term_err = system.tcgetattr(io.stderr)
end
backup.block_in = system.getnonblock(io.stdin)
backup.block_out = system.getnonblock(io.stdout)
backup.block_err = system.getnonblock(io.stderr)
backup.consoleoutcodepage = system.getconsoleoutputcp()
backup.consolecp = system.getconsolecp()
return backup
end
--- Restores terminal settings from a backup
-- @tparam table backup the backup of terminal settings, see `termbackup`.
-- @treturn boolean true
function system.termrestore(backup)
if getmetatable(backup) ~= backup_mt then
error("arg #1 to termrestore, expected backup table, got " .. type(backup), 2)
end
if backup.console_in then system.setconsoleflags(io.stdin, backup.console_in) end
if backup.term_in then system.tcsetattr(io.stdin, system.TCSANOW, backup.term_in) end
if backup.console_out then system.setconsoleflags(io.stdout, backup.console_out) end
if backup.term_out then system.tcsetattr(io.stdout, system.TCSANOW, backup.term_out) end
if backup.console_err then system.setconsoleflags(io.stderr, backup.console_err) end
if backup.term_err then system.tcsetattr(io.stderr, system.TCSANOW, backup.term_err) end
if backup.block_in ~= nil then system.setnonblock(io.stdin, backup.block_in) end
if backup.block_out ~= nil then system.setnonblock(io.stdout, backup.block_out) end
if backup.block_err ~= nil then system.setnonblock(io.stderr, backup.block_err) end
if backup.consoleoutcodepage then system.setconsoleoutputcp(backup.consoleoutcodepage) end
if backup.consolecp then system.setconsolecp(backup.consolecp) end
return true
end
end
do -- autotermrestore
local global_backup -- global backup for terminal settings
local add_gc_method do
-- __gc meta-method is not available in all Lua versions
local has_gc = not newproxy or false -- `__gc` was added when `newproxy` was removed
if has_gc then
-- use default GC mechanism since it is available
function add_gc_method(t, f)
setmetatable(t, { __gc = f })
end
else
-- create workaround using a proxy userdata, typical for Lua 5.1
function add_gc_method(t, f)
local proxy = newproxy(true)
getmetatable(proxy).__gc = function()
t["__gc_proxy"] = nil
f(t)
end
t["__gc_proxy"] = proxy
end
end
end
--- Backs up terminal settings and restores them on application exit.
-- Calls `termbackup` to back up terminal settings and sets up a GC method to
-- automatically restore them on application exit (also works on Lua 5.1).
-- @treturn[1] boolean true
-- @treturn[2] nil if the backup was already created
-- @treturn[2] string error message
function system.autotermrestore()
if global_backup then
return nil, "global terminal backup was already set up"
end
global_backup = system.termbackup()
add_gc_method(global_backup, function(self) pcall(system.termrestore, self) end)
return true
end
-- export a reset function only upon testing
if _G._TEST then
function system._reset_global_backup()
global_backup = nil
end
end
end
do
local oldunpack = unpack or table.unpack
local pack = function(...) return { n = select("#", ...), ... } end
local unpack = function(t) return oldunpack(t, 1, t.n) end
--- Wraps a function to automatically restore terminal settings upon returning.
-- Calls `termbackup` before calling the function and `termrestore` after.
-- @tparam function f function to wrap
-- @treturn function wrapped function
function system.termwrap(f)
if type(f) ~= "function" then
error("arg #1 to wrap, expected function, got " .. type(f), 2)
end
return function(...)
local bu = system.termbackup()
local results = pack(f(...))
system.termrestore(bu)
return unpack(results)
end
end
end
--- Debug function for console flags (Windows).
-- Pretty prints the current flags set for the handle.
-- @param fh file handle (`io.stdin`, `io.stdout`, `io.stderr`)
-- @usage -- Print the flags for stdin/out/err
-- system.listconsoleflags(io.stdin)
-- system.listconsoleflags(io.stdout)
-- system.listconsoleflags(io.stderr)
function system.listconsoleflags(fh)
local flagtype
if fh == io.stdin then
print "------ STDIN FLAGS WINDOWS ------"
flagtype = "CIF_"
elseif fh == io.stdout then
print "------ STDOUT FLAGS WINDOWS ------"
flagtype = "COF_"
elseif fh == io.stderr then
print "------ STDERR FLAGS WINDOWS ------"
flagtype = "COF_"
end
local flags = assert(system.getconsoleflags(fh))
local out = {}
for k,v in pairs(system) do
if type(k) == "string" and k:sub(1,4) == flagtype then
if flags:has_all_of(v) then
out[#out+1] = string.format("%10d [x] %s",v:value(),k)
else
out[#out+1] = string.format("%10d [ ] %s",v:value(),k)
end
end
end
table.sort(out)
for k,v in pairs(out) do
print(v)
end
end
--- Debug function for terminal flags (Posix).
-- Pretty prints the current flags set for the handle.
-- @param fh file handle (`io.stdin`, `io.stdout`, `io.stderr`)
-- @usage -- Print the flags for stdin/out/err
-- system.listconsoleflags(io.stdin)
-- system.listconsoleflags(io.stdout)
-- system.listconsoleflags(io.stderr)
function system.listtermflags(fh)
if fh == io.stdin then
print "------ STDIN FLAGS POSIX ------"
elseif fh == io.stdout then
print "------ STDOUT FLAGS POSIX ------"
elseif fh == io.stderr then
print "------ STDERR FLAGS POSIX ------"
end
local flags = assert(system.tcgetattr(fh))
for _, flagtype in ipairs { "iflag", "oflag", "lflag" } do
local prefix = flagtype:sub(1,1):upper() .. "_" -- I_, O_, or L_, the constant prefixes
local out = {}
for k,v in pairs(system) do
if type(k) == "string" and k:sub(1,2) == prefix then
if flags[flagtype]:has_all_of(v) then
out[#out+1] = string.format("%10d [x] %s",v:value(),k)
else
out[#out+1] = string.format("%10d [ ] %s",v:value(),k)
end
end
end
table.sort(out)
for k,v in pairs(out) do
print(v)
end
end
end
do
--- Reads a single byte from the console, with a timeout.
-- This function uses `fsleep` to wait until either a byte is available or the timeout is reached.
-- The sleep period is exponentially backing off, starting at 0.0125 seconds, with a maximum of 0.2 seconds.
-- It returns immediately if a byte is available or if `timeout` is less than or equal to `0`.
--
-- Using `system.readansi` is preferred over this function. Since this function can leave stray/invalid
-- byte-sequences in the input buffer, while `system.readansi` reads full ANSI and UTF8 sequences.
-- @tparam number timeout the timeout in seconds.
-- @tparam[opt=system.sleep] function fsleep the function to call for sleeping.
-- @treturn[1] byte the byte value that was read.
-- @treturn[2] nil if no key was read
-- @treturn[2] string error message; `"timeout"` if the timeout was reached.
function system.readkey(timeout, fsleep)
if type(timeout) ~= "number" then
error("arg #1 to readkey, expected timeout in seconds, got " .. type(timeout), 2)
end
local interval = 0.0125
local key = system._readkey()
while key == nil and timeout > 0 do
(fsleep or system.sleep)(math.min(interval, timeout))
timeout = timeout - interval
interval = math.min(0.2, interval * 2)
key = system._readkey()
end
if key then
return key
end
return nil, "timeout"
end
end
do
local left_over_key
local sequence -- table to store the sequence in progress
local utf8_length -- length of utf8 sequence currently being processed
local unpack = unpack or table.unpack
--- Reads a single key, if it is the start of ansi escape sequence then it reads
-- the full sequence. The key can be a multi-byte string in case of multibyte UTF-8 character.
-- This function uses `system.readkey`, and hence `fsleep` to wait until either a key is
-- available or the timeout is reached.
-- It returns immediately if a key is available or if `timeout` is less than or equal to `0`.
-- In case of an ANSI sequence, it will return the full sequence as a string.
-- @tparam number timeout the timeout in seconds.
-- @tparam[opt=system.sleep] function fsleep the function to call for sleeping.
-- @treturn[1] string the character that was received (can be multi-byte), or a complete ANSI sequence
-- @treturn[1] string the type of input: `"char"` for a single key, `"ansi"` for an ANSI sequence
-- @treturn[2] nil in case of an error
-- @treturn[2] string error message; `"timeout"` if the timeout was reached.
-- @treturn[2] string partial result in case of an error while reading a sequence, the sequence so far.
function system.readansi(timeout, fsleep)
if type(timeout) ~= "number" then
error("arg #1 to readansi, expected timeout in seconds, got " .. type(timeout), 2)
end
fsleep = fsleep or system.sleep
local key
if not sequence then
-- no sequence in progress, read a key
if left_over_key then
-- we still have a cached key from the last call
key = left_over_key
left_over_key = nil
else
-- read a new key
local err
key, err = system.readkey(timeout, fsleep)
if key == nil then -- timeout or error
return nil, err
end
end
if key == 27 then
-- looks like an ansi escape sequence, immediately read next char
-- as an heuristic against manually typing escape sequences
local key2 = system.readkey(0, fsleep)
if key2 ~= 91 and key2 ~= 79 then -- we expect either "[" or "O" for an ANSI sequence
-- not the expected [ or O character, so we return the key as is
-- and store the extra key read for the next call
left_over_key = key2
return string.char(key), "char"
end
-- escape sequence detected
sequence = { key, key2 }
else
-- check UTF8 length
utf8_length = key < 128 and 1 or key < 224 and 2 or key < 240 and 3 or key < 248 and 4
if utf8_length == 1 then
-- single byte character
utf8_length = nil
return string.char(key), "char"
else
-- UTF8 sequence detected
sequence = { key }
end
end
end
local err
if utf8_length then
-- read remainder of UTF8 sequence
local timeout_end = system.gettime() + timeout
while true do
key, err = system.readkey(timeout_end - system.gettime(), fsleep)
if err then
break
end
table.insert(sequence, key)
if #sequence == utf8_length then
-- end of sequence, return the full sequence
local result = string.char(unpack(sequence))
sequence = nil
utf8_length = nil
return result, "char"
end
end
else
-- read remainder of ANSI sequence
local timeout_end = system.gettime() + timeout
while true do
key, err = system.readkey(timeout_end - system.gettime(), fsleep)
if err then
break
end
table.insert(sequence, key)
if (key >= 65 and key <= 90) or (key >= 97 and key <= 126) then
-- end of sequence, return the full sequence
local result = string.char(unpack(sequence))
sequence = nil
return result, "ansi"
end
end
end
-- error, or timeout reached, return the sequence so far
local partial = string.char(unpack(sequence))
return nil, err, partial
end
end
return system
|