diff options
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 | |||