-
Notifications
You must be signed in to change notification settings - Fork 0
/
term.lt
163 lines (132 loc) · 3.78 KB
/
term.lt
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
-- determine forward or back slash
var slash = package.config.sub(@, 1, 1)
-- ansi colors
var color = {
reset = "\27[0m"
, red = "\27[91;1m"
, green = "\27[92;1m"
, yellow = "\27[93;1m"
, blue = "\27[94;1m"
, magenta = "\27[95;1m"
, cyan = "\27[96;1m"
, white = "\27[97;1m"
}
-- we dont want ending newline in print()
-- but io.write() doesn't implicitly call tostring()
var write = \... ->
var n = select("#", ...)
for i = 1, n, 1
io.stdout.write(@, tostring(select(i, ...)))
-- separate each item with tab
--if i ~= n
-- io.stdout.write(@, "\t")
-- show usage and exit
var usage = \... ->
write(...)
os.exit(1)
-- parses command line
-- yields switch and its matching parameter if any
var scan = \args ->
-- const
var yield = coroutine.yield
var null = ""
return coroutine.wrap(->
var switch = null
var k = 1
while args[k]
var a = args[k]
if "-" == string.sub(a, 1, 1)
-- previous loop had a switch
if switch ~= null
yield(switch)
switch = string.sub(a, 2)
else
yield(switch, a)
switch = null
k = k + 1
if switch ~= null
yield(switch)
)
var localize = \path ->
if slash == '\\'
return string.gsub(path, '/', slash)
return string.gsub(path, '\\', slash)
--``
https://github.com/keplerproject/lua-compat-5.3/wiki/os.execute
os.execute in Lua 5.2 and 5.3 returns three values: a boolean indicating sucessful/unsuccessful execution, a string ("exit" or "signal"), and the actual exit status or signal number.
But in 5.1 only 1 one value returned
--``
var exec = \cmd ->
--write(cmd)
var ok, exit_or_signal, code = os.execute(cmd)
if code
return code
-- Lua 5.1, ok is code
return ok
var mkdir = \path ->
var cmd
if slash == '\\'
cmd = "md " .. path
else
cmd = "mkdir -p " .. path
var code = exec(cmd)
-- success is always 0, see man page for command
if code == 0
return true
return false, cmd .. " failed, exit code: " .. tostring(code)
-- pushd and popd exists on linux, unix and DOS
var exist_dir = \path ->
var p = string.gsub(path, "/*$", "")
-- if Path Not Found, dont show on stderr
var code = exec("pushd " .. p .. " 2> nul")
if code == 0
exec("popd")
return code == 0
var list_files = \path ->
var cmd
if slash == '\\'
cmd = 'dir /b/a:-D "' .. path .. '"'
else
cmd = '/bin/ls -p "' .. path .. '" | grep -v /'
return io.popen(cmd)
-- Window to support ANSI color, may not work on all Windows version
if slash == '\\'
var bit = require("bit")
var ffi = require("ffi")
var kernel32 = ffi.load("kernel32")
ffi.cdef(``
typedef long BOOL;
typedef void* HANDLE;
typedef uint32_t DWORD;
typedef DWORD* LPDWORD;
static const int STD_OUTPUT_HANDLE = ((DWORD)-11);
static const int ENABLE_VIRTUAL_TERMINAL_PROCESSING = ((DWORD)4);
HANDLE GetStdHandle(DWORD nStdHandle);
BOOL GetConsoleMode(HANDLE hConsoleHandle, LPDWORD lpMode);
BOOL SetConsoleMode(HANDLE hConsoleHandle, DWORD dwMode);
DWORD GetLastError(void);
``)
var enable_VT = ->
var handle = kernel32.GetStdHandle(kernel32.STD_OUTPUT_HANDLE)
var lpMode = ffi.new("DWORD[1]")
var res = kernel32.GetConsoleMode(handle, lpMode)
if res ~= 0
var mode = bit.bor(lpMode[0], kernel32.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
res = kernel32.SetConsoleMode(handle, mode)
if res ~= 0
return true
return false
if not enable_VT()
-- write('No ANSI colors support, error ' .. kernel32.GetLastError())
color = setmetatable({}, {__index = -> return ""})
return {
slash = slash
, color = color
, write = write
, usage = usage
, scan = scan
, localize = localize
, mkdir = mkdir
, exist_dir = exist_dir
, list_files = list_files
}