summaryrefslogtreecommitdiff
path: root/examples/compat.lua
blob: c00d44ad0007d725ce740eae9acf18645487f398 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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