-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
312 lines (255 loc) · 8.45 KB
/
init.lua
File metadata and controls
312 lines (255 loc) · 8.45 KB
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
local expect = require "cc.expect"
local syscall = require "_syscall"
package.loaded.os = nil
local G = setmetatable({
colors = require "colors",
colours = require "colours",
disk = require "disk",
fs = require "fs",
gps = require "gps",
help = require "help",
http = require "http",
keys = require "keys",
--multishell = require "multishell",
os = require "os",
paintutils = require "paintutils",
parallel = require "parallel",
peripheral = require "peripheral",
rednet = require "rednet",
redstone = require "redstone",
settings = require "settings",
shell = require "shell",
term = require "term",
textutils = require "textutils",
vector = require "vector",
window = require "window"
}, {__index = _G})
G.rs = G.redstone
G._HOST = syscall.cchost()
G._CC_DEFAULT_SETTINGS = ""
G.sleep = G.os.sleep
function G.write(text)
expect(1, text, "string")
local lines = 0
local w, h = G.term.getSize()
local function inc_cy(cy)
lines = lines + 1
if cy > h - 1 then
G.term.scroll(1)
return cy
else
return cy + 1
end
end
while #text > 0 do
local nl = text:find("\n") or #text
local chunk = text:sub(1, nl)
text = text:sub(#chunk + 1)
local has_nl = chunk:sub(-1) == "\n"
if has_nl then chunk = chunk:sub(1, -2) end
local cx, cy = G.term.getCursorPos()
while #chunk > 0 do
if cx > w then
G.term.setCursorPos(1, inc_cy(cy))
cx, cy = G.term.getCursorPos()
end
local to_write = chunk:sub(1, w - cx + 1)
G.term.write(to_write)
chunk = chunk:sub(#to_write + 1)
cx, cy = G.term.getCursorPos()
end
if has_nl then
G.term.setCursorPos(1, inc_cy(cy))
end
end
return lines
end
function G.print(...)
local args = table.pack(...)
for i=1, args.n, 1 do
args[i] = tostring(args[i])
end
return G.write(table.concat(args, " ") .. "\n")
end
function G.printError(...)
local old = G.term.getTextColor()
G.term.setTextColor(G.colors.red)
print(...)
G.term.setTextColor(old)
end
local empty = {}
function G.read(replace, history, complete, default)
expect(1, replace, "string", "nil")
expect(2, history, "table", "nil")
expect(3, complete, "function", "nil")
expect(4, default, "string", "nil")
if replace then replace = replace:sub(1, 1) end
local hist = history or {}
history = {}
for i=1, #hist, 1 do
history[i] = hist[i]
end
local buffer = default or ""
local prev_buf = buffer
history[#history+1] = buffer
local hist_pos = #history
local cursor_pos = 0
local stx, sty = G.term.getCursorPos()
local w, h = G.term.getSize()
local dirty = false
local completions = {}
local comp_id = 0
local function clearCompletion()
if completions[comp_id] then
G.write((" "):rep(#completions[comp_id]))
end
end
local function full_redraw(force)
if force or dirty then
if complete and buffer ~= prev_buf then
completions = complete(buffer) or empty
comp_id = math.min(1, #completions)
end
prev_buf = buffer
G.term.setCursorPos(stx, sty)
local text = buffer
if replace then text = replace:rep(#text) end
local ln = G.write(text)
if completions[comp_id] then
local oldfg = G.term.getTextColor()
local oldbg = G.term.getBackgroundColor()
G.term.setTextColor(G.colors.white)
G.term.setBackgroundColor(G.colors.gray)
ln = ln + G.write(completions[comp_id])
G.term.setTextColor(oldfg)
G.term.setBackgroundColor(oldbg)
else
ln = ln + G.write(" ")
end
if sty + ln > h then
sty = sty - (sty + ln - h)
end
end
-- set cursor to the appropriate spot
local cx, cy = stx, sty
cx = cx + #buffer - cursor_pos-- + #(completions[comp_id] or "")
while cx > w do
cx = cx - w
cy = cy + 1
end
G.term.setCursorPos(cx, cy)
end
G.term.setCursorBlink(true)
while true do
full_redraw()
-- get input
local evt, param = G.os.pullEvent()
if evt == "char" then
dirty = true
clearCompletion()
if cursor_pos == 0 then
buffer = buffer .. param
elseif cursor_pos == #buffer then
buffer = param .. buffer
else
buffer = buffer:sub(0, -cursor_pos - 1)..param..buffer:sub(-cursor_pos)
end
elseif evt == "paste" then
dirty = true
clearCompletion()
if cursor_pos == 0 then
buffer = buffer .. param
elseif cursor_pos == #buffer then
buffer = param .. buffer
else
buffer = buffer:sub(0, -cursor_pos - 1)..param..
buffer:sub(-cursor_pos+(#id-1))
end
elseif evt == "key" then
local id = G.keys.getName(param)
if id == "backspace" and #buffer > 0 then
dirty = true
if cursor_pos == 0 then
buffer = buffer:sub(1, -2)
clearCompletion()
elseif cursor_pos < #buffer then
buffer = buffer:sub(0, -cursor_pos - 2)..buffer:sub(-cursor_pos)
end
elseif id == "delete" and cursor_pos > 0 then
dirty = true
if cursor_pos == #buffer then
buffer = buffer:sub(2)
elseif cursor_pos == 1 then
buffer = buffer:sub(1, -2)
else
buffer = buffer:sub(0, -cursor_pos - 1) .. buffer:sub(-cursor_pos + 1)
end
cursor_pos = cursor_pos - 1
elseif id == "up" then
if #completions > 1 then
dirty = true
clearCompletion()
if comp_id > 1 then
comp_id = comp_id - 1
else
comp_id = #completions
end
elseif hist_pos > 1 then
cursor_pos = 0
history[hist_pos] = buffer
hist_pos = hist_pos - 1
buffer = (" "):rep(#buffer)
full_redraw(true)
buffer = history[hist_pos]
dirty = true
end
elseif id == "down" then
if #completions > 1 then
dirty = true
clearCompletion()
if comp_id < #completions then
comp_id = comp_id + 1
else
comp_id = 1
end
elseif hist_pos < #history then
cursor_pos = 0
history[hist_pos] = buffer
hist_pos = hist_pos + 1
buffer = (" "):rep(#buffer)
full_redraw(true)
buffer = history[hist_pos]
dirty = true
end
elseif id == "left" then
if cursor_pos < #buffer then
clearCompletion()
cursor_pos = cursor_pos + 1
end
elseif id == "right" then
if cursor_pos > 0 then
cursor_pos = cursor_pos - 1
elseif comp_id > 0 then
dirty = true
buffer = buffer .. completions[comp_id]
end
elseif id == "tab" then
if comp_id > 0 then
dirty = true
buffer = buffer .. completions[comp_id]
end
elseif id == "home" then
cursor_pos = #buffer
elseif id == "end" then
cursor_pos = 0
elseif id == "enter" then
clearCompletion()
G.write("\n")
break
end
end
end
G.term.setCursorBlink(false)
return buffer
end
return G