-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnative
322 lines (299 loc) · 6.19 KB
/
native
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
-- this is like a .dll or .so file, as it defines all the functions
function os.readOnly(t)
local proxy = {}
local mt = { -- create a meta-table
__index = t,
__newindex = function (t,k,v)
error("Attempt to update a read-only table", 2)
end
}
setmetatable(proxy, mt)
return proxy
end
local colorNumberMap = os.readOnly({
['0'] = colors.white,
['1'] = colors.orange,
['2'] = colors.magenta,
['3'] = colors.lightBlue,
['4'] = colors.yellow,
['5'] = colors.lime,
['6'] = colors.pink,
['7'] = colors.gray,
['8'] = colors.lightGray,
['9'] = colors.cyan,
['a'] = colors.purple,
['b'] = colors.blue,
['c'] = colors.brown,
['d'] = colors.green,
['e'] = colors.red,
['f'] = colors.black
})
local colorWordMap = os.readOnly({
['white'] = colors.white,
['orange'] = colors.orange,
['magenta'] = colors.magenta,
['lightBlue'] = colors.lightBlue,
['yellow'] = colors.yellow,
['lime'] = colors.lime,
['pink'] = colors.pink,
['gray'] = colors.gray,
['lightGray'] = colors.lightGray,
['cyan'] = colors.cyan,
['purple'] = colors.purple,
['blue'] = colors.blue,
['brown'] = colors.brown,
['green'] = colors.green,
['red'] = colors.red,
['black'] = colors.black
})
function colors.asColor(str)
local s = string.lower(str)
local res = colorNumberMap[s]
if res == nil then
res = colorWordMap[s]
if res == nil then
error("Invalid color: " .. str, 2)
end
end
return res
end
function http.download(url, file)
local req = http.get(url)
if not req then
error("Could not reach " .. url, 2)
end
local content = req.readAll()
if not content then
error("Could not connect to " .. url)
end
f = fs.open(file, "w")
f.write(content)
f.close()
end
function string.asBoolean(str)
local s = string.lower(str)
if s == 'yes' or s == 'y' or s == 'true' or s == 't' or str == '1' then
return true
elseif s == 'no' or s == 'n' or s == 'false' or s == 'f' or str == '0' then
return false
else
error("Invalid boolean", 2)
end
end
function string.explode(str, div)
if (div == '') then
return false
end
local pos, res = 0, {}
-- for each divider found
for st,sp in function() return string.find(str, div, pos, true) end do
res[#res + 1] = string.sub(str, pos, st - 1) -- Attach chars left of current divider
pos = sp + 1 -- Jump past current divider
end
res[#res + 1] = string.sub(str, pos) -- Attach chars right of last divider
return res
end
function string.ends(str, End)
return string.sub(str, string.len(str) - string.len(End) + 1) == End
end
function string.starts(str, Start)
return string.sub(str, 1, string.len(Start)) == Start
end
local sb = term.setBackgroundColor
local sf = term.setTextColor
local curb
local curf
if term.isColor() then
curb = colors.black
curf = colors.white
end
function term.setBackgroundColor(col)
sb(col)
curb = col
end
function term.setTextColor(col)
sf(col)
curf = col
end
function term.getBackgroundColor()
return curb
end
function term.getTextColor()
return curf
end
function term.YN()
while true do
local x, y = term.getCursorPos()
local r = read()
local ok, r = os.try(string.asBoolean, r)
if ok then
return r
end
term.setCursorPos(x, y)
for i=1, string.len(r) do
write(' ')
end
term.setCursorPos(x, y)
end
end
function term.writeColored(text, fgc, bgc)
if term.isColor() then
local bg = term.getBackgroundColor()
local fg = term.getTextColor()
if bgc ~= nil then
term.setBackgroundColor(bgc)
end
term.setTextColor(fgc)
write(text)
if bgc ~= nil then
term.setBackgroundColor(bg)
end
term.setTextColor(fg)
else
write(text)
end
end
function os.castToTypeOf(val, typeOf)
local t, f = type(typeOf), type(val)
if t == f then
return val
elseif t == "number" then
return tonumber(val)
elseif t == "string" then
return tostring(val)
elseif t == "boolean" and f == "string" then
return string.asBoolean(val)
else
error("Could not cast from " .. type(val) .. " to " .. t , 2)
end
end
-- term.clear() already exists
function os.clear()
term.clear()
term.setCursorPos(1, 1)
end
function os.loading(color, func) -- func is a function that returns a boolean (that is all), true means stop
local anim = { '|', '/', '-', '\\' }
local i = 1;
local x, y = term.getCursorPos()
while (func ~= nil and not func()) or true do
local x1, y1 = term.getCursorPos()
term.setCursorPos(x, y)
term.writeColored(anim[i], color ~= nil and color or term.getTextColor())
if x1 ~= x and y1 ~= y then
term.setCursorPos(x1, y1)
end
i = i + 1
if i > 4 then
i = 1
end
sleep(0.1)
end
end
function os.pause()
write("Press any key to continue")
os.pullEvent("key")
end
function os.spinner(color, func)
local anim = {{
" O ",
" O O ",
"O O",
"O O",
" O O ",
" O O "
}, {
" O O ",
" O ",
"O O",
"O O",
" O O ",
" O O "
}, {
" O O ",
" O O ",
"O ",
"O O",
" O O ",
" O O "
}, {
" O O ",
" O O ",
"O O",
"O ",
" O O ",
" O O "
}, {
" O O ",
" O O ",
"O O",
"O O",
" O ",
" O O "
}, {
" O O ",
" O O ",
"O O",
"O O",
" O O ",
" O "
}, {
" O O ",
" O O ",
"O O",
"O O",
" O O ",
" O "
}, {
" O O ",
" O O ",
"O O",
"O O",
" O ",
" O O "
}, {
" O O ",
" O O ",
"O O",
" O",
" O O ",
" O O "
}, {
" O O ",
" O O ",
" O",
"O O",
" O O ",
" O O "
}, {
" O O ",
" O ",
"O O",
"O O",
" O O ",
" O O "
}, {
" O ",
" O O ",
"O O",
"O O",
" O O ",
" O O "
}}
local frame = 1
local sw, sh = string.len(anim[1][1]), #anim[1]
while true do
for i=1,sh do
term.setCursorPos(math.ceil(w / 2 - sw / 2), math.ceil(h / 2) - sh / 2 + i - 1)
term.writeColored(anim[frame][i], color ~= nil and color or term.getTextColor())
end
frame = frame + 1
if frame > #anim then
frame = 1
end
sleep(0.075)
end
end
function os.try(func, ...)
return pcall(func, ...)
end