diff options
| author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2024-06-20 22:43:06 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-20 22:43:06 +0200 |
| commit | c1a64c1b75f97cef97965b3bd9a941564a6270bd (patch) | |
| tree | b9a92dff6462abd5859c3c76f19748fad5d6c025 /examples/compat.lua | |
| parent | 47c24eed0191f8f72646be63dee94ac2b35eb062 (diff) | |
| parent | b87e6d6d762ee823e81dd7a8984f330eb4018fd8 (diff) | |
| download | luasystem-c1a64c1b75f97cef97965b3bd9a941564a6270bd.tar.gz luasystem-c1a64c1b75f97cef97965b3bd9a941564a6270bd.tar.bz2 luasystem-c1a64c1b75f97cef97965b3bd9a941564a6270bd.zip | |
Merge pull request #21 from lunarmodules/terminal
Diffstat (limited to 'examples/compat.lua')
| -rw-r--r-- | examples/compat.lua | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/examples/compat.lua b/examples/compat.lua new file mode 100644 index 0000000..c712105 --- /dev/null +++ b/examples/compat.lua | |||
| @@ -0,0 +1,40 @@ | |||
| 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.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 console output to UTF-8 encoding. | ||
| 15 | sys.setconsoleoutputcp(sys.CODEPAGE_UTF8) | ||
| 16 | |||
| 17 | -- Set up the terminal to handle ANSI escape sequences on Windows. | ||
| 18 | if sys.isatty(io.stdout) then | ||
| 19 | sys.setconsoleflags(io.stdout, sys.getconsoleflags(io.stdout) + sys.COF_VIRTUAL_TERMINAL_PROCESSING) | ||
| 20 | end | ||
| 21 | if sys.isatty(io.stderr) then | ||
| 22 | sys.setconsoleflags(io.stderr, sys.getconsoleflags(io.stderr) + sys.COF_VIRTUAL_TERMINAL_PROCESSING) | ||
| 23 | end | ||
| 24 | if sys.isatty(io.stdin) then | ||
| 25 | sys.setconsoleflags(io.stdin, sys.getconsoleflags(io.stdout) + sys.ENABLE_VIRTUAL_TERMINAL_INPUT) | ||
| 26 | end | ||
| 27 | |||
| 28 | |||
| 29 | else | ||
| 30 | -- On Posix, one can set a variable to an empty string, but on Windows, this | ||
| 31 | -- will remove the variable from the environment. To make this consistent | ||
| 32 | -- across platforms, we will remove the variable from the environment if the | ||
| 33 | -- value is an empty string. | ||
| 34 | local old_setenv = sys.setenv | ||
| 35 | function sys.setenv(name, value) | ||
| 36 | if value == "" then value = nil end | ||
| 37 | return old_setenv(name, value) | ||
| 38 | end | ||
| 39 | end | ||
| 40 | |||
