aboutsummaryrefslogtreecommitdiff
path: root/examples/compat.lua
diff options
context:
space:
mode:
Diffstat (limited to 'examples/compat.lua')
-rw-r--r--examples/compat.lua40
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
4local sys = require "system"
5
6
7
8if 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
29else
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
39end
40