diff options
| author | Thijs <thijs@thijsschreijer.nl> | 2023-11-16 09:09:54 +0100 |
|---|---|---|
| committer | Thijs Schreijer <thijs@thijsschreijer.nl> | 2024-04-30 09:28:01 +0200 |
| commit | bd994461ef7c2553da9a6945c685152bad50eb8f (patch) | |
| tree | 28adc32712f00a200a34357e731a570bf1a359dc /examples/compat.lua | |
| parent | 47c24eed0191f8f72646be63dee94ac2b35eb062 (diff) | |
| download | luasystem-bd994461ef7c2553da9a6945c685152bad50eb8f.tar.gz luasystem-bd994461ef7c2553da9a6945c685152bad50eb8f.tar.bz2 luasystem-bd994461ef7c2553da9a6945c685152bad50eb8f.zip | |
feat(term): getting/setting terminal config flags
Diffstat (limited to 'examples/compat.lua')
| -rw-r--r-- | examples/compat.lua | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/examples/compat.lua b/examples/compat.lua new file mode 100644 index 0000000..c00d44a --- /dev/null +++ b/examples/compat.lua | |||
| @@ -0,0 +1,37 @@ | |||
| 1 | -- This example shows how to remove platform differences to create a | ||
| 2 | -- cross-platform level playing field. | ||
| 3 | |||
| 4 | local sys = require "system" | ||
| 5 | |||
| 6 | |||
| 7 | |||
| 8 | if sys.is_windows then | ||
| 9 | -- Windows holds multiple copies of environment variables, to ensure `getenv` | ||
| 10 | -- returns what `setenv` sets we need to use the `system.getenv` instead of | ||
| 11 | -- `os.getenv`. | ||
| 12 | os.getenv = sys.getenv -- luacheck: ignore | ||
| 13 | |||
| 14 | -- Set up the terminal to handle ANSI escape sequences on Windows. | ||
| 15 | if sys.isatty(io.stdout) then | ||
| 16 | sys.setconsoleflags(io.stdout, sys.getconsoleflags(io.stdout) + sys.COF_VIRTUAL_TERMINAL_PROCESSING) | ||
| 17 | end | ||
| 18 | if sys.isatty(io.stderr) then | ||
| 19 | sys.setconsoleflags(io.stderr, sys.getconsoleflags(io.stderr) + sys.COF_VIRTUAL_TERMINAL_PROCESSING) | ||
| 20 | end | ||
| 21 | if sys.isatty(io.stdin) then | ||
| 22 | sys.setconsoleflags(io.stdin, sys.getconsoleflags(io.stdout) + sys.ENABLE_VIRTUAL_TERMINAL_INPUT) | ||
| 23 | end | ||
| 24 | |||
| 25 | |||
| 26 | else | ||
| 27 | -- On Posix, one can set a variable to an empty string, but on Windows, this | ||
| 28 | -- will remove the variable from the environment. To make this consistent | ||
| 29 | -- across platforms, we will remove the variable from the environment if the | ||
| 30 | -- value is an empty string. | ||
| 31 | local old_setenv = sys.setenv | ||
| 32 | function sys.setenv(name, value) | ||
| 33 | if value == "" then value = nil end | ||
| 34 | return old_setenv(name, value) | ||
| 35 | end | ||
| 36 | end | ||
| 37 | |||
