diff options
Diffstat (limited to 'system')
| -rw-r--r-- | system/init.lua | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/system/init.lua b/system/init.lua index e0e5f21..a9f57e9 100644 --- a/system/init.lua +++ b/system/init.lua | |||
| @@ -13,14 +13,16 @@ local system = require 'system.core' | |||
| 13 | system.CODEPAGE_UTF8 = 65001 | 13 | system.CODEPAGE_UTF8 = 65001 |
| 14 | 14 | ||
| 15 | do | 15 | do |
| 16 | local backup_mt = {} | 16 | local backup_indicator = {} |
| 17 | 17 | ||
| 18 | --- Returns a backup of terminal settings for stdin/out/err. | 18 | --- Returns a backup of terminal settings for stdin/out/err. |
| 19 | -- Handles terminal/console flags, Windows codepage, and non-block flags on the streams. | 19 | -- Handles terminal/console flags, Windows codepage, and non-block flags on the streams. |
| 20 | -- Backs up terminal/console flags only if a stream is a tty. | 20 | -- Backs up terminal/console flags only if a stream is a tty. |
| 21 | -- @return table with backup of terminal settings | 21 | -- @return table with backup of terminal settings |
| 22 | function system.termbackup() | 22 | function system.termbackup() |
| 23 | local backup = setmetatable({}, backup_mt) | 23 | local backup = { |
| 24 | __type = backup_indicator, -- cannot set a metatable, since autotermrestore uses it for GC | ||
| 25 | } | ||
| 24 | 26 | ||
| 25 | if system.isatty(io.stdin) then | 27 | if system.isatty(io.stdin) then |
| 26 | backup.console_in = system.getconsoleflags(io.stdin) | 28 | backup.console_in = system.getconsoleflags(io.stdin) |
| @@ -51,7 +53,7 @@ do | |||
| 51 | -- @tparam table backup the backup of terminal settings, see `termbackup`. | 53 | -- @tparam table backup the backup of terminal settings, see `termbackup`. |
| 52 | -- @treturn boolean true | 54 | -- @treturn boolean true |
| 53 | function system.termrestore(backup) | 55 | function system.termrestore(backup) |
| 54 | if getmetatable(backup) ~= backup_mt then | 56 | if type(backup) ~= "table" or backup.__type ~= backup_indicator then |
| 55 | error("arg #1 to termrestore, expected backup table, got " .. type(backup), 2) | 57 | error("arg #1 to termrestore, expected backup table, got " .. type(backup), 2) |
| 56 | end | 58 | end |
| 57 | 59 | ||
