diff options
author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2024-05-22 21:37:27 +0200 |
---|---|---|
committer | Thijs Schreijer <thijs@thijsschreijer.nl> | 2024-05-22 21:37:27 +0200 |
commit | 2746189f0a504d5929f8aa69edcc116dbbeff105 (patch) | |
tree | 2176dde5a3b2a439eef175921c61fc1ec4b1be22 /system/init.lua | |
parent | b1e250a00406f5268a773f7923bb14708567ce9f (diff) | |
download | luasystem-2746189f0a504d5929f8aa69edcc116dbbeff105.tar.gz luasystem-2746189f0a504d5929f8aa69edcc116dbbeff105.tar.bz2 luasystem-2746189f0a504d5929f8aa69edcc116dbbeff105.zip |
add tests for termbackup & termrestore
Diffstat (limited to '')
-rw-r--r-- | system/init.lua | 92 |
1 files changed, 49 insertions, 43 deletions
diff --git a/system/init.lua b/system/init.lua index c232cd2..b9a4f6f 100644 --- a/system/init.lua +++ b/system/init.lua | |||
@@ -4,61 +4,67 @@ | |||
4 | local sys = require 'system.core' | 4 | local sys = require 'system.core' |
5 | 5 | ||
6 | 6 | ||
7 | do | ||
8 | local backup_mt = {} | ||
9 | |||
10 | --- Returns a backup of terminal setting for stdin/out/err. | ||
11 | -- Handles terminal/console flags, Windows codepage, and non-block flags on the streams. | ||
12 | -- Backs up terminal/console flags only if a stream is a tty. | ||
13 | -- @return table with backup of terminal settings | ||
14 | function sys.termbackup() | ||
15 | local backup = setmetatable({}, backup_mt) | ||
16 | |||
17 | if sys.isatty(io.stdin) then | ||
18 | backup.console_in = sys.getconsoleflags(io.stdin) | ||
19 | backup.term_in = sys.tcgetattr(io.stdin) | ||
20 | end | ||
21 | if sys.isatty(io.stdout) then | ||
22 | backup.console_out = sys.getconsoleflags(io.stdout) | ||
23 | backup.term_out = sys.tcgetattr(io.stdout) | ||
24 | end | ||
25 | if sys.isatty(io.stderr) then | ||
26 | backup.console_err = sys.getconsoleflags(io.stderr) | ||
27 | backup.term_err = sys.tcgetattr(io.stderr) | ||
28 | end | ||
7 | 29 | ||
8 | --- Returns a backup of terminal setting for stdin/out/err. | 30 | backup.block_in = sys.getnonblock(io.stdin) |
9 | -- Handles terminal/console flags, Windows codepage, and non-block flags on the streams. | 31 | backup.block_out = sys.getnonblock(io.stdout) |
10 | -- Backs up terminal/console flags only if a stream is a tty. | 32 | backup.block_err = sys.getnonblock(io.stderr) |
11 | -- @return table with backup of terminal settings | ||
12 | function sys.termbackup() | ||
13 | local backup = {} | ||
14 | |||
15 | if sys.isatty(io.stdin) then | ||
16 | backup.console_in = sys.getconsoleflags(io.stdin) | ||
17 | backup.term_in = sys.tcgetattr(io.stdin) | ||
18 | end | ||
19 | if sys.isatty(io.stdout) then | ||
20 | backup.console_out = sys.getconsoleflags(io.stdout) | ||
21 | backup.term_out = sys.tcgetattr(io.stdout) | ||
22 | end | ||
23 | if sys.isatty(io.stderr) then | ||
24 | backup.console_err = sys.getconsoleflags(io.stderr) | ||
25 | backup.term_err = sys.tcgetattr(io.stderr) | ||
26 | end | ||
27 | 33 | ||
28 | backup.block_in = sys.getnonblock(io.stdin) | 34 | backup.consoleoutcodepage = sys.getconsoleoutputcp() |
29 | backup.block_out = sys.getnonblock(io.stdout) | 35 | backup.consolecp = sys.getconsolecp() |
30 | backup.block_err = sys.getnonblock(io.stderr) | ||
31 | 36 | ||
32 | backup.consoleoutcodepage = sys.getconsoleoutputcp() | 37 | return backup |
33 | backup.consolecp = sys.getconsolecp() | 38 | end |
34 | 39 | ||
35 | return backup | ||
36 | end | ||
37 | 40 | ||
38 | 41 | ||
42 | --- Restores terminal settings from a backup | ||
43 | -- @tparam table backup the backup of terminal settings, see `termbackup`. | ||
44 | -- @treturn boolean true | ||
45 | function sys.termrestore(backup) | ||
46 | if getmetatable(backup) ~= backup_mt then | ||
47 | error("arg #1 to termrestore, expected backup table, got " .. type(backup), 2) | ||
48 | end | ||
39 | 49 | ||
40 | --- Restores terminal settings from a backup | 50 | if backup.console_in then sys.setconsoleflags(io.stdin, backup.console_in) end |
41 | -- @tparam table backup the backup of terminal settings, see `termbackup`. | 51 | if backup.term_in then sys.tcsetattr(io.stdin, sys.TCSANOW, backup.term_in) end |
42 | -- @treturn boolean true | 52 | if backup.console_out then sys.setconsoleflags(io.stdout, backup.console_out) end |
43 | function sys.termrestore(backup) | 53 | if backup.term_out then sys.tcsetattr(io.stdout, sys.TCSANOW, backup.term_out) end |
44 | if backup.console_in then sys.setconsoleflags(io.stdin, backup.console_in) end | 54 | if backup.console_err then sys.setconsoleflags(io.stderr, backup.console_err) end |
45 | if backup.term_in then sys.tcsetattr(io.stdin, sys.TCSANOW, backup.term_in) end | 55 | if backup.term_err then sys.tcsetattr(io.stderr, sys.TCSANOW, backup.term_err) end |
46 | if backup.console_out then sys.setconsoleflags(io.stdout, backup.console_out) end | ||
47 | if backup.term_out then sys.tcsetattr(io.stdout, sys.TCSANOW, backup.term_out) end | ||
48 | if backup.console_err then sys.setconsoleflags(io.stderr, backup.console_err) end | ||
49 | if backup.term_err then sys.tcsetattr(io.stderr, sys.TCSANOW, backup.term_err) end | ||
50 | 56 | ||
51 | if backup.block_in ~= nil then sys.setnonblock(io.stdin, backup.block_in) end | 57 | if backup.block_in ~= nil then sys.setnonblock(io.stdin, backup.block_in) end |
52 | if backup.block_out ~= nil then sys.setnonblock(io.stdout, backup.block_out) end | 58 | if backup.block_out ~= nil then sys.setnonblock(io.stdout, backup.block_out) end |
53 | if backup.block_err ~= nil then sys.setnonblock(io.stderr, backup.block_err) end | 59 | if backup.block_err ~= nil then sys.setnonblock(io.stderr, backup.block_err) end |
54 | 60 | ||
55 | if backup.consoleoutcodepage then sys.setconsoleoutputcp(backup.consoleoutcodepage) end | 61 | if backup.consoleoutcodepage then sys.setconsoleoutputcp(backup.consoleoutcodepage) end |
56 | if backup.consolecp then sys.setconsolecp(backup.consolecp) end | 62 | if backup.consolecp then sys.setconsolecp(backup.consolecp) end |
57 | return true | 63 | return true |
64 | end | ||
58 | end | 65 | end |
59 | 66 | ||
60 | 67 | ||
61 | |||
62 | do -- autotermrestore | 68 | do -- autotermrestore |
63 | local global_backup -- global backup for terminal settings | 69 | local global_backup -- global backup for terminal settings |
64 | 70 | ||