-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathstartup.lua
More file actions
305 lines (296 loc) · 10.9 KB
/
startup.lua
File metadata and controls
305 lines (296 loc) · 10.9 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
-- Setup paths
local sPath = ".:/rom/programs"
if term.isColor() then
sPath = sPath..":/rom/programs/advanced"
end
if turtle then
sPath = sPath..":/rom/programs/turtle"
else
sPath = sPath..":/rom/programs/rednet:/rom/programs/fun"
if term.isColor() then
sPath = sPath..":/rom/programs/fun/advanced"
end
end
if pocket then
sPath = sPath..":/rom/programs/pocket"
end
if commands then
sPath = sPath..":/rom/programs/command"
end
if http then
sPath = sPath..":/rom/programs/http"
end
if worldfs then
sPath = sPath..":/rom/programs/worldfs"
end
shell.setPath( sPath )
help.setPath( "/rom/help" )
-- Setup aliases
shell.setAlias( "ls", "list" )
shell.setAlias( "dir", "list" )
shell.setAlias( "cp", "copy" )
shell.setAlias( "mv", "move" )
shell.setAlias( "rm", "delete" )
shell.setAlias( "clr", "clear" )
shell.setAlias( "rs", "redstone" )
shell.setAlias( "sh", "shell" )
if term.isColor() then
shell.setAlias( "background", "bg" )
shell.setAlias( "foreground", "fg" )
end
-- Setup completion functions
local function completeMultipleChoice( sText, tOptions, bAddSpaces )
local tResults = {}
for n=1,#tOptions do
local sOption = tOptions[n]
if #sOption + (bAddSpaces and 1 or 0) > #sText and string.sub( sOption, 1, #sText ) == sText then
local sResult = string.sub( sOption, #sText + 1 )
if bAddSpaces then
table.insert( tResults, sResult .. " " )
else
table.insert( tResults, sResult )
end
end
end
return tResults
end
local function completePeripheralName( sText, bAddSpaces )
return completeMultipleChoice( sText, peripheral.getNames(), bAddSpaces )
end
local tRedstoneSides = redstone.getSides()
local function completeSide( sText, bAddSpaces )
return completeMultipleChoice( sText, tRedstoneSides, bAddSpaces )
end
local function completeFile( shell, nIndex, sText, tPreviousText )
if nIndex == 1 then
return fs.complete( sText, shell.dir(), true, false )
end
end
local function completeDir( shell, nIndex, sText, tPreviousText )
if nIndex == 1 then
return fs.complete( sText, shell.dir(), false, true )
end
end
local function completeEither( shell, nIndex, sText, tPreviousText )
if nIndex == 1 then
return fs.complete( sText, shell.dir(), true, true )
end
end
local function completeEitherEither( shell, nIndex, sText, tPreviousText )
if nIndex == 1 then
local tResults = fs.complete( sText, shell.dir(), true, true )
for n=1,#tResults do
local sResult = tResults[n]
if string.sub( sResult, #sResult, #sResult ) ~= "/" then
tResults[n] = sResult .. " "
end
end
return tResults
elseif nIndex == 2 then
return fs.complete( sText, shell.dir(), true, true )
end
end
local function completeProgram( shell, nIndex, sText, tPreviousText )
if nIndex == 1 then
return shell.completeProgram( sText )
end
end
local function completeHelp( shell, nIndex, sText, tPreviousText )
if nIndex == 1 then
return help.completeTopic( sText )
end
end
local function completeAlias( shell, nIndex, sText, tPreviousText )
if nIndex == 2 then
return shell.completeProgram( sText )
end
end
local function completePeripheral( shell, nIndex, sText, tPreviousText )
if nIndex == 1 then
return completePeripheralName( sText )
end
end
local tGPSOptions = { "host", "host ", "locate" }
local function completeGPS( shell, nIndex, sText, tPreviousText )
if nIndex == 1 then
return completeMultipleChoice( sText, tGPSOptions )
end
end
local tLabelOptions = { "get", "get ", "set ", "clear", "clear " }
local function completeLabel( shell, nIndex, sText, tPreviousText )
if nIndex == 1 then
return completeMultipleChoice( sText, tLabelOptions )
elseif nIndex == 2 then
return completePeripheralName( sText )
end
end
local function completeMonitor( shell, nIndex, sText, tPreviousText )
if nIndex == 1 then
return completePeripheralName( sText, true )
elseif nIndex == 2 then
return shell.completeProgram( sText )
end
end
local tRedstoneOptions = { "probe", "set ", "pulse " }
local function completeRedstone( shell, nIndex, sText, tPreviousText )
if nIndex == 1 then
return completeMultipleChoice( sText, tRedstoneOptions )
elseif nIndex == 2 then
return completeSide( sText )
end
end
local tDJOptions = { "play", "play ", "stop " }
local function completeDJ( shell, nIndex, sText, tPreviousText )
if nIndex == 1 then
return completeMultipleChoice( sText, tDJOptions )
elseif nIndex == 2 then
return completePeripheralName( sText )
end
end
local tPastebinOptions = { "put ", "get ", "run " }
local function completePastebin( shell, nIndex, sText, tPreviousText )
if nIndex == 1 then
return completeMultipleChoice( sText, tPastebinOptions )
elseif nIndex == 2 then
if tPreviousText[2] == "put" then
return fs.complete( sText, shell.dir(), true, false )
end
end
end
local tChatOptions = { "host ", "join " }
local function completeChat( shell, nIndex, sText, tPreviousText )
if nIndex == 1 then
return completeMultipleChoice( sText, tChatOptions )
end
end
local function completeSet( shell, nIndex, sText, tPreviousText )
if nIndex == 1 then
return completeMultipleChoice( sText, settings.getNames(), true )
end
end
local tCommands
if commands then
tCommands = commands.list()
end
local function completeExec( shell, nIndex, sText, tPreviousText )
if nIndex == 1 and commands then
return completeMultipleChoice( sText, tCommands, true )
end
end
shell.setCompletionFunction( "rom/programs/alias.lua", completeAlias )
shell.setCompletionFunction( "rom/programs/cd.lua", completeDir )
shell.setCompletionFunction( "rom/programs/copy.lua", completeEitherEither )
shell.setCompletionFunction( "rom/programs/delete.lua", completeEither )
shell.setCompletionFunction( "rom/programs/drive.lua", completeDir )
shell.setCompletionFunction( "rom/programs/edit.lua", completeFile )
shell.setCompletionFunction( "rom/programs/eject.lua", completePeripheral )
shell.setCompletionFunction( "rom/programs/gps.lua", completeGPS )
shell.setCompletionFunction( "rom/programs/help.lua", completeHelp )
shell.setCompletionFunction( "rom/programs/id.lua", completePeripheral )
shell.setCompletionFunction( "rom/programs/label.lua", completeLabel )
shell.setCompletionFunction( "rom/programs/list.lua", completeDir )
shell.setCompletionFunction( "rom/programs/mkdir.lua", completeFile )
shell.setCompletionFunction( "rom/programs/monitor.lua", completeMonitor )
shell.setCompletionFunction( "rom/programs/move.lua", completeEitherEither )
shell.setCompletionFunction( "rom/programs/redstone.lua", completeRedstone )
shell.setCompletionFunction( "rom/programs/rename.lua", completeEitherEither )
shell.setCompletionFunction( "rom/programs/shell.lua", completeProgram )
shell.setCompletionFunction( "rom/programs/type.lua", completeEither )
shell.setCompletionFunction( "rom/programs/set.lua", completeSet )
shell.setCompletionFunction( "rom/programs/advanced/bg.lua", completeProgram )
shell.setCompletionFunction( "rom/programs/advanced/fg.lua", completeProgram )
shell.setCompletionFunction( "rom/programs/fun/dj.lua", completeDJ )
shell.setCompletionFunction( "rom/programs/fun/advanced/paint.lua", completeFile )
shell.setCompletionFunction( "rom/programs/http/pastebin.lua", completePastebin )
shell.setCompletionFunction( "rom/programs/rednet/chat.lua", completeChat )
shell.setCompletionFunction( "rom/programs/command/exec.lua", completeExec )
if turtle then
local tGoOptions = { "left", "right", "forward", "back", "down", "up" }
local function completeGo( shell, nIndex, sText )
return completeMultipleChoice( sText, tGoOptions, true)
end
local tTurnOptions = { "left", "right" }
local function completeTurn( shell, nIndex, sText )
return completeMultipleChoice( sText, tTurnOptions, true )
end
local tEquipOptions = { "left", "right" }
local function completeEquip( shell, nIndex, sText )
if nIndex == 2 then
return completeMultipleChoice( sText, tEquipOptions )
end
end
local function completeUnequip( shell, nIndex, sText )
if nIndex == 1 then
return completeMultipleChoice( sText, tEquipOptions )
end
end
shell.setCompletionFunction( "rom/programs/turtle/go.lua", completeGo )
shell.setCompletionFunction( "rom/programs/turtle/turn.lua", completeTurn )
shell.setCompletionFunction( "rom/programs/turtle/equip.lua", completeEquip )
shell.setCompletionFunction( "rom/programs/turtle/unequip.lua", completeUnequip )
end
if worldfs then
local tWorldFsOptions = { "list", "mount", "unmount"}
local function completeWorldFs(shell, nIndex, sText)
if nIndex == 1 then
return completeMultipleChoice(sText, tWorldFsOptions)
end
end
shell.setCompletionFunction("rom/programs/worldfs/worldfs.lua", completeWorldFs)
end
-- Run autorun files
if fs.exists( "/rom/autorun" ) and fs.isDir( "/rom/autorun" ) then
local tFiles = fs.list( "/rom/autorun" )
table.sort( tFiles )
for n, sFile in ipairs( tFiles ) do
if string.sub( sFile, 1, 1 ) ~= "." then
local sPath = "/rom/autorun/"..sFile
if not fs.isDir( sPath ) then
shell.run( sPath )
end
end
end
end
local function findStartups( sBaseDir )
local tStartups = nil
local sBasePath = "/" .. fs.combine( sBaseDir, "startup" )
local sStartupNode = shell.resolveProgram( sBasePath )
if sStartupNode then
tStartups = { sStartupNode }
end
-- It's possible that there is a startup directory and a startup.lua file, so this has to be
-- executed even if a file has already been found.
if fs.isDir( sBasePath ) then
if tStartups == nil then
tStartups = {}
end
for _,v in pairs( fs.list( sBasePath ) ) do
local sPath = "/" .. fs.combine( sBasePath, v )
if not fs.isDir( sPath ) then
tStartups[ #tStartups + 1 ] = sPath
end
end
end
return tStartups
end
-- Run the user created startup, either from disk drives or the root
local tUserStartups = nil
if settings.get( "shell.allow_startup" ) then
tUserStartups = findStartups( "/" )
end
if settings.get( "shell.allow_disk_startup" ) then
for n,sName in pairs( peripheral.getNames() ) do
if disk.isPresent( sName ) and disk.hasData( sName ) then
local startups = findStartups( disk.getMountPath( sName ) )
if startups then
tUserStartups = startups
break
end
end
end
end
if tUserStartups then
for _,v in pairs( tUserStartups ) do
shell.run( v )
end
end