-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move backup/restore of term settings to example
- Loading branch information
Showing
3 changed files
with
148 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
-- This example shows how to backup and restore terminal settings | ||
|
||
local sys = require "system" | ||
local global_backup | ||
|
||
|
||
|
||
local add_gc_method do | ||
-- feature detection; __GC meta-method, not available in all Lua versions | ||
local has_gc = false | ||
local tt = setmetatable({}, { -- luacheck: ignore | ||
__gc = function() has_gc = true end | ||
}) | ||
|
||
-- clear table and run GC to trigger | ||
tt = nil | ||
collectgarbage() | ||
collectgarbage() | ||
|
||
|
||
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 | ||
|
||
|
||
|
||
--- Returns a backup of terminal setting for stdin/out/err. | ||
-- Handles terminal/console flags 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 | ||
local function termbackup() | ||
local backup = {} | ||
|
||
if sys.isatty(io.stdin) then | ||
backup.console_in = sys.getconsoleflags(io.stdin) | ||
backup.term_in = sys.tcgetattr(io.stdin) | ||
end | ||
if sys.isatty(io.stdout) then | ||
backup.console_out = sys.getconsoleflags(io.stdout) | ||
backup.term_out = sys.tcgetattr(io.stdout) | ||
end | ||
if sys.isatty(io.stderr) then | ||
backup.console_err = sys.getconsoleflags(io.stderr) | ||
backup.term_err = sys.tcgetattr(io.stderr) | ||
end | ||
|
||
backup.block_in = sys.getnonblock(io.stdin) | ||
backup.block_out = sys.getnonblock(io.stdout) | ||
backup.block_err = sys.getnonblock(io.stderr) | ||
|
||
return backup | ||
end | ||
|
||
|
||
|
||
--- Restores terminal settings from a backup | ||
-- @param backup table with backup of terminal settings, see `termbackup`. | ||
-- @return true | ||
local function termrestore(backup) | ||
if backup.console_in then sys.setconsoleflags(io.stdin, backup.console_in) end | ||
if backup.term_in then sys.tcsetattr(io.stdin, sys.TCSANOW, backup.term_in) end | ||
if backup.console_out then sys.setconsoleflags(io.stdout, backup.console_out) end | ||
if backup.term_out then sys.tcsetattr(io.stdout, sys.TCSANOW, backup.term_out) end | ||
if backup.console_err then sys.setconsoleflags(io.stderr, backup.console_err) end | ||
if backup.term_err then sys.tcsetattr(io.stderr, sys.TCSANOW, backup.term_err) end | ||
|
||
sys.setnonblock(io.stdin, backup.block_in) | ||
sys.setnonblock(io.stdout, backup.block_out) | ||
sys.setnonblock(io.stderr, backup.block_err) | ||
return true | ||
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). | ||
-- @return[1] true | ||
-- @return[2] nil, if the backup was already created | ||
-- @return[2] string error message | ||
local function autotermrestore() | ||
if global_backup then | ||
return nil, "global terminal backup was already set up" | ||
end | ||
global_backup = termbackup() | ||
add_gc_method(global_backup, function(self) | ||
termrestore(self) end) | ||
return true | ||
end | ||
|
||
|
||
-- Export module table | ||
return { | ||
termbackup = termbackup, | ||
termrestore = termrestore, | ||
autotermrestore = autotermrestore, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
-- This example shows how to remove platform differences to create a | ||
-- cross-platform level playing field. | ||
|
||
local sys = require "system" | ||
|
||
|
||
|
||
if sys.is_windows then | ||
-- Windows holds multiple copies of environment variables, to ensure `getenv` | ||
-- returns what `setenv` sets we need to use the `system.getenv` instead of | ||
-- `os.getenv`. | ||
os.getenv = sys.getenv -- luacheck: ignore | ||
|
||
-- Set up the terminal to handle ANSI escape sequences on Windows. | ||
if sys.isatty(io.stdout) then | ||
sys.setconsoleflags(io.stdout, sys.getconsoleflags(io.stdout) + sys.COF_VIRTUAL_TERMINAL_PROCESSING) | ||
end | ||
if sys.isatty(io.stderr) then | ||
sys.setconsoleflags(io.stderr, sys.getconsoleflags(io.stderr) + sys.COF_VIRTUAL_TERMINAL_PROCESSING) | ||
end | ||
if sys.isatty(io.stdin) then | ||
sys.setconsoleflags(io.stdin, sys.getconsoleflags(io.stdout) + sys.ENABLE_VIRTUAL_TERMINAL_INPUT) | ||
end | ||
|
||
|
||
else | ||
-- On Posix, one can set a variable to an empty string, but on Windows, this | ||
-- will remove the variable from the environment. To make this consistent | ||
-- across platforms, we will remove the variable from the environment if the | ||
-- value is an empty string. | ||
local old_setenv = sys.setenv | ||
function sys.setenv(name, value) | ||
if value == "" then value = nil end | ||
return old_setenv(name, value) | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters