forked from MewesK/Midcraft-Commander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
midcraft-commander
578 lines (494 loc) · 12.3 KB
/
midcraft-commander
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
-----------------------------------------------------------------------------
-- Midcraft Commander
-- Author: MewK
-- Version: 0.1.2
--
-- This program is released under the MIT License (MIT).
-----------------------------------------------------------------------------
local args = { ... }
if # args == 1 and (args[1] == '-h' or args[1] == '?') then
print('Midcraft Commander 0.1.2')
print('')
print('Keys:')
print(' Ctrl Activates the menu')
print(' Tab Activates the integrated shell')
print('')
print('Menu:')
print(' Run Run with parameters command')
print(' Ed Edit command')
print(' Cp Copy command')
print(' Ct Cut command')
print(' Pst Paste command')
print(' Dl Delete command')
print(' Rn Rename Command')
print(' NwFl New File command')
print(' NwDr New directory command')
print(' Qt Exit command')
return
end
local running = true
local w, h = term.getSize() -- int
local hw = math.floor(w / 2)
local views = {}
views[1] = { x = 1, y = 2, width = hw - 1, height = h - 3, active = true, scrollOffset = 0, selectionIndex = 1, currentDirectory = '', entryList = {} }
views[2] = { x = hw + 2, y = 2, width = hw - 1, height = h - 3, active = false, scrollOffset = 0, selectionIndex = 1, currentDirectory = '', entryList = {} }
local _activeView = 1;
local function activeView() return views[_activeView] end
local menuActive = false
local menuItems = { 'Run', 'Ed', 'Cp', 'Ct', 'Pst', 'Dl', 'Rn', 'NwFl', 'NwDr', 'Qt' }
local menuItemSelection = 1
local commandHistory = {}
local copyIndex = 0
local cutIndex = 0
local copyCache = nil
local cutCache = nil
-- GUI
local function trimString(text, width)
if width > 6 and string.len(text) > 0 and string.len(text) > width then
text = string.sub(text, 0, width - 3)..'...'
end
return text
end
local function drawEntries(view)
local drawIndex = 0
local startIndex = view.scrollOffset + 1
local endIndex = view.scrollOffset + view.height
if endIndex > # view.entryList then
endIndex = # view.entryList
end
for index = startIndex, endIndex do
local entry = view.entryList[index]
local _entry = trimString(entry, view.width - 5)
-- Selection indicator
if view.active and view.selectionIndex == index then
_entry = '» '.._entry
else
_entry = ' '.._entry
end
-- Copy indicator
if copyCache ~= nil and copyIndex == index then
local copyName = fs.getName(copyCache)
if fs.isDir(copyCache) then
copyName = '/'..copyName
end
if copyName == entry then
_entry = _entry..' +'
end
-- Cut indicator
elseif cutIndex == index and cutCache ~= nil then
local cutName = fs.getName(cutCache)
if fs.isDir(cutCache) then
cutName = '/'..cutName
end
if cutName == entry then
_entry = _entry..' -'
end
end
term.setCursorPos(view.x, view.y + drawIndex)
term.write(_entry)
drawIndex = drawIndex + 1
end
end
local function drawLineH(x, y, length)
term.setCursorPos(x, y)
for i = 1, length do
term.write('-')
end
end
local function drawLabeledLineH(x, y, length, text)
text = trimString(text, length - 5)
drawLineH(x, y, length)
term.setCursorPos(x + 1, y)
term.write(' '..text..' ')
end
local function drawLineV(x, y, length)
for i = 0, length - 1 do
term.setCursorPos(x, y + i)
if i == 0 or i == length - 1 then
term.write('+')
else
term.write('|')
end
end
end
local function redraw()
term.clear()
-- Draw frame
drawLabeledLineH(1, 1, hw, views[1].currentDirectory)
drawLabeledLineH(hw + 2, 1, hw, views[2].currentDirectory)
drawLineH(1, h - 1, w)
drawLineV(hw + 1, 1, h - 1)
drawLineV(hw, 1, h - 1)
-- Draw dir lists
for index, view in ipairs(views) do
drawEntries(view)
end
-- Draw scroll indicators
-- Left top
if views[1].scrollOffset > 0 then
term.setCursorPos(hw, 2)
term.write('#')
end
-- Left bottom
if # views[1].entryList - views[1].scrollOffset > views[1].height then
term.setCursorPos(hw, h - 2)
term.write('#')
end
-- Right top
if views[2].scrollOffset > 0 then
term.setCursorPos(hw + 1, 2)
term.write('#')
end
-- Right bottom
if # views[2].entryList - views[2].scrollOffset > views[2].height then
term.setCursorPos(hw + 1, h - 2)
term.write('#')
end
-- Draw menu
term.setCursorPos(3, h)
term.clearLine()
for index, item in ipairs(menuItems) do
if index == menuItemSelection and menuActive then
term.write('['..item..']')
else
term.write(' '..item..' ')
end
end
end
-- I/O
local function readUserLine(prefix, commandHistory)
term.setCursorPos(1, h)
term.clearLine()
term.write(prefix)
local line = read(nil, commandHistory)
return line
end
local function moveEntry(source, destination, copyMode, forceOverwrite)
-- Input validation
if not source or not destination or not fs.exists(source) then
return 0
end
-- File equals
if source == destination then
return -1
end
-- File exists
if fs.exists(destination) then
local overwrite
if forceOverwrite then
overwrite = 'y'
else
overwrite = readUserLine('File exists. Overwrite? (y/n): ', nil)
end
if overwrite == 'y' then
fs.delete(destination)
if copyMode then
fs.copy(source, destination)
else
fs.move(source, destination)
end
return 2
else
return -2
end
else
if copyMode then
fs.copy(source, destination)
else
fs.move(source, destination)
end
return 1
end
end
local function readDirectory(path)
-- Sort into dirs/files
local files = {}
local dirs = {}
if path ~= '/' then
table.insert(dirs, '/..')
end
for index, entry in pairs(fs.list(path)) do
local sPath = fs.combine(path, entry)
if fs.isDir(sPath) then
table.insert(dirs, '/'..entry)
else
table.insert(files, entry)
end
end
table.sort(dirs)
table.sort(files)
for index, file in ipairs(files) do
table.insert(dirs, file)
end
return dirs
end
-- Other
local function refreshViews()
for n, view in ipairs(views) do
view.entryList = readDirectory(view.currentDirectory)
end
redraw()
end
local function clearCache()
copyCache = nil
cutCache = nil
copyIndex = 0
cutIndex = 0
end
-- Execution
local function runProgram(path, ...)
term.clear()
term.setCursorPos(1, 1)
shell.run(path, ...)
-- Wait for user action if text is on screen
local cx,cy = term.getCursorPos()
if cx ~= 1 or cy ~= 1 then
local text = '< Press any key to continue >'
term.setCursorPos(hw - math.floor(string.len(text) / 2), h)
term.clearLine()
term.write(text)
repeat
local event, param = os.pullEvent()
until event == 'key'
end
redraw()
end
local function runCommand(command)
local words = {}
for match in string.gmatch(command, "[^ \t]+") do
table.insert(words, match)
end
if words[1] then
runProgram(words[1], unpack(words, 2))
refreshViews()
end
end
local function executeEntry(path)
if fs.exists(path) then
if fs.isDir(path) then
return readDirectory(path)
else
runProgram(path)
refreshViews()
end
end
return nil
end
-- Menu
local menuFunctions = {
-- Run with parameters command
['Run'] = function(path)
local parameter = readUserLine('Parameters: ', nil)
runCommand(path..' '..parameter)
redraw()
end,
-- Edit command
['Ed'] = function(path)
if not fs.isDir(path) then
runProgram('edit', path)
end
end,
-- Copy command
['Cp'] = function(path)
clearCache()
copyCache = path
copyIndex = activeView().selectionIndex
redraw()
end,
-- Cut command
['Ct'] = function(path)
clearCache()
cutCache = path
cutIndex = activeView().selectionIndex
redraw()
end,
-- Paste command
['Pst'] = function(path)
if copyCache ~= nil or cutCache ~= nil then
local code
-- Copy
if copyCache ~= nil then
code = moveEntry(copyCache, fs.combine(activeView().currentDirectory, fs.getName(copyCache)), true, false)
-- Move
else
code = moveEntry(cutCache, fs.combine(activeView().currentDirectory, fs.getName(cutCache)), false, false)
end
-- Overwrite canceled
if code == -2 then
redraw()
-- Source equals destination or invalid parameter
elseif code == -1 or code == 0 then
clearCache()
redraw()
-- Attempt successful
elseif code == 1 or code == 2 then
clearCache()
refreshViews()
end
end
end,
-- Delete command
['Dl'] = function(path)
path = shell.resolve(path)
fs.delete(path)
activeView().selectionIndex = activeView().selectionIndex - 1
if (activeView().selectionIndex < 1) then
activeView().selectionIndex = 1
end
refreshViews()
end,
-- New File command
['Rn'] = function(path)
local name = readUserLine('New Name: ', nil)
if name ~= '' then
moveEntry(path, fs.combine(activeView().currentDirectory, name), false, true)
refreshViews()
end
end,
-- New File command
['NwFl'] = function(path)
local name = readUserLine('Name: ', nil)
if name ~= '' then
path = shell.resolve('/'..fs.combine(activeView().currentDirectory, name))
local file = io.open(path, 'w')
file:write(' ') -- edit can't handle empty files
file:close()
refreshViews()
else
redraw()
end
end,
-- New directory command
['NwDr'] = function(path)
local name = readUserLine('Name: ', nil)
if name ~= '' then
path = shell.resolve('/'..fs.combine(activeView().currentDirectory, name))
fs.makeDir(path)
refreshViews()
else
redraw()
end
end,
-- Exit command
['Qt'] = function(path)
running = false
end
}
local function callMenuItem(name, path)
local menuFunction = menuFunctions[menuItems[name]]
if menuFunction then
menuFunction(path)
end
menuActive = false
redraw()
end
-- Body
for n, view in ipairs(views) do
view.currentDirectory = '/'..shell.dir();
view.entryList = readDirectory(view.currentDirectory);
end
redraw()
while running do
local event, param = os.pullEvent()
if event == 'key' then
-- Up
if param == 200 then
-- Move entry selection
if not menuActive then
if activeView().selectionIndex > 1 then
activeView().selectionIndex = activeView().selectionIndex - 1
if activeView().selectionIndex <= activeView().scrollOffset then
activeView().scrollOffset = activeView().scrollOffset - activeView().height
end
redraw()
end
end
-- Down
elseif param == 208 then
-- Move entry selection
if not menuActive then
if activeView().selectionIndex < # activeView().entryList then
activeView().selectionIndex = activeView().selectionIndex + 1
if activeView().selectionIndex > activeView().height + activeView().scrollOffset then
activeView().scrollOffset = activeView().scrollOffset + activeView().height
end
redraw()
end
end
-- Left
elseif param == 203 then
-- Change active side
if not menuActive then
if _activeView == 2 then
activeView().active = false
_activeView = 1
activeView().active = true
redraw()
end
-- Move menu selection
else
menuItemSelection = menuItemSelection - 1
if menuItemSelection < 1 then
menuItemSelection = # menuItems
end
redraw()
end
-- Right
elseif param == 205 then
-- Change active side
if not menuActive then
if _activeView == 1 then
activeView().active = false
_activeView = 2
activeView().active = true
redraw()
end
-- Move menu selection
else
menuItemSelection = menuItemSelection + 1
if menuItemSelection > # menuItems then
menuItemSelection = 1
end
redraw()
end
-- Enter
elseif param == 28 then
local targetPath = '/'..fs.combine(activeView().currentDirectory, activeView().entryList[activeView().selectionIndex])
-- Run
if not menuActive then
local newEntryList = executeEntry(targetPath)
if newEntryList ~= nil then
activeView().currentDirectory = targetPath
activeView().selectionIndex = 1
activeView().entryList = newEntryList
redraw()
end
-- Menu selection
else
callMenuItem(menuItemSelection, targetPath)
end
-- Ctrl (menu toggle)
elseif param == 29 then
menuActive = not menuActive
if menuActive then
menuItemSelection = 1
end
redraw()
-- Tab (command line toggle)
elseif param == 15 then
local command
repeat
command = readUserLine(shell.dir()..'> ', commandHistory)
if command ~= '' then
if command ~= commandHistory[# commandHistory] then
table.insert(commandHistory, command)
end
runCommand(command)
end
until command == ''
redraw()
end
end
end
term.clear()
term.setCursorPos(1, 1)